Scala Zip Function
Scala Zip function
If you're a Java developer new to the Scala programming language and you have recently come across the zip function of the Iterable trait; odds are your scratching your head wondering what zip is.
In this simple tutorial we will demonstrate what zip is and how it is used on a List. Zip is a fairly old concept found in functional programming languages. Zip effectively takes two Iterable collections, for instance "List" and combines the two into one list of pairs. The pairs are derived from taking each element at the same position in both lists and combing them into one pair. To illustrate this let's take a look at a simple example.
// A simple list of peoples first names
val people = List("Abel", "Anthony", "Junior", "Blas")
// A simple list of lucky numbers
val numbers = List(5, 2, 7, 4)
// Here we zip the people and numbers lists and print out the resulting list.
println(people.zip(numbers))This code effectively constructs two lists. One list contains the names of people and the second list lucky numbers. On the last line we zip the people's list with the lucky numbers list and print out the resulting list of pairs. The output of this code is the following:
List((Abel,5), (Anthony,2), (Junior,7), (Blas,4))
As you can see in the output, each element at the same position of both lists is combined into a tuple and added to the resulting list in the same order they appear in the specified people and numbers list.
It's important to note, that if the specified lists to zip are not the same size, the resulting list will be the size of the shortest list. What this means is that zip only generates pairs for elements that are found in the same position available on both lists. For example, if the first list contains the elements ("Abel", "Anthony", "Junior", "Blas") and the second list contains the elements (5, 2, 7) then the resulting list would be: (("Abel",5), ("Anthony",2), ("Junior",7). As you can see the element Blas from the first list is not present in the resulting list. Scala has another clever function that deals with this issue, it's the zipAll function defined in the Iterable trait of the Scala collections API. The second part of this tutorial covers zipAll here. Let's jump into a simple example to further clarify.
// A simple list of peoples first names
val people = List("Abel", "Anthony", "Junior", "Blas")
// A list that contains less elements than the peoples list
val numbers = List(5, 2, 7)
// Here we zip the people and numbers lists and print out the resulting list.
println(people.zip(numbers))And the result of this code is:
List((Abel,5), (Anthony,2), (Junior,7))
As you can see the pair for the element "Blas" at position 4 is not present in the result.
And that basically sums up what the zip function found in Scala's collections is all about. Hopefully this simple tutorial has helped you demystify the zip function, if not feel free to drop me a comment with any questions you might have. The second part of this tutorial covers the zipAll function.