Draft tools to ease the writing of loops

This commit is contained in:
Tissevert 2021-01-06 12:33:12 +01:00
parent 0e92cf2b05
commit 3b69884615
2 changed files with 89 additions and 0 deletions

42
src/UnitJS/Async/Box.js Normal file
View File

@ -0,0 +1,42 @@
import * as Async from UnitJS.Async;
function Box(initValue) {
this.value = initValue;
}
Box.prototype.get = function(f) {
f(this.value);
};
Box.prototype.set = function(newValue) {
return function(f) {
this.value = newValue;
f();
}.bind(this);
};
Box.prototype.check = function(predicate) {
return function(f) {
predicate(this.value)(f);
}
};
Box.prototype.update = function(modifier) {
return function(f) {
modifier(this.value)(function (newValue) {this.value = newValue; f()});
}.bind(this);
};
return {
make: make
};
function make(initValue) {
var box = new Box(initValue);
return {
get: box.get.bind(box),
set: box.set.bind(box),
check: box.check.bind(box),
update: box.update.bind(box),
};
}

47
src/UnitJS/Curry.js Normal file
View File

@ -0,0 +1,47 @@
return {
curry: curry,
flip: flip,
uncurry: uncurry,
plus: plus,
minus: minus,
substractTo: substractTo,
times: times,
dividedBy: dividedBy,
divide: divide
};
function plus(a) {
return function(b) {
return a+b;
}
}
function minus(a) {
return function(b) {
return b-a; // reversed to match the expected infix feel : «minus(4)» should substract 4 to whatever's passed to it, not the other way round — which is implemented by substractTo
}
}
function substractTo(a) {
return function(b) {
return a-b;
}
}
function times(a) {
return function(b) {
return a*b;
}
}
function dividedBy(a) {
return function(b) {
return b/a; // same natural order as for minus — the other way round equivalent is divide
}
}
function divide(a) {
return function(b) {
return a/b;
}
}