Skip to content

Instantly share code, notes, and snippets.

@livoras
Last active April 18, 2016 09:10
Show Gist options
  • Save livoras/ab43c879720f8b3c1446e44b196b67a2 to your computer and use it in GitHub Desktop.
Save livoras/ab43c879720f8b3c1446e44b196b67a2 to your computer and use it in GitHub Desktop.
Simple currying function.
/**
* 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