Abel Perez

Mindplex Media

Perhaps the most important principle for the good algorithm designer is to refuse to be content

  • Edit
  • Delete
  • Tags
  • Autopost

spacer
Aho Hopcroft and Ullman

0 Responses

Tweet

I execute extremely accurate logic with my paint brush!

  • Edit
  • Delete
  • Tags
  • Autopost

spacer

You tell me who I am?

0 Responses

Tweet

Scala zipAll Function

  • Edit
  • Delete
  • Tags
  • Autopost

Scala zipAll Function

The Scala zipAll function effectively takes two lists and generates a new collection that consists of corresponding pairs of elements that are found at the same position of both lists. In other words, each pair in the new list is the result of taking each element at the same position from both lists. For example, if the value at position 0 from the first list is "Abel" and the value at position 0 from the second list is 100, then the resulting pair would be ("Abel", 100). This pair would then be the element at position 0 of the resulting list from the zipAll function.

The big difference between zip and zipAll is that unlike the zip function where the amount of elements contained in the resulting list is the same as the shortest list, zipAll compensates for the shortest list, by adding a default value as the element of the resulting pair for the shortest list. For example, if the first list contains the value "Abel" at position 3 and the second list only contains two elements, and the default value specified for y (the second list) is 100, then the pair at position 3 for the resulting list would be ("Abel", 100). This probably sounds very confusing so let’s jump right into an example to further illustrate.

// the first list
val xList = List("Abel", "Anthony", "Junior", "Blas")

// the second list
val yList = List(5, 4, 7)

// apply the zipAll function to both lists and display the results
println(xList.zipAll(yList, "John", 100))

The result of running this code is the following:

List((Abel,5), (Anthony,4), (Junior,7), (Blas,100))

As you can see the zipAll function added the specified value of y 100 to the element "Blas" from the first list, simply because the second list is one element short than the first list. If this would have been the zip function, the result would have been the following:

List((Abel,5), (Anthony,4), (Junior,7))

In the zip function variant, the pair for "Blas" would have been skipped altogether.

As you might expect, the same applies to the reverse scenario where the first list is shorter than the second list. The following demonstrates the expected behavior.

// the first list
val xList = List("Abel", "Anthony", "Junior")

// the second list
val yList = List(5, 4, 7, 2)

// apply the zipAll function to both lists and display the results
println(xList.zipAll(yList, "John", 100))

Here is the expected output of running the reverse scenario.

List((Abel,5), (Anthony,4), (Junior,7), (John,2))

Here we can see that the resulting list contains the default value of x "John" for the pair at position 4. The Scala zipAll function is extremely useful and flexible for zipping lists together. In the next part of this tutorial we demonstrate the third zipping function zipWithIndex.

0 Responses

Tweet

Scala Zip Function

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.