gerich-home / it-depends

Lightweight dependency tracking library for JavaScript
http://it-depends-js.github.io/
Other
1 stars 2 forks source link

Parametric computeds? #7

Closed gerich-home closed 8 years ago

gerich-home commented 8 years ago

Sometimes the list of all computed values in your program is not known apriori. In that case you have to create an array that will store computeds for each value. There is an idea to introduce parametric computeds. You will be able to "curry" some parametrs that will be passed to your computed. The calculated value will be cached and associated with the passed parameters

var c = itDepends.value(1);
var d = itDepends.computed(function(a, b) {
    return Math.sqrt(b*b - 4*a*c());
});

console.log(d(2, 1)); // calculation
console.log(d(2, 1)); // cached value
console.log(d(10, 5)); // calculation
console.log(d(10, 5)); // cached value
console.log(d(2, 1)); // cached value
c(2);
console.log(d(10, 5)); // calculation
console.log(d(2, 1)); // calculation
console.log(d(2, 1)); // cached value

Concerns Parametric syntax conflicts with writable computed syntax. Idea is to make a breaking changeand introduce explicit write method There should be also some kind of curry method, so you will be able to get the child computed with fixed set of parameters

var d21 = d.with(2, 1);
console.log(d21());
gerich-home commented 8 years ago

Implemented core functionality in f3bf1bdbb46af30d36592c5a938c79c2ae8ed1f1 Curry is remaining to implement

gerich-home commented 8 years ago

Implemented via .withArgs(...) method