GitHub 6248★

Flip the arguments of a function

JavaScript version

// Reverse the order of arguments
const flip =
(fn) =>
(...args) =>
fn(...args.reverse());

// For binary functions
const flip = (fn) => (b, a) => fn(a, b);

// Or for curried functions
const flip = (fn) => (b) => (a) => fn(a)(b);

Examples

const isParent = (parent, child) => parent.children.includes(child);
const isChild = flip(isParent);
Follow me on and to get more useful contents.