Last active
April 18, 2016 09:10
-
-
Save livoras/ab43c879720f8b3c1446e44b196b67a2 to your computer and use it in GitHub Desktop.
Simple currying function.
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
/** | |
* toArray :: ArrayLike => Array | |
*/ | |
const toArray = (arrLike) => [].slice.call(arrLike, 0); | |
/** | |
* Currying a function | |
*/ | |
const curry = (fn) => { | |
const newContext = (args) => { | |
return function run() { | |
const newArgs = args.concat(toArray(arguments)); | |
return newArgs.length === fn.length | |
? fn.apply(fn, newArgs) | |
: newContext(newArgs); | |
}; | |
}; | |
return newContext([]); | |
}; | |
function compose () { | |
var fns = toArray(arguments); | |
if (fns === 0) return; | |
if (fns.length === 1) { | |
return function () { | |
var fn = fns[0]; | |
return fn.apply(fn, arguments); | |
} | |
} else { | |
return function () { | |
var rest = compose.apply( | |
compose, | |
fns.slice(1) | |
); | |
return fns[0]( | |
rest.apply(rest, arguments) | |
); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment