Created
April 29, 2013 03:42
-
-
Save aubreylouw/5479588 to your computer and use it in GitHub Desktop.
Scala unzip
This file contains 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
/* unzip decomposes a list of pairs into a pair of lists. | |
* The recursive case illustrates pattern-matching the result of the | |
* recursive call in order to apply an operation to the elements. | |
*/ | |
def unzip [X,Y] (ps:List[(X,Y)]) : (List[X], List[Y]) = ps match { | |
case Nil => (Nil, Nil) | |
case (x, y) :: qs => { | |
val (xs, ys) = unzip (qs) | |
(x :: xs, y :: ys) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment