The function to apply the value to if Maybe
is Just
.
A new Maybe
with the result of applying mapFn
to the value in
a Just
, or Nothing
if maybe
is Nothing
.
T The type of the wrapped value.
U The type of the wrapped value of the returned Maybe
.
Generated using TypeDoc
Map over a
Maybe
instance: apply the function to the wrapped value if the instance isJust
, and returnNothing
if the instance isNothing
.map
works a lot likeArray.prototype.map
:Maybe
andArray
are both containers for other things. If you have no items in an array of numbers namedfoo
and callfoo.map(x => x + 1)
, you'll still just have an array with nothing in it. But if you have any items in the array ([2, 3]
), and you callfoo.map(x => x + 1)
on it, you'll get a new array with each of those items inside the array "container" transformed ([3, 4]
).That's exactly what's happening with
map
. If the container is empty – theNothing
variant – you just get back an empty container. If the container has something in it – theJust
variant – you get back a container with the item inside transformed.(So... why not just use an array? The biggest reason is that an array can be any length. With a
Maybe
, we're capturing the idea of "something or nothing" rather than "0 to n" items. And this lets us implement a whole set of other interfaces, like those in this module.)Examples