Skip to content

Instantly share code, notes, and snippets.

@RSNara
Last active October 4, 2015 08:20
Show Gist options
  • Save RSNara/ddb84fef7fca30b59bb2 to your computer and use it in GitHub Desktop.
Save RSNara/ddb84fef7fca30b59bb2 to your computer and use it in GitHub Desktop.
export const _ = Symbol();
export function curry(fn) {
return function func(...args) {
if (countRealArguments(_, args) < fn.length) {
return function inner(...innerArgs) {
return this::func(...merge(_, args, innerArgs))
}
} else {
return this::fn(...args);
}
}
}
export function compose(...fns) {
return function(value) {
while (fns.length) {
const fn = fns.pop();
value = this::fn(value);
}
return value;
}
}
function countRealArguments(unreal, args) {
return args.filter( x => !Object.is(x, unreal) ).length;
}
function merge(unreal, args1, args2) {
let [args, index] = args1.reduce(([args, index], current) => {
return [
Object.is(current, unreal) && index < args2.length
? [...args, args2[index++]]
: [...args, current],
index
];
}, [[], 0]);
return [...args, ...args2.slice(index)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment