Scala Trait Traversable 第4部分

Scala Trait Traversable 第4部分

操作如下。

折叠:
这里的操作是 reduceRight, reduceLeft, /:或 foldLeft, :/或 foldRight 。这些方法对Traversable集合的连续元素进行二进制操作。

示例 :

// Scala program for operations
// of Folds
  
// Creating object 
object Folds
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(8, 6, 4, 2)
  
        // Applying method of
        // Folds
        // val y = x.foldLeft(0)(_ - _) or
        val y = (0 /: x)(_ - _)
  
        // Evaluates the Expression 
        // given below and displays it
        // ((((0 - 8) - 6) - 4) - 2) 
        println(y)
  
    }
}

输出:

-20

这里,foldLeft(0)是所需的操作,其中0是 “初始值”。
这个方法在给定的Traversable集合的连续元素之间应用二进制操作,从左到右移动,从初始值开始。

示例 :

// Scala program for operations
// of Folds
  
// Creating object 
object Folds
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(8, 6, 4, 2)
  
        // Applying method of
        // Folds
        // val y = x.foldRight(0)(_ - _) or
        val y = (x :\ 0)(_ - _)
  
        // Evaluates the Expression 
        // given below and displays it
        // (8 - (6 - (4 - (2 - 0))))
        println(y)
    }
}

输出:

4

这里,foldRight将在集合的连续元素之间进行二进制操作,从左到右移动,以初始值结束。

示例 :

// Scala program for operations
// of Folds
  
// Creating object 
object Folds
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(8, 6, 4, 2)
  
        // Applying method of folds
        val y = x.reduceLeft(_ - _)
  
        // Evaluates the Expression 
        // given below and displays it
        // (((8 - 6) - 4) -2)
        println(y)
    }
}

输出:

-4

在这里,reduceLeft像foldLeft一样在集合的连续元素之间进行二进制操作,但考虑到给定集合的第一个元素作为初始值。

示例 :

// Scala program for operations
// of Folds
  
// Creating object 
object Folds
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(8, 6, 4, 2)
  
        // Applying method of folds
        val y = x.reduceRight(_ - _)
  
        // Evaluates the Expression 
        // given below and displays it
        // (8 - (6 - (4 - 2)))
        println(y)
    }
}

输出:

4

在这里,reduceRight将执行类似于foldRight的二进制操作,但将集合的最后一个元素作为初始值。

具体的折叠:
这里的操作是min, max, product, 和sum。这些操作对不同类型的Traversable集合进行操作。

示例 :

// Scala program for operations
// of Specific folds
  
// Creating object 
object Specificfold
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(3, 4, 7, 10)
  
        // Applying method of 
        // Specific folds
        val y = x.sum
  
        // Displays sum of all the
        // elements in the list 
        println(y)
    }
}

输出:

24

这里,sum将返回所给集合中所有元素的总和。

示例 :

// Scala program for operations
// of Specific folds
  
// Creating object 
object Specificfold
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(2, 5, 10, 12)
  
        //Applying method of 
        //Specific folds
        val y = x.product
  
        //Displays product of all 
        //the elements in the list 
        println(y)
    }
}

输出:

1200

这里,product将返回集合中所有元素的乘积。

示例 :

// Scala program for operations
// of Specific folds
  
// Creating object 
object Specificfold
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(21, 15, 27, 22)
  
        // Applying method of 
        // Specific folds
        val y = x.max
  
        // Displays largest element
        // of the list
        println(y)
    }
}

输出:

27

这里,max将返回集合中所有元素中最大的元素。

示例 :

// Scala program for operations
// of Specific folds
  
// Creating object 
object Specificfold
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(21, 15, 27, 22)
  
        // Applying method of 
        // Specific folds
        val y = x.min
  
        // Displays smallest element
        // of the list
        println(y)
    }
}

输出:

15

这里,min将返回给定集合中所有元素中最小的元素。

字符串操作:
这里的操作有addString, mkString, stringPrefix . 这些操作被用来提供另一种方法,将给定的Traversables集合转换为一个字符串。

示例 :

// Scala program for operations
// of Strings
  
// Creating object 
object Strings
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(7, 8, 9, 10, 11)
        val v = List(21, 19, 17, 15)
  
        // Applying Strings operations
        val y = x.mkString(" < ")
        val w = v.mkString(" > ")
  
        // Displays all the elements
        // separated by (<)
        println(y)
        println("\n")
  
        // Displays all the elements
        // separated by (>)
        println(w)
    }
}

输出:

7 < 8 < 9 < 10 < 11
21 > 19 > 17 > 15

这里,mkString(“<“)是期望的操作,”<“是分隔符。
这个操作被用来返回由给定的分隔符分隔的元素集合。

示例 :

// Scala program for operations
// of Strings
  
// Creating object 
object Strings
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(7, 8, 9, 10, 11)
  
        // Creating StringBuilder
        val B = new StringBuilder("The numbers are: ")
      
        // Applying Strings operation
        val y = x.addString(B, ", ")
  
        // Displays all the elements
        // of the list in the String 
        // builder separated by (,)
        println(y)
    }
}

输出:

The numbers are: 7, 8, 9, 10, 11

这里,addString(B, “, “)是期望的操作,其中,”B “是一个字符串生成器,”, “是一个分隔符。
该操作添加StringBuilder中由所述分隔符分隔的元素的给定集合。

示例 :

// Scala program for operations
// of Strings
  
// Creating object 
object Strings
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating a Set of numbers 
        val x = Set(7, 8, 9, 10, 11)
      
        // Applying Strings operation
        val y = x.stringPrefix
  
        // Displays the name of the 
        // collection used
        println(y)
    }
}

输出:

Set

这里,stringPrefix将返回所使用的集合的名称。

  • 复制操作。
    两个复制操作是copyToArray和copyToBuffer。这些操作用于将可遍历集合的元素复制到一个数组或一个缓冲区。
  • 视图操作。
    这里的操作有view, view(from, to)。这些操作被用来在给定的可遍历集合上产生一个视图。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程