Skip to content

Instantly share code, notes, and snippets.

@josephjunker
Created June 25, 2017 01:31
Show Gist options
  • Save josephjunker/9a08d2847c33e07c6c2ae21ab6e00f85 to your computer and use it in GitHub Desktop.
Save josephjunker/9a08d2847c33e07c6c2ae21ab6e00f85 to your computer and use it in GitHub Desktop.
partial function application with holes
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