Last active
December 5, 2016 19:43
-
-
Save grvcoelho/29914e184075e7609c918cce2f37c400 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 curryN = (len, fn, received = []) => (...args) => { | |
let combined = [...received, ...args] | |
if (args.length === len) { | |
return fn(...combined) | |
} | |
return curryN(len - args.length, fn, combined) | |
} | |
const curry = fn => curryN(fn.length, fn) | |
const sum = curry((a, b) => a + b) |
const P = Symbol();
const _ = require('lodash')
const isPlaceholder = a => a === P
const merge = (dest, origin) => {
const newArgs = dest.map(i => {
let elem = i
if (isPlaceholder(i) && origin[0]) {
elem = origin.shift()
}
return elem;
})
return newArgs.concat(origin)
}
const actualLength = (arr) =>
arr.reduce((len, curr) => isPlaceholder(curr) ? len : len + 1, 0);
const curry = (fn, arity = fn.length, received = []) => (...args) => {
const combined = merge(_.clone(received), _.clone(args))
if (actualLength(args) >= arity) {
return fn(...combined)
}
return curry(fn, arity - actualLength(args), combined)
}
/**
* Run
*/
const fn = curry((a, b, c) => a - b * c)
const a = fn(10, P, 3)(5)
a
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WIP