Skip to content

Instantly share code, notes, and snippets.

@stefanfrede
Created August 3, 2016 05:22
Show Gist options
  • Save stefanfrede/a6c14ff422257b3cdfb46c50cb27880c to your computer and use it in GitHub Desktop.
Save stefanfrede/a6c14ff422257b3cdfb46c50cb27880c to your computer and use it in GitHub Desktop.
flipAndCurry is a function that takes a function as argument, “flips” the order of arguments around, and then curries it.
const flipAndCurry = (fn) =>
(first) => (second) => fn(second, first);
// Example:
// https://gist.github.com/stefanfrede/596597d8b544de08491364eab20053c6
const mapWith = flipAndCurry(map);
// flipAndCurry throws the current context away, so it can’t be used to flip methods.
// A small alteration gets the job done:
const flipAndCurry = (fn) =>
(first) =>
function (second) {
return fn.call(this, second, first);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment