memo
[code lang=text] scala> for { | s <- Array("a","b","c") | } | println(s) a b c [/code]
[code lang=text] scala> for { | i <- 1 to 5 | j <- 1 to 2 | } | println(i * j) 1 2 2 4 3 6 4 8 5 10 [/code]
if入れる系
[code lang=text] scala> for { | i <- 0 to 10 | if i % 2 == 0 | } | println(i) 0 2 4 6 8 10 [/code]
yield = 値を返すfor
[code lang=text] scala> for { | i <- 1 to 5 | } yield i res8: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5)
scala> def test = for { | i <- 0 to 10 | if i % 2 == 0 | } yield i test: scala.collection.immutable.IndexedSeq[Int]
scala> test res9: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10) [/code]
yieldに式も書ける
[code lang=text] scala> for { | i <- 1 to 5 | } yield i * 2 res12: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10) [/code]