斯卡拉 |缩小、折叠或扫描
在本教程中,我们将学习 Scala 中的 Reduce、Fold 和 Scan 函数。
- Reduce : Reduce函数应用于 scala 中包含列表、集合、映射、序列和元组的集合数据结构。 reduce函数中的参数是一个二元运算,它合并集合中的所有元素并返回一个值。前两个值与二元运算相结合,该运算的结果与集合的下一个值相结合,最后我们得到一个值。
此代码使用 reduce函数实现序列中元素的总和。
例子 :// Scala program sum of elements // using reduce function // Creating object object geeks { // Main method def main(arg:Array[String]) { // initialize a sequence of elements val seq_elements: Seq[Double] = Seq(3.5, 5.0, 1.5) println(s"Elements = $seq_elements") // find the sum of the elements // using reduce function val sum: Double = seq_elements.reduce((a, b) => a + b) println(s"Sum of elements = $sum") } }
输出:
Elements = List(3.5, 5.0, 1.5) Sum of elements = 10.0
此代码使用reduce函数查找序列中的最大和最小元素
例子 :// Scala program to find maximum and minimum // using reduce function // Creating object object geeks { // Main method def main(arg:Array[String]) { // initialize a sequence of elements val seq_elements : Seq[Double] = Seq(3.5, 5.0, 1.5) println(s"Elements = $seq_elements") // find the maximum element using reduce function val maximum : Double = seq_elements.reduce(_ max _) println(s"Maximum element = $maximum") // find the minimum element using reduce function val minimum : Double = seq_elements.reduce(_ min _) println(s"Minimum element = $minimum") } }
输出:
Elements = List(3.5, 5.0, 1.5) Maximum element = 5.0 Minimum element = 1.5
- Fold :像 reduce 一样, fold 也采用二元运算,合并集合中的所有元素并返回单个值。不同之处在于 fold 允许我们定义一个初始值。由于这个属性, fold 也可以管理空集合。如果集合为空,则初始化的值成为最终答案。因此,我们还可以使用其他数据类型的初始值从集合集中返回不同的值。 Reduce 只能返回相同类型的值,因为它的初始值是集合中的第一个值。
此代码使用折叠函数实现序列中元素的总和。这里的初始值取为 0.0,因为序列的数据类型为 Double。
例子 :// Scala program sum of elements // using fold function // Creating object object geeks { // Main method def main(arg:Array[String]) { // initialize a sequence of elements val seq_elements: Seq[Double] = Seq(3.5, 5.0, 1.5) println(s"Elements = $seq_elements") // find the sum of the elements using fold function val sum: Double = seq_elements.fold(0.0)((a, b) => a + b) println(s"Sum of elements = $sum") } }
输出:
Elements = List(3.5, 5.0, 1.5) Sum of elements = 10.0
此代码用连字符连接字符串。我们使用初始值作为空字符串。因此,我们的 fold 方法将在空字符串上应用运算符,以及与 reduce 一样,我们不会在集合的第一个值之前获得连字符。
例子 :// Scala program concatenate string // using fold function // Creating object object geeks { // Main method def main(arg:Array[String]) { // initialize a sequence of strings val str_elements: Seq[String] = Seq("hello", "Geeks", "For", "Geeks") println(s"Elements = $str_elements") // Concatenate strings with fold function val concat: String = str_elements.fold("")( (a, b) => a + "-" + b) println(s"After concatenation = $concat") } }
输出:
Elements = List(hello, Geeks, For, Geeks) After concatenation = -hello-Geeks-For-Geeks
- Scan : Scan函数将二元运算作为参数,并为该运算返回集合中每个元素的值。它返回集合中该二元运算符的每次迭代。在扫描中我们也可以定义初始值。
此代码使用扫描函数实现所有元素总和的迭代。
例子 :// Scala program sum of elements // using scan function // Creating object object geeks { // Main method def main(arg:Array[String]) { //initialize a sequence of numbers val numbers: Seq[Int] = Seq(4, 2, 1, 6, 9) println(s"Elements of numbers = $numbers") //find the sum of the elements using scan function val iterations: Seq[Int] = numbers.scan(0)(_ + _) println("Running total of all elements" + s"in the collection = $iterations") } }
输出:
Elements of numbers = List(4, 2, 1, 6, 9) Running total of all elements in the collection = List(0, 4, 6, 7, 13, 22)
这是用连字符连接字符串的实现,并显示了迭代。
例子 :// Scala program concatenate string // using scan function // Creating object object geeks { // Main method def main(arg:Array[String]) { // initialize a sequence of strings val str_elements : Seq[String] = Seq("hello", "Geeks", "For", "Geeks") println(s"Elements = $str_elements") // Concatenate strings with scan function val concat : Seq[String] = str_elements.scan("")((a, b) => a + "-" + b) println(s"After concatenation = $concat") } }
输出:
Elements = List(hello, Geeks, For, Geeks)
After concatenation = List(, -hello, -hello-Geeks, -hello-Geeks-For, -hello-Geeks-For-Geeks)