Created
July 20, 2019 17:58
-
-
Save suicide/b0e5d5a02baa909fb31d15df5571639e to your computer and use it in GitHub Desktop.
a zipper-like merger function with tailrec
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
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