Functions as Parameters
はてなにScalaのシンタックスハイライト欲しいなあ。
Javaで代用は寂しい。
scala> val triple = (x: Int) => 3 *x triple: (Int) => Int = <function> scala> 1 to 10 map(triple) res0: RandomAccessSeq.Projection[Int] = RangeM(3, 6, 9, 12, 15, 18, 21, 24, 27, 30) scala> def map(lst:List[Int], fun: (Int) => Int) : List[Int] = | if (lst.isEmpty) | Nil else | fun(lst.head) :: map(lst.tail,fun) map: (List[Int],(Int) => Int)List[Int] scala> map(List(1,2,3),(x :Int) => 3 * x) res1: List[Int] = List(3, 6, 9)