Home > Articles

This chapter is from the book

4.9 Zipping

One reason for using tuples is to bundle together values so that they can be processed together. This is commonly done with the zip method. For example, the code

val symbols = Array("<", "-", ">")
val counts = Array(2, 10, 2)
val pairs = symbols.zip(counts)

yields an array of pairs

Array(("<", 2), ("-", 10), (">", 2))

The pairs can then be processed together:

for (s, n) <- pairs do print(s * n) // Prints <<---------->>

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.