scala collection
map
documentation says this on map
function:
Builds a new collection by applying a function to all elements of this list.
so it runs on every item in the collection.
but for try it says:
Maps the given function to the value from this Success or returns this if this is a Failure.
so it runs only on success
, while i can understand nothing to run on failure
i could still just print it. why is it inconsistent with collection map interface? do i need to check out what map
does on every data structure I use and may find it behaves differently?
map
on any monadic structure always operates on the "success case", i.e. where the data you're interested in are.
In List
the "success case" is represented by its elements, on Try
by the Success
case, in Future
the same thing, in Option
by the Some
case.
List
is also little different than the others, since it doesn't really have a "failure case", like Try
, Future
or Option
do (although you could consider the Nil
list as such)
Generally speaking you can think in this terms: whenever you map
over something, you're applying a transformation on the data held by the container on its "success case"
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