Last active
May 31, 2017 08:02
-
-
Save alvaropinot/d9f3f6181998161122722aa005102564 to your computer and use it in GitHub Desktop.
curry and pipe playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pipe = (...functions) => startingValue => functions.reduce((actualValue, fn) => fn(actualValue), startingValue) | |
const sumTwo = (a) => a + 2 | |
const mult = (a) => a + 2 | |
const repeat = times => fn => Array(times).fill(fn) | |
const doTimes = times => fn => x => pipe(...repeat(times)(fn))(x) | |
const sumTwo4Times = doTimes(4)(sumTwo) | |
sumTwo4Times(4) | |
// var curry = (fn) => (...args) => () => fn(...args) | |
const curry = function (fn) { | |
const arity = fn.length | |
return function resolve (...args) { | |
console.log(args) | |
return args.length === arity ? | |
fn(...args) : | |
// function (foo) { | |
// console.log(foo) | |
// } | |
function (...foo) { | |
return resolve([...args, ...foo]) | |
} | |
} | |
} | |
const sum = function(a, b) { | |
return a + b; | |
} | |
curry(sum)(2, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment