I am a newbie to purescript. This is the book Leanpub-purescript in which I'm learning. I can't understand what flip funtion is. Is that similar to swapping concepts?
> :type flip
forall a b c. (a -> b -> c) -> b -> a -> c
which means a value goes to b, then b to a, then c is itself??. I'm struck with this. Please explain flip concept and if the book I'm referring is not good, suggest some other materials
The
flipfunction reverses the order of arguments of a two-argument function. Consider a simplesubtractfunction:If
flipis called on thesubtractfunction, it changes which number is being subtracted from:It also works with functions of differing argument types:
If it makes more sense to you, try looking at flip as a function which accepts a two-argument function as an argument and returns another two-argument function as a result:
One of the use cases for
flipis when you want to partially apply a function, but the argument you want to partially apply is on the second place. You can thenflipthe original function, and partially apply the resulting function.