3 ms·
val plusOne = for(x <- List(1,2,3)) yield x+1 desugars to: List(1,2,3).map(_+1) // or longhand, map(x=> x+1) and: for(x <- List(1,2,3); y <- List(4,
by virtualwhys 13y ago
val plusOne = for(x <- List(1,2,3)) yield x+1
desugars to:
List(1,2,3).map(_+1) // or longhand, map(x=> x+1)
and:
for(x <- List(1,2,3); y <- List(4,5,6)) yield x*y
desugars to:
List(1,2,3).flatMap(x=> List(4,5,6).map(_*x))
So, composable futures means you can chain a bunch of futures together in a for comprehension and yield the successful or failing result without blocking.
The essential point is that if you have some Thing that implements map and flatMap, it can be chained through a for comprehension.
Only just dipping my toes in Haskell, but I must say that Scala is a truly wonderful language, one that makes my daily dev a whole lot of fun.
- deckiedan 13y agoWould this be equivalent to: plusOne = (x+1 for x in (1,2,3)) And then the second example: mult = (x*y for x,y in zip((1,2,3), (4,5,6))) In python?
- virtualwhys 13y agoyup, not sure what Python does with your examples under the hood, but they're equivalent, and, I must say, nicely concise ;-) In Scala I guess we could go zip-style and do: List(1,2,3) zip List(4,5,6) map{case(x,y)=> x*y} but I was trying to demo for comprehensions and their relation to map/flatMap for the OP.