Skip to content

Instantly share code, notes, and snippets.

@kraftdorian
Created November 14, 2021 23:17
Show Gist options
  • Save kraftdorian/7c8cd7494deeb1dd3debde89dafe0a94 to your computer and use it in GitHub Desktop.
Save kraftdorian/7c8cd7494deeb1dd3debde89dafe0a94 to your computer and use it in GitHub Desktop.
TypeScript flip function
function flip<A, B, C>(f: (a0: A, a1: B) => C): (b0: B, b1: A) => C {
return (b0: B, b1: A) => {
return f(b1, b0);
}
}
// ==== TEST ====
const x: string = 'test';
const y: number = 1;
function f(arg1: string, arg2: number): string {
return arg1 + String(arg2);
}
const g = flip(f);
console.log(
f(x, y) === g(y, x)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment