JSFiddle - React, Tailwind, and code Playground
by dimitrs_papadimitriou
JavaScript
var Sum = v => ({ v: v, empty: 0, concat: y => Sum(y.v + v) });
var n = (l, r) => ({
l: l,
r: r,
map: f => n(l.map(f), r.map(f)),
fold: (acc, accf) => l.fold(r.fold(acc, accf), accf),
foldMap: f => l.foldMap(f).concat(r.foldMap(f))
});
var lf = v => ({
v: v,
map: f => lf(f(v)),
fold: (acc, accf) => accf(acc, v),
foldMap: f => f(v)
});
var TreeMonoid = m => tree => ({
tree: tree,
empty: lf(m.empty),
concat: y => {
return n(
m(tree.l.v).concat(m(y.tree.l.v)),
m(tree.r.v).concat(m(y.tree.r.v))
);
}
});
var r = n(lf(7), n(lf(5), lf(6))).foldMap(x => (x > 4 ? [x] : []));
var r = n(lf(7), n(lf(5), lf(6))).fold(0, (acc, x) => acc + x);
var repository = {
get() {
return Promise.resolve([
{ id: 1, name: "i_", age: 25 },
{ id: 2, name: "j_", age: 29 }
]);
}
};
var IO = fn => ({
map: f => IO(() => f(fn())),
chain: f => IO(() => f(fn()).run()),
run: () => fn(),
fold: (acc, accf) => accf(acc, fn()),
cata: alg => alg(fn())
});
var reader = view => ({
map: f => reader(x => view(f(x))),
run: c => view(c),
chain: f => reader(v => f(view(v)))
});
var composeF = FGfab => ({
value: () => FGfab,
map: fab => composeF(FGfab.map(x => x.map(fab))),
app: function(cFGa) {
//FGfab.map(Gfab=>x=>Gfab(x))
return cFGa.map(FGa => FGfab.map(x => y => x + FGa));
},
traverse: (TyperRep, f) =>
FGfab.traverse(TyperRep, y => y.traverse(TyperRep, f))
});
Object.prototype.compose = function() {
return composeF(this);
};
Object.prototype.mapC = function(f) {
return composeF(this)
.map(f)
.value();
};
Promise.prototype.traverse = function(TyperRep, f) {
var initialPromise = this;
return new Promise(function(resolve) {
initialPromise.then(result => func(result).then(x => TyperRep(resolve(x))));
});
};
Promise.prototype.chain = function(func) {
var initialPromise = this;
return new Promise(function(resolve) {
initialPromise.then(result => func(result).then(x =>...