Created
June 25, 2017 01:31
-
-
Save josephjunker/9a08d2847c33e07c6c2ae21ab6e00f85 to your computer and use it in GitHub Desktop.
partial function application with holes
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
function partialFactory() { | |
const hole = {}; | |
function mergeArgs(unmerged, merged, args) { | |
return ( | |
!unmerged.length && !args.length ? merged | |
: !args.length ? mergeArgs(unmerged.slice(1), merged.concat([unmerged[0]]), args) | |
: !unmerged.length ? mergeArgs(unmerged, merged.concat([args[0]]), args.slice(1)) | |
: unmerged[0] === hole ? mergeArgs(unmerged.slice(1), merged.concat([args[0]]), args.slice(1)) | |
: /* unmerged is not a hole */ mergeArgs(unmerged.slice(1), merged.concat([unmerged[0]]), args)); | |
} | |
return { | |
_: hole, | |
holePartial: (fn, ...args) => { | |
const initialArgs = args; | |
return (...args) => { | |
const mergedArgs = mergeArgs(initialArgs, [], args); | |
return fn.apply(null, mergedArgs); | |
}; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment