Skip to content

Instantly share code, notes, and snippets.

@Korilakkuma
Created May 20, 2015 07:18
Show Gist options
  • Save Korilakkuma/0f3ec03d1be30c2743eb to your computer and use it in GitHub Desktop.
Save Korilakkuma/0f3ec03d1be30c2743eb to your computer and use it in GitHub Desktop.
Scala reverse List by foldLeft
object Reverser {
def reverse[T](list: List[T]): List[T] = list.foldLeft(List[T]())((x, y) => y :: x)
def main(args: Array[String]): Unit = {
println(reverse(List(1, 2, 3, 4, 5)))
println(reverse(List("a", "b", "c", "d")))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment