Map over a Maybe instance: apply the function to the wrapped value
if the instance is Just, and return Nothing if the
instance is Nothing.
map works a lot like Array.prototype.map: Maybe and Array are both
containers for other things. If you have no items in an array of numbers
named foo and call foo.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
call foo.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 – the
Nothing variant – you just get back an empty container. If the container has
something in it – the Just 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.)
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