Public
Edited
Sep 8, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// compose1.f([args1, length1])(1, 2, 3)
Insert cell
Insert cell
id = x => x
Insert cell
list = funcs => x => funcs.map(func => func(x))
Insert cell
assertArray = x => {
if (!Array.isArray(x)) throw new Error('Not an array');
return x;
}
Insert cell
compose = funcs => x => {
let res = x;
for (const func of assertArray(funcs)) {
res = func(res);
}
return res;
}
Insert cell
length = compose([assertArray, x => x.length]);
Insert cell
$ = fn => (...args) => fn(args)
Insert cell
object = funcs => x => Object.fromEntries(funcs.map(([key, func]) => [key, func(x)]))
Insert cell
call = head => arg => x => head(x)(arg(x))
Insert cell
callConst = head => arg => x => head(arg(x))
Insert cell
cnst = val => x => val
Insert cell
embed = object([['$', id]])
Insert cell
getter = key => x => x[key]
Insert cell
Insert cell
assign3 = variable => val => ctx => {
ctx[variable] = val(ctx);
}
Insert cell
ops = funcs => x => {
let res;
for (const func of funcs) {
res = func(x);
}
return res;
}
Insert cell
ops([
assign3('x')(cnst(1)),
getter('x'),
])({})
Insert cell
plusTwo = x => y => x + y
Insert cell
takeArray = fun => x => {
let res = fun;
for (const arg of x) {
res = res(arg);
}
return res;
}
Insert cell
reduce = fun => init => arr => {
let res = init;
for (const x of arr) {
res = fun(res)(x);
}
return res;
}
Insert cell
ops([
assign3('x')(takeArray(callConst)([takeArray(plusTwo), cnst([3, 15])])),
getter('x'),
])({})
Insert cell
Insert cell
assign4 = name => value => ctx => ({...ctx, [name]: value(ctx)})
Insert cell
embedArgs = x => ({$: x})
Insert cell
makeFun4 = stmts => compose([embedArgs, ...stmts, getter('return')])
Insert cell
fun4 = makeFun4([
assign4('b')(cnst(10)),
assign4('return')(callConst(takeArray(plusTwo))(list([getter('$'), getter('b')]))),
])
Insert cell
fun4(42)
Insert cell
Insert cell
Insert cell
stored6 = () => {
const res = ctx => {
return ctx(res);
}
return res;
}
Insert cell
mkCtx6 = (parent, ...initDefs) => {
const defs = new Map(parent && parent.defs);
const cache = new Map();
const res = fun => {
if (!cache.has(fun)) {
let resFun = fun;
while (defs.has(resFun)) {
resFun = defs.get(resFun);
}
console.log(resFun);
cache.set(fun, resFun(res));
}
return cache.get(fun);
};
res.def = (fun1, fun2) => {
defs.set(fun1, fun2);
return res;
}
res.cache = cache;
res.defs = defs;
for (const def of (initDefs || [])) {
res.def(def[0], def[1]);
}
return res;
}
Insert cell
seven = cnst(7)
Insert cell
a = stored6()
Insert cell
b = stored6()
Insert cell
aPlusB = callConst(takeArray(plusTwo))(list([a, b]))
Insert cell
arg6 = stored6()
Insert cell
return6 = stored6()
Insert cell
ctx61 = mkCtx6(
null,
[a, arg6],
[b, seven],
[return6, aPlusB],
)
Insert cell
makeFun6 = parentCtx => x => return6(mkCtx6(parentCtx, [arg6, cnst(x)]))
Insert cell
fun6 = makeFun6(ctx61)
Insert cell
fun6(11)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more