Skip to content

Instantly share code, notes, and snippets.

@jdan
Last active November 11, 2020 18:58
Show Gist options
  • Select an option

  • Save jdan/58a9d8ee2e1cbb004e4bbb89dff3c9c8 to your computer and use it in GitHub Desktop.

Select an option

Save jdan/58a9d8ee2e1cbb004e4bbb89dff3c9c8 to your computer and use it in GitHub Desktop.

Revisions

  1. jdan revised this gist Nov 11, 2020. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions maybe.js
    Original file line number Diff line number Diff line change
    @@ -19,19 +19,18 @@ class BindFunction {
    }
    valueOf() {
    let m = oracle.pop();
    oracle.push(m.isNothing ? Nothing() : Just(this.f(m.value)));
    oracle.push(m.isNothing ? Nothing() : this.f(m.value));
    }
    }

    let double = new BindFunction((a) => a * 2);
    let double = new BindFunction((a) => Just(a * 2));
    let m = Just(5);
    let n = Nothing();

    console.log("m:", stringOfMaybe(m));
    m >>= double;
    console.log("m >>= double:", stringOfMaybe(oracle.pop()));

    let n = Nothing();

    console.log("n:", stringOfMaybe(n));
    n >>= double;
    console.log("n >>= double:", stringOfMaybe(oracle.pop()));
  2. jdan created this gist Nov 11, 2020.
    37 changes: 37 additions & 0 deletions maybe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    let oracle = [];

    class Maybe {
    constructor(isNothing, value) {
    this.isNothing = isNothing;
    this.value = value;
    }
    valueOf() {
    oracle.push(this);
    }
    }
    let Nothing = () => new Maybe(true);
    let Just = (v) => new Maybe(false, v);
    let stringOfMaybe = (m) => (m.isNothing ? "Nothing" : `Just ${m.value}`);

    class BindFunction {
    constructor(f) {
    this.f = f;
    }
    valueOf() {
    let m = oracle.pop();
    oracle.push(m.isNothing ? Nothing() : Just(this.f(m.value)));
    }
    }

    let double = new BindFunction((a) => a * 2);
    let m = Just(5);

    console.log("m:", stringOfMaybe(m));
    m >>= double;
    console.log("m >>= double:", stringOfMaybe(oracle.pop()));

    let n = Nothing();

    console.log("n:", stringOfMaybe(n));
    n >>= double;
    console.log("n >>= double:", stringOfMaybe(oracle.pop()));