Created
May 18, 2013 07:23
-
-
Save kjunine/5603559 to your computer and use it in GitHub Desktop.
Tests foldLeft and foldRight of List in Scala.
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
| package streams | |
| object tests { | |
| def xtoy(x: Int, y: List[Int]): List[Int] = { | |
| println(x + " @ " + y) | |
| x :: y | |
| } | |
| def ytox(x: List[Int], y: Int): List[Int] = { | |
| println(y + " @ " + x) | |
| y :: x | |
| } | |
| val l = List.range(1, 11) | |
| val r = List(100, 200, 300) | |
| // y = r, x = l(n) from right to left | |
| l.foldRight(r)(xtoy) | |
| // x = r, y = l(n) from left to right | |
| l.foldLeft(r)(ytox) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment