Akshat Sharma

Introducing Compute

Updated on
Tagged as {javascript, compute}

Compute, a reactive programming library for the browser, and the server

tl;dr:

The (slightly) Longer Version

Compute provides an implementation of observable, and observable-array. Compute also lets you define relationships among these observables. Compute is compatible with knockoutjs, to the extent that it will use knockout's observables. Here's some sample code:

var base = Compute.o();
var altitude = Compute.o();
var hypotenuse = Compute.from(base, altitude, function(b, a){
  return Math.sqrt(b*b + a*a);
});
base(3);
altitude(4);
console.log(hypotenuse()); //5
base(5);
altitude(12);
console.log(hypotenuse()); //13

Compute provides two kinds of observables. Observables (Compute.o) and Observable Array (Compute.oa), like knockoutjs. Compute lets you string arbitrary number of such observables together.

var principle = Compute.o(100);
var rate = Compute.o(0.05);
var time = Compute.o(3);
var interest = Compute.from(principle, rate, time, function(p, r, t){
  return p * r * t;
});
interest.$fire(); //since observable subscriptions are only called on mutation, force value function
console.debug(interest);

Compute also allows you to call an arbitrary function without setting the value of an observable.

var aye = Compute.o();
var bee = Compute.o();
var see = Compute.o();
var dee = Compute.o();
Compute.on(aye, bee, see, dee, function(a, b, c, d){
  console.log("One of our observables changed. I can react to it!");
});

Works with Knockout

Finally, if you are using knockout, Compute uses ko.observable (and ko.observableArray) for C.o and C.oa. This lets you use C.o and C.oa in your viewmodel and have them directly bound to your knockout bindings.

Origins:

Compute is inspired by Knockout, Haskell and a proprietary library I use at work to make awesome end-to-end analytics for big-data.