Scala - can yield be used multiple times with a for loop?

前端 未结 6 2192
-上瘾入骨i
-上瘾入骨i 2020-12-10 11:56

An example:

val l = List(1,2,3)
val t = List(-1,-2,-3)

Can I do something like this?

for (i <- 0 to 10) yield (l(i)) yie         


        
6条回答
  •  轮回少年
    2020-12-10 12:28

    Apparently not. I get a compile error when I try it.

    It looks like for .. yield is an expression. You can't have two yields, since that's not really part of the expression.

    If you want to yield multiple values, why not yield them as a tuple or a list?

    For example:

    for( t <- List(1,2,3); l <- List(-1,-2,-3))
      yield (t, l)
    

提交回复
热议问题