Created
June 29, 2014 11:07
-
-
Save freewind/d9a210aeb9861eb204d8 to your computer and use it in GitHub Desktop.
use `andThen` to compose functions
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
| import scala.io.Source.stdin | |
| object App { | |
| def main(args: Array[String]) { | |
| stdin.getLines foreach { line => | |
| println(myconvert(line.toInt)) | |
| } | |
| } | |
| val double = (number:Int) => 2*number | |
| val add10 = (number:Int) => 10 + number | |
| val negative = (number:Int) => -1 * number | |
| val mod0 = negative andThen double andThen add10 | |
| val mod1 = add10 andThen negative andThen double | |
| val mod2 = add10 andThen double andThen negative | |
| def myconvert(n:Int) = { | |
| n%3 match { | |
| case 0 => mod0(n) | |
| case 1 => mod1(n) | |
| case 2 => mod2(n) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment