val add1: Int => Int = x => x + 1
val sq = (x: Int) => x * x
val add100 = (x: Int) => x + 100
foo map (add100 compose sq compose add1)
//res9: List[Int] = List(104, 109, 116, 125, 136)
//functionをリストにしたら、自由度高いじゃと思って!下記の方法を見つかった!
// https://bcomposes.wordpress.com/2011/08/20/fun-with-function-composition-in-scala/
val fncs = List(add1, sq, add100)
// fncs: List[Int => Int] = List(<function1>, <function1>, <function1>)
val foo: List[Int] = List(1, 2, 3, 4, 5)
foo map (fncs.reverse reduce( _ compose _ ))
//res10: List[Int] = List(104, 109, 116, 125, 136)
//とりあえずこれか!うん!
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme