Created
April 28, 2016 08:39
-
-
Save songfei1983/36a9414b7411fa1e5a9ac8b80d0ecb40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
//とりあえずこれか!うん! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment