I understand that following will be:
[(+2),(+1)]<*>[1,2,3] == [3,4,5,2,3,4]
I also understand that fmap
is implemented as map
. But how could I mentally map this computation in my head? The first time I saw this I assumed the following:
[(*2),(+1)]<*>[1,2,3] == [4,5,6]
The second time around I though about: [[3,4,5],[2,3,4]]
. But on the other hand <*>
returns an f b
so I knew it wouldn't be possible. So my question is, what are the mental steps to make sense out of this?
Is a functor, which means that it has a unary (i.e. single-argument) map function (and, thus, an infix version of the same function, called <!> ). Provides a way to apply arguments one at a time, via a <*> function. Provides a way to " lift " values into the applicative. F#'s Option<'t> type is an applicative:
In this post, I'd like to dive deeper into the practical usage of applicatives in F#. We described an applicative as a type that: Is a functor, which means that it has a unary (i.e. single-argument) map function (and, thus, an infix version of the same function, called <!> ).
We described an applicative as a type that: Is a functor, which means that it has a unary (i.e. single-argument) map function (and, thus, an infix version of the same function, called <!> ). Provides a way to apply arguments one at a time, via a <*> function. Provides a way to " lift " values into the applicative.
In fact, instead of thinking of applicatives as souped-up functors, it's often easier to think of them as a "lite" version of monads. 2 Let's see where this notion takes us, starting with a basic computation builder for options: Using this builder, we can create monadic computation expressions, such as:
fs <*> xs
is more-or-less [f x | f <- fs, x <- xs]
. Monads have the Applicative instance
fM <*> xM = do
f <- fM
x <- xM
return (f x)
which corresponds to the list comprehension pretty directly.
To remember this you might find it easier to simply imagine <*>
to be ×
(Cartesian product):
[a, b, c] × [x, y, z] == [a x, a y, a z, b x, b y, ...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With