Skip to content

Instantly share code, notes, and snippets.

@aubreylouw
Created April 29, 2013 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aubreylouw/5479588 to your computer and use it in GitHub Desktop.
Save aubreylouw/5479588 to your computer and use it in GitHub Desktop.
Scala unzip
/* 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