Scala List zip

I just realized we forgot to talk about zip.

Today should be an easy one.

Don’t forget, you can use Range to create List more easily.

Easy ?

zip allows you to combine two list into one. The syntax is

List[A].zip(List[B]) => List[(A, B)]

It will return a list with both type as a tuple

I also introduce a little new function: swap. It is a function available for Tuple[A, B]. It allows to flip a tuple(a, b) into a tuple(b, a).

Extra exercise:

What happen if you zip several times in a row ?

Try out:

And then try to combine it all with .map{ case ??? => }

What will be the case pattern here ?

Reveal more information and clues
Load Exercise