Scala中Zip相关的函数
在Scala中存在好几个Zip相关的函数,比如zip,zipAll,zipped 以及zipWithIndex等等。我们在代码中也经常看到这样的函数,这篇文章主要介绍一下这些函数的区别以及使用。
1、zip函数将传进来的两个参数中相应位置上的元素组成一个pair数组。如果其中一个参数元素比较长,那么多余的参数会被删掉。看下英文介绍吧:
Returns a list formed from this list and another iterable collection by combining corresponding elements in pairs. If one of the two collections is longer than the other, its remaining elements are 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), (1,1), (2,1), (3,2), (4,3), (5,5))
2、zipAll 函数和上面的zip函数类似,但是如果其中一个元素个数比较少,那么将用默认的元素填充。
The zipAll method generates an iterable of pairs of corresponding elements from xs and ys, where the shorter sequence is extended to match the longer one by appending elements x or y
/**
* User: 过往记忆
* Date: 14-12-17
* Time: 上午10:16
* bolg: http://www.iteblog.com
* 本文地址:http://www.iteblog.com/archives/1225
* 过往记忆博客,专注于hadoop、hive、spark、shark、flume的技术博客,大量的干货
* 过往记忆博客微信公共帐号: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函数,这个不好翻译,自己看英文解释吧
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、zipWithIndex函数将元素和其所在的下表组成一个pair。
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), (1,1), (1,2), (2,3), (3,4), (5,5), (8,6), (13,7))
5、unzip函数可以将一个元组的列表转变成一个列表的元组
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), (1,1), (1,2), (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, 13),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)
Scala中Zip相关的函数的更多相关文章
- scala中的高阶函数
版权申明:转载请注明出处. 文章来源:http://bigdataer.net/?p=332 排版乱?请移步原文获得更好阅读体验 1.scala中的函数 scala是一门面向对象和函数式编程相结合的语 ...
- Scala 中 构造函数,重载函数的执行顺序
在调试scala在线开发教程(http://www.imobilebbs.com/wordpress/archives/4911)的过程中看到了以下代码,但是这段代码无论怎么调试都无法成功. abst ...
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
今天来了解下scala的泛型 先让我们看下这段代码 class Triple[F,S,T](val first:F,val second: S,val third: T) val triple = n ...
- Scala中 zip或者zipWithIndex的用法
问题:你要遍历一个有序集合,同时你又想访问一个循环计数器,但最重要的是你真的不需要手动创建这个计数器.解决方案: 使用zipWithIndex或者zip方法来自动地创建一个计数器,假设你有一个有 ...
- 2018-02-17 中文代码示例[译]Scala中创建隐式函数
前言: 学习Scala时, 顺便翻译一下自己有兴趣的文章. 代码中所有命名都中文化了(不是翻译). 比如原文用的是甜甜圈的例子. 原文: Scala Tutorial - Learn How To C ...
- Scala中柯里化函数
高阶函数转一阶函数: val add1 = (x: Int) => x + 5 def add2(x: Int)(y: Int) = x + y //传入一个参数转换为一阶函数 def add3 ...
- (数据科学学习手札48)Scala中的函数式编程
一.简介 Scala作为一门函数式编程与面向对象完美结合的语言,函数式编程部分也有其独到之处,本文就将针对Scala中关于函数式编程的一些常用基本内容进行介绍: 二.在Scala中定义函数 2.1 定 ...
- (转)scala中map与flatMap浅析
在函数式语言中,函数作为一等公民,可以在任何地方定义,在函数内或函数外,可以作为函数的参数和返回值,可以对函数进行组合.由于命令式编程语言也可以通过类似函数指针的方式来实现高阶函数,函数式的最主要的好 ...
- Scala中的构造器和高阶函数
构造器 在定义类时可以定义主构造器.主构造器可以同时声明字段. /** * 主构造器 * @author Administrator */ //在scala中,类和方法交织在一起 class Test ...
随机推荐
- Telegram
官网:https://www.telegram.org/ GitHub:https://github.com/telegramdesktop/tdesktop
- ajax:post 400错误
POST http://localhost:8080/purchase/purchase-apply/update.htm 400 (Bad Request) n.ajaxTransport.k.co ...
- Java for LeetCode 061 Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- java equals 和 "==" 比较
java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(== ...
- hdu 5284 BestCoder Round #48 ($) 1001 水题 *
题意:看一个字符串中是否包含顺序的 w y h ,字符之间可以有其他字符,并且如果有多个连续的vv,则可以看做一个w 比较水,直接看代码 #include<cstdio> #incl ...
- 学会使用Chromium中的LOG
转自:http://blog.csdn.net/kuerjinjin/article/details/43937345 简介 众所周知chromium项目无比巨大,想去快速的了解,调试并添加自己想要的 ...
- 安卓通过putExtra传递数据的几种方式
通过intent传递数据时,使用以下代码报错: hMap<string, object=""> map=(Map<string, object="&qu ...
- Xamarin Anroid开发教程之验证环境配置是否正确
Xamarin Anroid开发教程之验证环境配置是否正确 经过前面几节的内容已经把所有的编程环境设置完成了,但是如何才能确定所有的一切都处理争取并且没有任何错误呢?这就需要使用相应的实例来验证,本节 ...
- AppleWatch开发教程之调试程序使用帮助文档
AppleWatch开发教程之调试程序使用帮助文档 AppleWatch开发教程之调试程序 调试又被称为排错,是发现和减少程序错误的一个过程.在Xcode中进行调试的需要实现以下几个步骤: 1.添加断 ...