supernatural
supernatural

Reputation: 1187

What type of expression is this in scala?

Considering x,p,r are evaluated from the previous expressions , what is happening underneath , <- and then after =

val a = for{
 x <- y
 p = q (x)
 r <- s (p)
} yield (something(p.something, r.something))

Upvotes: 1

Views: 60

Answers (1)

cchantep
cchantep

Reputation: 9168

The <- is equivalent (syntactic sugar) to .flatMap call, while = is equivalent to val x = (and yield kind of final .map).

So the code is equivalent to:

val a = y.flatMap { x => // first <-
  val p = q (x)
  s(p).map { r => // 2nd <- + yield
    something(p.something, r.something)
  }
}

Upvotes: 5

Related Questions