Skip to content

Instantly share code, notes, and snippets.

@ivn-cote
Forked from Avaq/combinators.js
Created February 11, 2016 12:23
Show Gist options
  • Save ivn-cote/c42e3e52c7ddf61a0c9b to your computer and use it in GitHub Desktop.
Save ivn-cote/c42e3e52c7ddf61a0c9b to your computer and use it in GitHub Desktop.

Revisions

  1. @Avaq Avaq revised this gist Feb 10, 2016. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions combinators.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ const I = x => x;
    const K = x => _ => x;
    const A = f => x => f(x);
    const T = x => f => f(x);
    const W = f => x => f(x)(x);
    const C = f => y => x => f(x)(y);
    const B = f => g => x => f(g(x));
    const S = f => g => x => f(x)(g(x));
    1 change: 1 addition & 0 deletions combinators.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ identity | **I** | id | identity | `a → a`
    constant | **K** | const | always | `a → b → a`
    apply | **A** | ($) | call | `(a → b) → a → b`
    thrush | **T** | (&) | | `a → (a → b) → b`
    duplication | **W** | | | `(a → a → b) → a → b`
    flip | **C** | flip | flip | `(a → b → c) → b → a → c`
    compose | **B** | (.) | compose | `(b → c) → (a → b) → a → c`
    substitution | **S** | | | `(a → b → c) → (a → b) → a → c`
  2. @Avaq Avaq revised this gist Feb 8, 2016. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions combinators.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,4 @@ flip | **C** | flip | flip | `(a → b → c) → b → a → c`
    compose | **B** | (.) | compose | `(b → c) → (a → b) → a → c`
    substitution | **S** | | | `(a → b → c) → (a → b) → a → c`
    psi | **P** | on | | `(b → b → c) → (a → b) → a → a → c`
    fix | **Y** | fix | | `(a → a) → a`
    converge | | | converge | `(b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d`
    useWith | | | useWith | `(b -> d -> e) -> (a -> b) -> (c -> d) -> a -> c -> e`
    fix-point | **Y** | fix | | `(a → a) → a`
  3. @Avaq Avaq revised this gist Feb 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion combinators.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,6 @@ flip | **C** | flip | flip | `(a → b → c) → b → a → c`
    compose | **B** | (.) | compose | `(b → c) → (a → b) → a → c`
    substitution | **S** | | | `(a → b → c) → (a → b) → a → c`
    psi | **P** | on | | `(b → b → c) → (a → b) → a → a → c`
    fix | **Y** | fix | | `(Any... -> a) f => (f -> f) -> f`
    fix | **Y** | fix | | `(a → a) → a`
    converge | | | converge | `(b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d`
    useWith | | | useWith | `(b -> d -> e) -> (a -> b) -> (c -> d) -> a -> c -> e`
  4. @Avaq Avaq revised this gist Feb 1, 2016. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions pointfree-factorial.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    const {ifElse, dec} = R;
    const isZero = R.gt(1);
    const ifElse = f => g => h => x => f(x) ? g(x) : h(x);
    const isZero = a => a < 1;
    const mult = a => b => a * b;
    const dec = a => a - 1;

    const factorial = Y(B(ifElse(isZero, K(1)))(B(S(mult))(C(B)(dec))));
    const factorial = Y(B(ifElse(isZero)(K(1)))(B(S(mult))(C(B)(dec))));

    factorial(4); //> 24
    factorial(4);
  5. @Avaq Avaq revised this gist Jan 31, 2016. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions pointfree-factorial.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,3 @@
    const K = x => _ => x;
    const C = f => y => x => f(x)(y);
    const B = f => g => x => f(g(x));
    const S = f => g => x => f(x)(g(x));
    const Y = f => (g => g(g))(g => f(x => g(g)(x)));

    const {ifElse, dec} = R;
    const isZero = R.gt(1);
    const mult = a => b => a * b;
  6. @Avaq Avaq revised this gist Jan 31, 2016. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions pointfree-factorial.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    const K = x => _ => x;
    const C = f => y => x => f(x)(y);
    const B = f => g => x => f(g(x));
    const S = f => g => x => f(x)(g(x));
    const Y = f => (g => g(g))(g => f(x => g(g)(x)));

    const {ifElse, dec} = R;
    const isZero = R.gt(1);
    const mult = a => b => a * b;

    const factorial = Y(B(ifElse(isZero, K(1)))(B(S(mult))(C(B)(dec))));

    factorial(4); //> 24
  7. @Avaq Avaq revised this gist Jan 31, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions combinators.md
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@ Name | # | Haskell | Ramda | Signature
    ------------:|-------|---------|----------|----------
    identity | **I** | id | identity | `a → a`
    constant | **K** | const | always | `a → b → a`
    apply | **A** | $ | call | `(a → b) → a → b`
    thrush | **T** | | | `a → (a → b) → b`
    apply | **A** | ($) | call | `(a → b) → a → b`
    thrush | **T** | (&) | | `a → (a → b) → b`
    flip | **C** | flip | flip | `(a → b → c) → b → a → c`
    compose | **B** | . | compose | `(b → c) → (a → b) → a → c`
    compose | **B** | (.) | compose | `(b → c) → (a → b) → a → c`
    substitution | **S** | | | `(a → b → c) → (a → b) → a → c`
    psi | **P** | on | | `(b → b → c) → (a → b) → a → a → c`
    fix | **Y** | | | `(Any... -> a) f => (f -> f) -> f`
    fix | **Y** | fix | | `(Any... -> a) f => (f -> f) -> f`
    converge | | | converge | `(b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d`
    useWith | | | useWith | `(b -> d -> e) -> (a -> b) -> (c -> d) -> a -> c -> e`
  8. @Avaq Avaq revised this gist Jan 31, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions combinators.md
    Original file line number Diff line number Diff line change
    @@ -9,3 +9,5 @@ compose | **B** | . | compose | `(b → c) → (a → b) → a → c
    substitution | **S** | | | `(a → b → c) → (a → b) → a → c`
    psi | **P** | on | | `(b → b → c) → (a → b) → a → a → c`
    fix | **Y** | | | `(Any... -> a) f => (f -> f) -> f`
    converge | | | converge | `(b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d`
    useWith | | | useWith | `(b -> d -> e) -> (a -> b) -> (c -> d) -> a -> c -> e`
  9. @Avaq Avaq revised this gist Jan 31, 2016. No changes.
  10. @Avaq Avaq revised this gist Jan 31, 2016. 4 changed files with 0 additions and 75 deletions.
    30 changes: 0 additions & 30 deletions fold.js
    Original file line number Diff line number Diff line change
    @@ -1,30 +0,0 @@
    //Combinators
    const I = x => x;

    //List manipulation
    const head = xs => xs[0];
    const tail = xs => xs.slice(1);
    const append = x => xs => [...xs, x];

    //Iteration
    const foldl = f => y => xs => xs.length > 0 ? foldl(f)(f(y)(head(xs)))(tail(xs)) : y;
    const foldr = f => y => xs => xs.length > 0 ? f(foldr(f)(y)(tail(xs)))(head(xs)) : y;
    const map = f => foldl(y => x => append(f(x))(y))([]);
    const filter = f => foldl(y => x => f(x) ? append(x)(y) : y)([]);

    //Function composition
    const compose = f => g => x => f(g(x));
    const pipe = foldr(compose)(I);

    //Math
    const add = a => b => a + b;
    const mult = a => b => a * b;
    const gt = a => b => b > a;

    //Program
    pipe([
    map(add(1)),
    map(mult(2)),
    filter(gt(10))
    ])
    ([1, 2, 3, 4, 5, 6, 7, 8]);
    11 changes: 0 additions & 11 deletions permutations.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    const {map, join, addIndex, chain, isEmpty, insert, remove} = R;
    const cat = map(join(''));
    const ichain = addIndex(chain);

    //permutations :: [a] -> [[a]]
    const permutations = xs => isEmpty(xs) ? [[]] : ichain(
    (x, i) => map(R.insert(0, x), permutations(remove(i, 1, xs))),
    xs
    );

    cat(permutations(['A', 'B', 'C']));
    34 changes: 0 additions & 34 deletions reader-future.js
    Original file line number Diff line number Diff line change
    @@ -1,34 +0,0 @@
    console.clear();
    const then = Date.now();
    const log = x => console.log(`${Date.now() - then} - ${x}`);
    const err = x => console.log(`${Date.now() - then} - ERROR: ${x}`);
    const {K, I, pipe} = S;
    const {curry, reduce, ap, once, compose, chain, map} = R;
    const ftap = f => a => f(a).map(K(a));

    //A mock database
    const connect = curry(config => (log('Connecting...'), Future.of({name: 'Connection', config})))
    const close = curry(connection => Future((rej, res) => (log(`Closed ${connection.name}`), res(null))))
    const query = curry((x, connection) => Future((rej, res) =>
    setTimeout(() => (log(`QUERY[${x}]`), res(eval(x))), 300)
    ));


    const RFuture = Reader.T(Future);
    const app = RFuture(chain(query('1 + 1')));

    const closeConnection = () => RFuture.ask.chain(pipe([
    chain(close),
    RFuture.lift
    ]));

    const querySquare = n => RFuture.ask.chain(pipe([
    query(`${n} * ${n}`),
    RFuture.lift
    ]))

    app
    .chain(querySquare)
    .chain(ftap(closeConnection))
    .run(connect({foo: 'bar'}))
    .fork(err, log)
    File renamed without changes.
  11. @Avaq Avaq revised this gist Jan 31, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions combinators.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Name | # | Haskell | Ramda | Signature
    ------------:|-------|---------|----------|----------
    identity | **I** | id | identity | `a → a`
    constant | **K** | const | always | `a → b → a`
    apply | **A** | $ | call | `(a → b) → a → b`
    thrush | **T** | | | `a → (a → b) → b`
    flip | **C** | flip | flip | `(a → b → c) → b → a → c`
    compose | **B** | . | compose | `(b → c) → (a → b) → a → c`
    substitution | **S** | | | `(a → b → c) → (a → b) → a → c`
    psi | **P** | on | | `(b → b → c) → (a → b) → a → a → c`
    fix | **Y** | | | `(Any... -> a) f => (f -> f) -> f`
  12. @Avaq Avaq revised this gist Jan 30, 2016. 3 changed files with 20 additions and 3 deletions.
    9 changes: 9 additions & 0 deletions combinators.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const I = x => x;
    const K = x => _ => x;
    const A = f => x => f(x);
    const T = x => f => f(x);
    const C = f => y => x => f(x)(y);
    const B = f => g => x => f(g(x));
    const S = f => g => x => f(x)(g(x));
    const P = f => g => x => y => f(g(x))(g(y));
    const Y = f => (g => g(g))(g => f(x => g(g)(x)));
    11 changes: 11 additions & 0 deletions recursion.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    const head = xs => xs[0];
    const tail = xs => xs.slice(1);
    const add = a => b => a + b;

    const dot = Y(f => n => n < 1 ? '' : '.' + f(n - 1));
    const factorial = Y(f => n => n < 1 ? 1 : n * f(n - 1));
    const reduce = f => Y(g => y => xs => xs.length < 1 ? y : g(f(y)(head(xs)))(tail(xs)));

    dot(3) //> "..."
    factorial(4) //> 24
    reduce(add)(0)([1, 2, 3]) //> 6
    3 changes: 0 additions & 3 deletions y-combinator.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    const Y = f => (g => g(g))(g => f(x => g(g)(x)));
    const factorial = Y(f => n => n < 1 ? 1 : n * f(n - 1));
    factorial(4)
  13. @Avaq Avaq revised this gist Jan 30, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions y-combinator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    const Y = f => (g => g(g))(g => f(x => g(g)(x)));
    const factorial = Y(f => n => n < 1 ? 1 : n * f(n - 1));
    factorial(4)
  14. @Avaq Avaq revised this gist Jan 29, 2016. 1 changed file with 34 additions and 0 deletions.
    34 changes: 34 additions & 0 deletions reader-future.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    console.clear();
    const then = Date.now();
    const log = x => console.log(`${Date.now() - then} - ${x}`);
    const err = x => console.log(`${Date.now() - then} - ERROR: ${x}`);
    const {K, I, pipe} = S;
    const {curry, reduce, ap, once, compose, chain, map} = R;
    const ftap = f => a => f(a).map(K(a));

    //A mock database
    const connect = curry(config => (log('Connecting...'), Future.of({name: 'Connection', config})))
    const close = curry(connection => Future((rej, res) => (log(`Closed ${connection.name}`), res(null))))
    const query = curry((x, connection) => Future((rej, res) =>
    setTimeout(() => (log(`QUERY[${x}]`), res(eval(x))), 300)
    ));


    const RFuture = Reader.T(Future);
    const app = RFuture(chain(query('1 + 1')));

    const closeConnection = () => RFuture.ask.chain(pipe([
    chain(close),
    RFuture.lift
    ]));

    const querySquare = n => RFuture.ask.chain(pipe([
    query(`${n} * ${n}`),
    RFuture.lift
    ]))

    app
    .chain(querySquare)
    .chain(ftap(closeConnection))
    .run(connect({foo: 'bar'}))
    .fork(err, log)
  15. @Avaq Avaq revised this gist Jan 18, 2016. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions fold.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,6 @@
    //Combinators
    const I = x => x;

    //Function composition
    const compose = f => g => x => f(g(x));

    //List manipulation
    const head = xs => xs[0];
    const tail = xs => xs.slice(1);
    @@ -13,11 +10,21 @@ const append = x => xs => [...xs, x];
    const foldl = f => y => xs => xs.length > 0 ? foldl(f)(f(y)(head(xs)))(tail(xs)) : y;
    const foldr = f => y => xs => xs.length > 0 ? f(foldr(f)(y)(tail(xs)))(head(xs)) : y;
    const map = f => foldl(y => x => append(f(x))(y))([]);
    const filter = f => foldl(y => x => f(x) ? append(x)(y) : y)([]);

    //Function composition
    const compose = f => g => x => f(g(x));
    const pipe = foldr(compose)(I);

    //Lib
    //Math
    const add = a => b => a + b;
    const mult = a => b => a * b;
    const pipe = foldr(compose)(I);
    const gt = a => b => b > a;

    //Program
    map(pipe([add(1), mult(2)]))([1, 2, 3]);
    pipe([
    map(add(1)),
    map(mult(2)),
    filter(gt(10))
    ])
    ([1, 2, 3, 4, 5, 6, 7, 8]);
  16. @Avaq Avaq revised this gist Jan 18, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions permutations.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    const {map, join, addIndex, chain, isEmpty, insert, remove} = R;
    const cat = map(join(''));
    const ichain = addIndex(chain);

    //permutations :: [a] -> [[a]]
    const permutations = xs => isEmpty(xs) ? [[]] : ichain(
    (x, i) => map(R.insert(0, x), permutations(remove(i, 1, xs))),
    xs
    );

    cat(permutations(['A', 'B', 'C']));
  17. @Avaq Avaq created this gist Jan 17, 2016.
    23 changes: 23 additions & 0 deletions fold.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    //Combinators
    const I = x => x;

    //Function composition
    const compose = f => g => x => f(g(x));

    //List manipulation
    const head = xs => xs[0];
    const tail = xs => xs.slice(1);
    const append = x => xs => [...xs, x];

    //Iteration
    const foldl = f => y => xs => xs.length > 0 ? foldl(f)(f(y)(head(xs)))(tail(xs)) : y;
    const foldr = f => y => xs => xs.length > 0 ? f(foldr(f)(y)(tail(xs)))(head(xs)) : y;
    const map = f => foldl(y => x => append(f(x))(y))([]);

    //Lib
    const add = a => b => a + b;
    const mult = a => b => a * b;
    const pipe = foldr(compose)(I);

    //Program
    map(pipe([add(1), mult(2)]))([1, 2, 3]);