Friday, November 24, 2017

For comprehensions - Syntactic sugar for flatMap & map


Given the following definitions...


def getX = Option(List(3))

def getY = Option(List(5))


The below 2 computations are the same:


getX.flatMap(x => getY.map(y => x ++ y))



val z = for {
       x <- getX
       y <- getY

     } yield x ++ y


Value of z:



z: Option[List[Int]] = Some(List(3, 5))


No comments: