Skip to content

Instantly share code, notes, and snippets.

@suicide
Created July 20, 2019 17:58
Show Gist options
  • Save suicide/b0e5d5a02baa909fb31d15df5571639e to your computer and use it in GitHub Desktop.
Save suicide/b0e5d5a02baa909fb31d15df5571639e to your computer and use it in GitHub Desktop.
a zipper-like merger function with tailrec
object Merger {
def merge[T](a: List[T], b: List[T]): List[T] = {
@scala.annotation.tailrec
def innerMerge(result: List[T], a: List[T], b: List[T]): List[T] = {
(a, b) match {
case (Nil, x) => result ++ x
case (x, Nil) => result ++ x
case (aHead :: aTail, bHead :: bTail) => innerMerge(List(aHead, bHead) , aTail, bTail)
}
}
innerMerge(Nil, a, b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment