Created
May 20, 2015 07:18
-
-
Save Korilakkuma/0f3ec03d1be30c2743eb to your computer and use it in GitHub Desktop.
Scala reverse List by foldLeft
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
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