LoginSignup
1
1

More than 5 years have passed since last update.

関数合成

Posted at
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)
//とりあえずこれか!うん!
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1