Zip-related functions in Scala

Source: Internet
Author: User

There are several zip-related functions in Scala, such as zip,zipall,zipped, Zipwithindex, and so on. We often see such functions in our code, and this article mainly introduces the differences and uses of these functions.
1, the ZIP function will pass in two parameters in the corresponding position of the elements to form a pair array. If one of the parameter elements is longer, then the extra parameters are deleted. Look at the English introduction:
Returns a list formed from the This list and another iterable collection by combining corresponding elements in pairs. If one of the collections is longer than the other, it remaining elements is ignored.

scala> val numbers = Seq (0, 1, 2, 3, 4)
Numbers:seq[int] = List (0, 1, 2, 3, 4)

Scala> val Series = Seq (0, 1, 1, 2, 3)
Series:seq[int] = List (0, 1, 1, 2, 3)

Scala> Numbers Zip series
res24:seq[(int, int)] = List ((0,0), ((), (2,1), (3,2), (4,3), (5,5))


2. The Zipall function is similar to the zip function above, but if one of the elements is relatively small, the default element is populated.

The Zipall method generates an iterable of pairs of corresponding elements from Xs and Ys, where the shorter sequence is E Xtended to match the longer one by appending elements x or Y

/**
* User: Past memory
* DATE:14-12-17
* Time: 10:16
* bolg:http://www.iteblog.com
* This address: http://www.iteblog.com/archives/1225
* Past Memory Blog, focus on Hadoop, Hive, Spark, shark, Flume technology blog, a lot of dry
* Past Memory Blog public account: Iteblog_hadoop
*/
scala> val xs = List (1, 2, 3)
Xs:list[int] = List (1, 2, 3)

scala> val ys = List (' A ', ' B ')
Ys:list[char] = List (A, B)

scala> val Zs = List ("I", "II", "III", "IV")
Zs:list1 = List (I, II, III, IV)

scala> val x = 0
X:int = 0

scala> val y = ' _ '
Y:char = _

Scala> val z = "_"
Z:java.lang.string = _

Scala> Xs.zipall (ys, X, y)
res30:list[(Int, Char)] = List ((1,a), (2,b), (3,_))

Scala> Xs.zipall (ZS, X, z)
res31:list[(Int, java.lang.String)] = List ((1,i), (2,ii), (3,III), (0,IV))

3, zipped function, this is not good translation, you see the English explanation it
The zipped method on tuples generalizes several common operations to work on multiple lists.

Scala> val values = List.range (1, 5)
Values:list[int] = List (1, 2, 3, 4)

Scala> (values, values). Zipped Tomap
Res34:scala.collection.immutable.map[int,int] = Map (1, 1, 2, 2, 3, 3, 4, 4)

scala> val sumofsquares = (values, values). Zipped Map (_ * _) Sum
Sumofsquares:int = 30

4. The Zipwithindex function consists of a pair with the element and the following table in which it resides.
The Zipwithindex method pairs every element of a list with the position where it appears in the list.

Scala> val Series = Seq (0, 1, 1, 2, 3, 5, 8, 13)
Series:seq[int] = List (0, 1, 1, 2, 3, 5, 8, 13)

Scala> Series.zipwithindex
res35:seq[(int, int)] = List ((0,0), (All), (up), (2,3), (3,4), (5,5), (8,6), (13,7))

5. The unzip function can transform a list of tuples into a tuple of a list
The Unzip method changes back a list of tuples to a tuple of lists.

scala> val seriesin = Seq (0, 1, 1, 2, 3, 5, 8, 13)
Seriesin:seq[int] = List (0, 1, 1, 2, 3, 5, 8, 13)

scala> val Fibonacci = Seriesin.zipwithindex
fibonacci:seq[(int, int)] = List ((0,0), (All), (up), (2,3), (3,4), (5,5), (8,6), (13,7))

Scala> Fibonacci.unzip
RES46: (Seq[int], seq[int]) = (List (0, 1, 1, 2, 3, 5, 8,), list (0, 1, 2, 3, 4, 5, 6, 7))

scala> val seriesout = Fibonacci.unzip _1
Seriesout:seq[int] = List (0, 1, 1, 2, 3, 5, 8, 13)

scala> val numbersout = Fibonacci.unzip _2
Numbersout:seq[int] = List (0, 1, 2, 3, 4, 5, 6, 7)

Zip-related functions in Scala

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.