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))

If you want a good book on Scala, take a look at Programming Scala (I'm currently reading through this).

0 replies

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 *