4 ms·
This seems like an obvious functional-style function that C++ should have, given the trend for functional idioms in recent versions. However, even a C++ cynic s
by hackyhacky 1y ago
This seems like an obvious functional-style function that C++ should have, given the trend for functional idioms in recent versions. However, even a C++ cynic such as myself was shocked by the length and (to me) incomprehensible implementation at the end of the article. A definition of that complexity for something so simple tells me that something has gone off the rails. The equivalent function in Haskell, fwiw, can be implemented in its entirety as follows:
flip :: (a -> b -> c) -> b -> a -> c
flip f x y = f y x
- mcherm 1y agoThat DOES seem easy, but it's because the code you wrote only works for functions of two arguments. Can you write a Haskell version that works for functions with any number of arguments?
- rcxdude 1y agoDoesn't that version work with arbitrary numbers of arguments already thanks to currying?
- hackyhacky 1y agoSort of: the version I gave flips the first two parameters of a function, leaving the rest untouched. The C++ std::flip from the article, however, reverses the order of all parameters. Reproducing that in Haskell would require fancy type-level programming or compile-time metaprogramming, both of which would defeat the simplicity of my initial version.