在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相关的函数的更多相关文章

  1. scala中的高阶函数

    版权申明:转载请注明出处. 文章来源:http://bigdataer.net/?p=332 排版乱?请移步原文获得更好阅读体验 1.scala中的函数 scala是一门面向对象和函数式编程相结合的语 ...

  2. Scala 中 构造函数,重载函数的执行顺序

    在调试scala在线开发教程(http://www.imobilebbs.com/wordpress/archives/4911)的过程中看到了以下代码,但是这段代码无论怎么调试都无法成功. abst ...

  3. 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用

    今天来了解下scala的泛型 先让我们看下这段代码 class Triple[F,S,T](val first:F,val second: S,val third: T) val triple = n ...

  4. Scala中 zip或者zipWithIndex的用法

    问题:你要遍历一个有序集合,同时你又想访问一个循环计数器,但最重要的是你真的不需要手动创建这个计数器.解决方案:    使用zipWithIndex或者zip方法来自动地创建一个计数器,假设你有一个有 ...

  5. 2018-02-17 中文代码示例[译]Scala中创建隐式函数

    前言: 学习Scala时, 顺便翻译一下自己有兴趣的文章. 代码中所有命名都中文化了(不是翻译). 比如原文用的是甜甜圈的例子. 原文: Scala Tutorial - Learn How To C ...

  6. Scala中柯里化函数

    高阶函数转一阶函数: val add1 = (x: Int) => x + 5 def add2(x: Int)(y: Int) = x + y //传入一个参数转换为一阶函数 def add3 ...

  7. (数据科学学习手札48)Scala中的函数式编程

    一.简介 Scala作为一门函数式编程与面向对象完美结合的语言,函数式编程部分也有其独到之处,本文就将针对Scala中关于函数式编程的一些常用基本内容进行介绍: 二.在Scala中定义函数 2.1 定 ...

  8. (转)scala中map与flatMap浅析

    在函数式语言中,函数作为一等公民,可以在任何地方定义,在函数内或函数外,可以作为函数的参数和返回值,可以对函数进行组合.由于命令式编程语言也可以通过类似函数指针的方式来实现高阶函数,函数式的最主要的好 ...

  9. Scala中的构造器和高阶函数

    构造器 在定义类时可以定义主构造器.主构造器可以同时声明字段. /** * 主构造器 * @author Administrator */ //在scala中,类和方法交织在一起 class Test ...

随机推荐

  1. c++11之bind

    std::bind是个c++推出的新的特性,非常有用,让你写起来率试不爽. #include <iostream> using namespace std; #include <fu ...

  2. Windows 8操作技巧之快捷键大全

    Windows 8操作系统发布之后,因为其新颖的界面和对触屏友好的设计,使许多长期使用Windows系统的用户,也觉得一时难以适应,一些操作方式也不知道如何去实现.在Windows系统中,快捷键无疑是 ...

  3. springJDBC实现查询方法二

    无废话,看代码: @Override public List<Sites> queryAllSites(Pager pager) { String sql = "select * ...

  4. javaScript中利用ActiveXObject来创建FileSystemObject操作文件

    注:如果用javascript读本地文件,遇到安全问题. 需在浏览器中进行设置,如下:     工具—> Internet选项->安全->自定义级别->启用“没有标识为安全的A ...

  5. ASP.NET Web API 2 中的属性路由使用(转载)

    转载地址:ASP.NET Web API 2 中的属性路由使用

  6. 项目如何脱离TFS 2010的管理

    在VS 里,文件->源代码管理->更改源代码管理->取消绑定.

  7. Java Hour 49 保存和查询历史的Weather

    吾一直坚信,是需求的不断变化推动了架构的不断演变. 新的需求 能够查看指定日期的Weather,因为客户想要比较昨天和今天的天气情况,所以需要能够查询历史数据的功能. 1 能保存当前的天气到数据库 2 ...

  8. google地图引入网页

    <script src=);//指定坐标,缩放级别,setcenter 设置地图中心位置             //var mapControl = new GMapTypeControl() ...

  9. Android---让你的APK程序开机自动运行(转)

    转自: http://blog.sina.com.cn/s/blog_72f6e45701014l6t.html 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service. ...

  10. matlab练习程序(多圆交点)

    最近总是对计算几何方面的程序比较感兴趣. 多圆求交点,要先对圆两两求交点. 有交点的圆分为相切圆和相交圆. 相切圆求法: 1.根据两圆心求直线 2.求公共弦直线方程 3.求两直线交点即两圆切点. 相交 ...