This section is going to cover some useful functions, available by default, like const, apply, flip and more.
const :: forall a b. a -> b -> a
const x _ = x
filter (const true) [1, 2, 3]
--- [1, 2, 3]
In this example, const is used to solve a small problem. Filter takes a function as the first parameter. This function is used to determine, which values from the second parameter (an array) should be filtered out. To not filter anything at all, we pass const as it is a function and only returns the first parameter - "true". Therefore, our filter-criteria is "true", and thus, all values in the array are returned.
In case you need const, just the other way around, there is snoc.
flip helps us to flip around the order of parameters, applied to a function.
As we just learned about const, it helps us to visualize what flip actually does:
log (show (const 1 2))
Since the second parameter is ignored, and only the first one is returned, the above shown code prints "1".
Now, when applying flip to it, "2" is printed:
log (show (flip const 1 2))