Last active
October 4, 2015 08:20
-
-
Save RSNara/ddb84fef7fca30b59bb2 to your computer and use it in GitHub Desktop.
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
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