Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Do notation

node v8.17.0
version: 13.0.0
endpointsharetweet
const { mdo } = require("@masaeedu/do")
const { Arr } = require("@masaeedu/fp") { const result = mdo(Arr)(({ x, y }) => [ [x, () => [1, 2]], [y, () => [3, 4]], () => Arr.of(x + y) ]); console.log(result); // => [4, 5, 5, 6] }
const { Maybe } = require("@masaeedu/fp") { const { Just, Nothing, match } = Maybe const show = match({ Nothing: "Nothing", Just: x => `Just(${x})` }) const validate = x => x > 5 ? Nothing : Just(x) const result = i => mdo(Maybe)(({ x }) => [ [x, () => validate(i)], () => validate(x * 2) ]) console.log(show(result(6))) // => Nothing console.log(show(result(1))) // => Just(2) console.log(show(result(3))) // => Nothing }
// 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
Loading…

no comments

    sign in to comment