Scala zipAll Example

The zip function combines two lists into tuples. If the lists are of differing lengths, the shorter length is used. If you don’t like this behavior, the zipAll function will keep all elements, filling in specified values for the blanks (compare this to the recycling rule in R, which lets you continuously cycle through the shorter list).

val a = List("a", "b", "c", "d")
val b = List(1, 2, 3);
 
println(a.zipAll(b, "for missing values", 100))

And here is the output for each:

List((a,1), (b,2), (c,3))
List((a,1), (b,2), (c,3), (d,100))

Interested in Scala? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

1 reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *