Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is scala Collection.map versus Try.map not consistent?

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?

like image 240
Jas Avatar asked Mar 19 '23 14:03

Jas


1 Answers

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"

like image 163
Gabriele Petronella Avatar answered Mar 21 '23 20:03

Gabriele Petronella