Parameters

getterSetter{function(newVal, oldVal)}:

A function that gets, and optionally sets, the value of the compute. When called with no arguments, getterSetter should return the current value of the compute. When called with a single argument, getterSetter should arrange things so that the next read of the compute produces that value. This compute will automatically update its value when any observables values are read.

context{Object}:

The this to use when calling the getterSetter function.

Returns

compute(initialValue [, settings])

Creates a compute from a value and optionally specifies how to read, update, and
listen to changes in dependent values. This form of compute can be used to
create a compute that derives its value from any source.

Parameters

initialValue{*}:

The initial value of the compute. If settings is
not provided, the compute simply updates its value to whatever the first argument
to the compute is.

compute(initialValue, setter(newVal,oldVal))

Parameters

initialValue{*}:

The initial value of the compute.

setter{function(newVal, oldVal)}:

A function that is called when a compute is called with an argument. The function is passed
the first argumented passed to compute and the current value. If
set returns a value, it is used to compare to the current value of the compute. Otherwise,
get is called to get the current value of the compute and that value is used
to determine if the compute has changed values.

Computes are similar
to observable maps like can-define/map/map, but they represent a single value rather than a collection of values.

Computes can derive their value from other computes, maps and lists.
When the derived values change, the compute's value will be automatically updated. This
is can-compute's best feature. For example, the following combines the age
compute in the previous example, and a name compute into an info compute:

Derived computes

If you use a compute that derives its
value from properties of an observable map or other can-computes, the compute will listen for changes in those
properties and automatically recalculate itself, emitting a change event if its value
changes.

The following example shows creating a fullName compute
that derives its value from two properties on the person observe:

Because Person is an observable can-define/map/map can-compute knows to listen for changes because the map's firstName and lastName properties are read.

Translator computes - computes that update their derived values

Sometimes you need a compute to be able to translate one value to another. For example,
consider a widget that displays and allows you to update the progress in percent
of a task. It accepts a compute with values between 0 and 100. But,
our task observe has progress values between 0 and 1 like:

var task = new DefineMap({
progress: 0.75
});

Use compute( getterSetter ) to create a compute that updates itself
when task's progress changes, but can also update progress when
the compute function is called with a value. For example: