Skip to content

Instantly share code, notes, and snippets.

@neftaly
Last active April 10, 2022 18:47
Show Gist options
  • Select an option

  • Save neftaly/b77455b3df9a8b50bf8b to your computer and use it in GitHub Desktop.

Select an option

Save neftaly/b77455b3df9a8b50bf8b to your computer and use it in GitHub Desktop.

Revisions

  1. neftaly revised this gist Sep 16, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions curry.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    const curry = (fn, preArgs = []) => (...postArgs) => {
    const args = [...preArgs, ...postArgs];
    return (args.length < fn.length) ? curry(fn, args) : fn(...args);
    const curry = (fn, ...oldArgs) => (...newArgs) => {
    const args = [...oldArgs, ...newArgs];
    return (args.length < fn.length) ? curry(fn, ...args) : fn(...args);
    };
  2. neftaly revised this gist Sep 16, 2015. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions curry.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,4 @@
    const curry = (fn, preArgs = []) => (...postArgs) => {
    const args = [...preArgs, ...postArgs]; // Combine old and new arguments
    if (args.length < fn.length) { // Check ig enough arguments received
    return curry(fn, args); // Recurse to emit an argument receiver fn
    }
    return fn(...args); // Finished; Apply arguments to fn and return result
    const args = [...preArgs, ...postArgs];
    return (args.length < fn.length) ? curry(fn, args) : fn(...args);
    };
  3. neftaly revised this gist Sep 16, 2015. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions curry2_expanded.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    const curryN = (arity, fn) => {

    // Take old arguments, and an array of new arguments
    const receiver = oldArgs => (...newArgs) => {

    const args = [...oldArgs, ...newArgs]; // Combine old and new arguments

    if (args.length >= arity) { // Have enough arguments have been received?
    return fn(...args); // Run fn with argument list
    }

    return receiver(args); // Recurse, and await further arguments

    };

    return receiver([]); // Start with an empty array of prior arguments

    };

    const curry = fn => curryN(fn.length, fn);
  4. neftaly revised this gist Jul 3, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions curry2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const curryN = (arity, fn) => {
    const receiver = preArgs => (...postArgs) => {
    const args = [...preArgs, ...postArgs];
    return (args.length < arity) ? receiver(args) : fn(...args);
    }
    return receiver([]);
    }

    const curry = fn => curryN(fn.length, fn);
  5. neftaly revised this gist Jul 3, 2015. 2 changed files with 6 additions and 6 deletions.
    10 changes: 5 additions & 5 deletions curry.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    const curry = (fn, preArgs = []) => (...postArgs) => {
    const args = [...preArgs, ...postArgs]; // Combine old and new arguments
    if (args.length < fn.length) { // Check ig enough arguments received
    return curry(fn, args); // Recurse to emit an argument receiver fn
    }
    return fn(...args); // Finished; Apply arguments to fn and return result
    const args = [...preArgs, ...postArgs]; // Combine old and new arguments
    if (args.length < fn.length) { // Check ig enough arguments received
    return curry(fn, args); // Recurse to emit an argument receiver fn
    }
    return fn(...args); // Finished; Apply arguments to fn and return result
    };
    2 changes: 1 addition & 1 deletion test.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    const add3 = function (a, b, c) {
    return a + b + c;
    return a + b + c;
    };

    const x = curry(add3);
  6. neftaly created this gist Jul 3, 2015.
    7 changes: 7 additions & 0 deletions curry.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    const curry = (fn, preArgs = []) => (...postArgs) => {
    const args = [...preArgs, ...postArgs]; // Combine old and new arguments
    if (args.length < fn.length) { // Check ig enough arguments received
    return curry(fn, args); // Recurse to emit an argument receiver fn
    }
    return fn(...args); // Finished; Apply arguments to fn and return result
    };
    9 changes: 9 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const add3 = function (a, b, c) {
    return a + b + c;
    };

    const x = curry(add3);

    x(1)(1)(1); //=> 3
    x(1, 1)(1); //=> 3
    x(1, 1, 1); //=> 3