zoukankan      html  css  js  c++  java
  • scala编程第16章学习笔记(3)——List类的高阶方法

    列表间映射:map、flatMap和foreach

    1.xs map f 操作返回把函数f应用在xs的每个列表元素之后由此组成的新列表。如:

    scala> List(1, 2, 3) map (_ + 1)
    res0: List[Int] = List(2, 3, 4)
    
    scala> val words = List("the", "quick", "brown", "fox")
    words: List[String] = List(the, quick, brown, fox)
    
    scala> words map (_.length)
    res1: List[Int] = List(3, 5, 5, 3)
    
    scala> words map (_.toList.reverse.mkString)
    res2: List[String] = List(eht, kciuq, nworb, xof)

    2.flatMap操作符与map类似,不过它的右操作元是能够返回元素列表的函数。它对列表的每个元素调用该方法,然后连接所有方法的结果并返回。map与flatMap的差异举例说明如下:

    scala> words map (_.toList)
    res3: List[List[Char]] = List(List(t, h, e), List(q, u, i, c, k), List(b, r, o,
    w, n), List(f, o, x))
    
    scala> words flatMap (_.toList)
    res4: List[Char] = List(t, h, e, q, u, i, c, k, b, r, o, w, n, f, o, x)

    List.range是可以创建某范围内所有整数列表的工具方法。例如:

    scala> List.range(1, 5) flatMap (i => List.range(1, i) map (j => (i, j)))
    res6: List[(Int, Int)] = List((2,1), (3,1), (3,2), (4,1), (4,2), (4,3))

    3.foreach是第三种与映射类似的操作。它的右操作元是过程(返回Unit的函数)。它只是对每个列表元素都调用一遍过程。操作的结果仍然是Unit,不会产生结果列表。例如:

    scala> var sum = 0
    sum: Int = 0
    
    scala> List(1, 2, 3, 4, 5) foreach (sum += _)
    
    scala> sum
    res9: Int = 15

    列表过滤:filter、partition、find、takeWhile、dropWhile和span
    1.xs filter p操作产生xs中符合p(x)为true的所有元素组成的列表。如:

    scala> List (1, 2, 3, 4, 5) filter (_ % 2 == 0)
    res10: List[Int] = List(2, 4)
    
    scala> words filter (_.length == 3)
    res11: List[String] = List(the, fox)

    2.partition方法与filter类似,不过返回的是列表对。其中一个包含所有论断为真的元素,另一个包含所有论断为假的元素。
    xs partition p  等价于 (xs filter p, xs filter (!p()))

    举例如下:

    scala> List(1, 2, 3, 4, 5) partition (_ % 2 ==0)
    res12: (List[Int], List[Int]) = (List(2, 4),List(1, 3, 5))

    3.find方法同样与filter方法类似,不过返回的是第一个满足给定论断的元素,而并不是全部。xs find p 操作以列表xs和论断p为操作元。返回可选值。如果xs中存在元素x使得p(x)为真,Some(x)将返回。否则,若p对所有元素都不成立,None将返回。举例如下:

    scala> List(1, 2, 3, 4, 5) find (_ % 2 == 0)
    res13: Option[Int] = Some(2)
    
    scala> List(1, 2, 3, 4, 5) find (_  <= 0)
    res15: Option[Int] = None

    4. xs takeWhile p操作返回列表xs中最长的能够满足p的前缀。例如:

    scala> List(1, 2, 3, -4, 5) takeWhile (_ > 0)
    res16: List[Int] = List(1, 2, 3)

    5.xs dropWhile p操作移除最长能够满足p的前缀。举例如下:

    scala> words dropWhile (_ startsWith "t")
    res17: List[String] = List(quick, brown, fox)

    6.span方法把takeWhile和dropWhile组合成一个操作。它返回一对列表,定义与下列等式一致:
    xs span p 等价于 (xs takeWhile p, xs dropWhile p)

    scala> List(1, 2, 3, -4, 5) span (_ >0)
    res18: (List[Int], List[Int]) = (List(1, 2, 3),List(-4, 5))

     列表的论断:forall和exists

    1. xs forall p 如果列表的所有元素满足p则返回true。

    2. xs exists p 如果列表中有一个值满足p就返回true。

    例如,要找出以列表的列表形式表达的矩阵是否有全为零的行:

    scala> def hasZeroRow(m: List[List[Int]]) = m exists (row => row forall (_ == 0))
    hasZeroRow: (m: List[List[Int]])Boolean
    
    scala> val m= List(List(3,0,0), List(0,3,0), List(0,0,3))
    m: List[List[Int]] = List(List(3, 0, 0), List(0, 3, 0), List(0, 0, 3))
    
    scala> hasZeroRow(m)
    res21: Boolean = false

     折叠列表:/:和:

    scala> def sum(xs: List[Int]): Int = (0 /: xs) (_ + _)
    sum: (xs: List[Int])Int

    sum(List(a, b, c))  等价于 0 + a + b + c

    scala> def product(xs: List[Int]): Int = (1 /: xs) (_ * _)
    product: (xs: List[Int])Int

    product(List(a, b, c)) 等价于 1 * a * b * c
    左折叠操作“(z /: xs) (op)” 与三个对象有关:开始值z,列表xs,以及二元操作op。折叠的结果op应用到前缀值z及每个相邻元素上。

    :操作符被称为右折叠。与左折叠一样带有三个操作元,不过前两个的出现次序相反:第一个操作元是待折叠的列表,第二个是开始值。(xs : z) (op)

    列表排序:sortWith

    对列表xs的操作,xs sortWith before,可以对列表的元素执行排序,其中“before”是比较元素的方法。举例如下:

    scala> List(1, -3, 4, 2, 6) sortWith (_ < _)
    res2: List[Int] = List(-3, 1, 2, 4, 6)

    scala> words sortWith (_.length > _.length)

    res5: List[String] = List(quick, brown, the, fox)
  • 相关阅读:
    测试Hibernate的DAO方法
    MyBatis 一对一关系映射
    MyBatis 实现基本CRUD操作
    配置日志框架——Log4j
    MyBatis 基础配置
    Hibernate与 MyBatis的比较
    关于用JSON拼凑出来的DOM对象的操作以及EasyUI的提交方式
    Struts2 单例与多例
    SpringMVC rest风格的url
    SpringMVC 控制器之对ServletAPI的支持与对JSON的支持
  • 原文地址:https://www.cnblogs.com/gaopeng527/p/4102937.html
Copyright © 2011-2022 走看看