Learning scala map by tackling it's conflicts - Is scala Collection.map inconsistent with scala Try.map

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.
scala> List(1,2,3).map(x => { println(s"i'm running on $x"); x })
i'm running on 1
i'm running on 2
i'm running on 3
res7: List[Int] = List(1, 2, 3)
you see it was running on 1 and on 2 and on 3 - on all items in the collection
However the scala documentation on Try.map says this:
Maps the given function to the value from this Success or returns this if this is a Failure.
so it runs only on success.
See the following example:
scala> Try(1).map(x => { println("i'm running on $x"); x * 2} )
i'm running on $x
res8: scala.util.Try[Int] = Success(2)
you see it was running on item 1, well we can provide only one item to Try. Now lets try it with a Failure should map run on it? if you read the documentation for collection it says: "Builds a new collection by applying a function to all elements of the list" is the Failure part of the "list"? (in our case a Try). Well...
scala> Try(1/0).map(x => { println("i'm running on an item which results in a Failure"); 2} )
res12: scala.util.Try[Int] = Failure(java.lang.ArithmeticException: / by zero)
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?
as you can see it did not run on it. The reason from Erik Mejer words:
Monads lead you through the happy path
so was there a difference from collection map to Try map after all collection map runs on all items as doc says and the Try map runs only on Success items as docs says. Well not really you should read
this:
Builds a new collection by applying a function to all elements of this list.
as actually this:
Builds a new collection by applying a function to success elements of this list, however all items in a plain list are considered to be success. And this is actually why it runs on all items in the list

Comments