-3

I'm pretty new to Scala and came up with the following construction:

val value=
  for {
    p1 <- getList()
    p2 <- parser.parse(p1)  //parser.parse(String) Returns some useful value
  } yield p2
value.asJava

Where

def getList(): List[String] = {
   //compiled code
}

I don't quite understand what's going on in the first piece of code. Searching for scala left arrow operator did't shed the light on this. Can't you explain it?

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

2
for {
    p1 <- getList()
    p2 <- parser.parse(p1)
  } yield p2

is equivalent to (psudocode, not tested):

var result: List = Nil
val value = {
    foreach(p1 in getList()){
     foreach(p2 in parser.parse(p1)){
      result ::: p2
     }
    }
    result
   }

But like others said, you could have easily found this by reading up on Scala's for comprehension.

airudah
  • 1,169
  • 12
  • 19