Skip to content

Instantly share code, notes, and snippets.

@kjunine
Created May 18, 2013 07:23
Show Gist options
  • Select an option

  • Save kjunine/5603559 to your computer and use it in GitHub Desktop.

Select an option

Save kjunine/5603559 to your computer and use it in GitHub Desktop.
Tests foldLeft and foldRight of List in Scala.
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