I need to implement a "zipLongest" function in Scala; that is, combine two sequences together as pairs, and if one is longer than the other, use a default value. (Unlike the standard zip method, which will just truncate to the shortest sequence.)
I've implemented it directly as follows:
def zipLongest[T](xs: Seq[T], ys: Seq[T], default: T): Seq[(T, T)] = (xs, ys) match { case (Seq(), Seq()) => Seq() case (Seq(), y +: rest) => (default, y) +: zipLongest(Seq(), rest, default) case (x +: rest, Seq()) => (x, default) +: zipLongest(rest, Seq(), default) case (x +: restX, y +: restY) => (x, y) +: zipLongest(restX, restY, default) } Is there a better way to do it?