flip :: (a -> b -> c) -> b -> a -> c
flip f x y = f y x
A little better, I'd say. Note that the first line is the type signature and is technically optional.EDIT: and if that's too exotic for you, here's the solution in Python.
def flip(x):
return lambda *z: x(*reversed(z))Here's a similar version in C++ that is a one liner too.
https://godbolt.org/z/dhsdoGcc4
Notably, examine the simple assembly generated. In comparison, most other languages doing this will not be able to optimize away these abstractions at all.
The fact that the compiler is able to optimize away this code is a compiler issue, not a language issue. ghc will optimize away Haskell's flip and I didn't once have to write a compile-time index-reverser in template code.
Agreed, hence my one liner version.
> ghc will optimize away Haskell's flip
Can you show me how? I am seeing 5000 lines of assembly.