Given value f
with type :: Applicative f => f (a -> b -> c)
, What's the best way to map arguments to the inner function.
So far I've found the following:
(\x -> x a b) <$> f
(flip ($ a) b) <$> f
($ b) <$> ($ a) <$> f
I guess my question is why Haskell doesn't have a :: a -> b -> (a -> b -> c) -> c
function. Or does it?
The
Applicative
class has the<*>
operator (usually pronounced "ap", and is equivalent toControl.Monad.ap
for mostMonad
s), which combined with the<$>
operator (itself just an infix alias forfmap
) lets you write code likeIf you need to apply pure arguments, then use the
pure
method of theApplicative
class:An example might be
The
Applicative f => f (a -> b -> c)
is being constructed byf <$>
here, so if you already had something likeThen you could just use it as
The
<*>
operator has the typeso if your function has type
x -> y -> z
, thena ~ x
andb ~ y -> z
, so repeated application of<*>
(get it?) passes more arguments to your wrapped function.