// Some examples of asynchronicity monads
const { from, observe } = require("most")
const Most = require("@most/core")
{
const result = mdo(Most)(({ x, y }) => [
[x, () => from([1, 2, 3, 4])],
() => Most.of(x + 1)
])
observe(console.log, result)
// logs:
// => 2
// => 3
// => 4
// => 5
undefined;
}
const { Cont } = require("@masaeedu/fp")
{
const gettime = cb => cb(new Date().getTime() / 1000)
const delay = x => d => cb => setTimeout(() => cb(x), d * 1000)
const delay_ = delay(undefined)
const result = mdo(Cont)(({ start, t1, t2 }) => [
[start, () => gettime],
() => delay_(1),
[t1, () => gettime],
() => delay_(2),
[t2, () => gettime],
() => Cont.of([t1 - start, t2 - start])
]);
result(console.log);
// eventually logs, approximately:
// => [1, 3]
undefined;
}
// Future work:
// - A hypothetical "tediousmdo" that has more information and can be more efficient
// (e.g. by using applicative for things that have no data dependency). Also does
// not need to be declared in any particular order
// ```
// {
// const result = tediousmdo(Arr)({
// y: ({ x }) => [x, x],
// x: ( ) => [1, 2],
// out: ({ x, y }) => Arr.of(x + y)
// }); // => [2, 2, 4, 4]
// }
// ```
// - Do this as a [Babel macro](https://github.com/kentcdodds/babel-plugin-macros) instead of at runtime