Scalaメモ20100727
忘れっぽいからめもっとく
forについて
//foreachで以下のように書いた場合とほぼ同じ scala> for( c <-"string" ) | println(c) s t r i n g scala> "string".foreach(println) s t r i n g scala> val stringq = for( c <- "string"){ c } stringq: Unit = () scala> val stringq = "string".foreach{c => c} stringq: Unit = ()
//mapと scala> for( i <- 1 to 10) | yield i * i res22: RandomAccessSeq.Projection[Int] = RangeM(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) scala> val numbers = for( i <- 1 to 10) | yield i*i numbers: RandomAccessSeq.Projection[Int] = RangeM(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) scala> (1 to 10).map{ i => i * i } res23: RandomAccessSeq.Projection[Int] = RangeM(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) scala> val numbers = (1 to 10).map{ i => i * i } numbers: RandomAccessSeq.Projection[Int] = RangeM(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)