spark 中的 reduce 非常的好用,reduce 可以对 dataframe 中的元素进行计算、拼接等等。...sentence FROM BIGDATA") val a: RDD[String] = sqlresult.rdd.map(_.getAs[String]("sentence")) val b = a.reduce...List[String]] = sqlresult.rdd.map{ row=>List(row.getAs[String]("sentence"))} val d: List[String] = c.reduce
reduce, reduceLeft, reduceRight: 计算一个单独的累计结果。 fold, foldLeft, foldRight: 计算一个单独的累计结果,带一个起始值。...reduce, fold, scan: 用于并行计算。 reduceLeft, foldLeft, scanLeft: 从前到后线性计算。...foldLeft是线性计算。...") println("foldLeft: " + listAdd + " to: " + listAdd.foldLeft(100)(addOp(_, _))) println...Suereth Community-driven documentation for Scala Collection Overview
reduce将RDD中元素前两个传给输入函数,产生一个新的return值,将新产生的return值与RDD中下一个元素(即第三个元素)组成两个元素,再被传给输入函数,这样递归运作,直到最后只有一个值为止...*/ val rdd07 = sc.parallelize(1 to 10) val sum = rdd07.reduce((x, y) => x + y) println("sum
map(), reduce()高阶函数。...20, 30) scala> val sum = unique.reduce( (a: Int, b: Int) => a + b ) sum: Int = 60 map是一个不可变的键值库,其他语言也叫...] = List(MILK, TEA) reduce规约列表 列表规约指的是把列表收缩为单个值 数学reduce操作:max,min,product,sum boolean reduce操作:contains...主要关注点是fold和foldLeft版本之间的差别。fold,reduce和scan都限于返回与列表元素类型相同的一个值。foldLeft可以实现forall布尔操作,但是fold做不到。...// false是starting value // def foldLeft[B](z: B)(op: (B, A) ⇒ B): B scala> val included = List(46, 19
A 2 def op(a1: A, a2: A): A //二元函数 3 val zero: A //恒等值identity 4 } 我们用scala...可以把reduce函数的参数拓展开来看看: 1 reduce[A](as: List[A])(zero: A)(op: (A,A) => A) : A 这个类型款式跟折叠算法的类型款式非常相似: 1...//> listConcatMonoid: [A]=> ch10.ex1.Monoid[List[A]]{val zero: scala.collection...我们也可以用foldLeft来实现foldMap。...f: A => B): B foldRight和foldLeft的f函数是(A,B) => B,如果用curry表达:A => (B => B),如果能把 A => ?
scala> arr.aggregate(0)(_+_.sum,_+_) res14: Int = 20 scala> arr.aggregate(10)(_+_.sum,_+_) res16...21 //整体汇总 //实际上调用的是reduceLeft scala> a.reduce(_ + _) res24: Int = 21 //并行支持 scala> a.par res25: scala.collection.parallel.mutable.ParArray...[Int] = ParArray(1, 2, 3, 4, 5, 6) scala> a.par.reduce(_ + _) res26: Int = 21 //折叠:有初始值(无特定顺序) //fold...> a.par.fold(10)(_ + _) res28: Int = 51 //折叠:有初始值(有特定顺序) //foldLeft()(_ + _) //foldRight()(_ + _)...> res29.mapValues(_.foldLeft(0)(_ + _._2)) res31: scala.collection.immutable.Map[String,Int] = Map(tom
Set 特性 不重复、无序 不可变set 创建Set& apply方式创建 val set=Set[Int](1,2,3,4,5) 查看setApi 进入 scala $ scala Welcome...scala> val set=Set[Int](1,2,3,4,5) set: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4) set....partition toArray -- fold product toBuffer /: foldLeft...reduce toIndexedSeq :\ foldRight reduceLeft...- find product toArray -- flatMap reduce
scala> arr.reduce(_+_) res8: Int = 25 scala> arr.reduce(_*_) res9...: Int = 700 scala> arr.reduce(_-_) res10: Int = -23 #注意:从左到右的顺序依次计算... scala> arr.reduce(_-_) //默认使用reduceLeft,从左到右计算 res10: Int = -23 ...list: List[Int] = List(1, 2, 3, 4, 5) scala> list.reduce(_+_) res0: Int = 15...(0)(_+_) ^ scala> arr.foldLeft(0)((x, y) => x + y._2
sortWith +: fold mkString sorted /: foldLeft...collect indexOfSlice productPrefix toBuffer collectFirst indexWhere reduce...> import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer scala> val list...orElse sum -- fold padTo tail --= foldLeft...aggregate hashCode product toIterable andThen head reduce
下面是一个例子,在Scala集合 trait TraversableOnce 定义了 foldLeft def foldLeft[B](z: B)(op: (B, A) => B): B foldLeft...以下是该函数的一个用例: 从初值0开始, 这里 foldLeft 将函数 (m, n) => m + n 依次应用到列表中的每一个元素和之前累积的值上。...val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val res = numbers.foldLeft(0)((m, n) => m + n) print...如果不使用多参数列表,代码可能像这样: numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) 注意使用多参数列表时,我们还可以利用Scala的类型推断来让代码更加简洁...,如下所示: val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numberFunc = numbers.foldLeft(List[Int](
使用 map 映射函数来解决11.1.4 模拟实现 map 映射函数的机制11.1.5 课堂练习11.2 集合元素的扁平-flatMap11.3 集合元素的过滤-filter11.4 集合元素的化简-reduce11.5...startA) println("names=" + names2) // names=List(Alice) } } 输出结果如下: names=List(Alice) 11.4 集合元素的化简-reduce...list.reduceLeft(minus)) // -13 println(list.reduceRight(minus)) // 3 = (1-(2-(3-(4-5)))) println(list.reduce...目的:理解 foldLeft 的用法 示例代码如下: package com.atguigu.chapter11.exercise import scala.collection.mutable.ArrayBuffer... 遍历 for (item <- viewSquares2) { println(item) } // 小结: // 对集合进行 map, filter, reduce
op 特性1:参数为一个匿名函数 特性2:规约结果一定是List元素的类型,所以是被经常使用的(相较于foldLeft) 对于List变量a scala> a res33: List[Int] = List...scala> a.reduceLeft(_+_) res32: Int = 10 foldLeft(z:U)(op: (U, T) => U ) z x1 x2 ... xn op...op 特性1:使用柯里化定义 特性2:必须有初始值z 特性3:返回值是初始值z的类型,故不太使用 scala> a res33: List[Int] = List(1, 2, 3, 4) //使用foldLeft...进行元素的求和,并且初值为0 scala> a.foldLeft(0)((x,y) => x+y) res34: Int = 10 //使用通配符 scala> a.foldLeft(0)(_+_) res35...: Int = 10 //初值改变后的结果 scala> a.foldLeft(1)(_+_) res36: Int = 11 惰性求值的类型:Stream 流 //使用to或until来获取range
reduce reduce 收敛 4个参数,返回的是叠加后的结果, 原数组不发生变化,回调函数返回的结果 //从左向右 //prev 代表前一项,cur 代表当前项 【求和】 let arr =...[1,3,5,8,9,7]; let sum = arr.reduce(function(prev,cur,index,arr){ //return 100;//本次的返回值 会作为下一次的...; 还可以这样 var arr1 = [{price:50,count:8},{price:50,count:6},{price:45,count:9}]; let totalSum = arr1.reduce...console.log("总价格是:",totalSum);//会返回NAN 因为第一次会返回一个数,将作为下一次的prev,就没有price 和 count属性了 解决办法 let totalSum1 = arr1.reduce...cur.price; },0);//默认指定第一次的prev console.log("总价格是:",totalSum1); 【求和乘】 let arr2 = [1,2,3]; let res = arr2.reduce
Map 集合 Scala中的Map和Java类似,也是一个散列表,它存储的内容也是键值对(key-value)映射,Scala中不可变的Map是有序的,可变的Map是无序的。...支持的Api scala> map. + contains foldLeft iterator product...splitAt toStream ++ copyToArray foldRight keySet reduce
集合 1.列表 List scala> val numbers = List(1, 2, 3, 4) numbers: List[Int] = List(1, 2, 3, 4) 2.集 Set scala...scala> val hostPort = ("localhost", 80) hostPort: (String, Int) = (localhost, 80) scala> hostPort._1...scala> numbers.drop(5) res0: List[Int] = List(6, 7, 8, 9, 10) scala> numbers.dropWhile(_ % 2 !...= 0) res0: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9, 10) 8.foldLeft & foldRight scala> numbers.foldLeft...(0)((m: Int, n: Int) => m + n) res0: Int = 55 scala> numbers.foldLeft(0) { (m: Int, n: Int) => println
Failure(e) => println(s"Error processing future operations, error = ${e.getMessage}") } Future.foldLeft...VS Future reduceLeft foldLeft 和 reduceLeft 都是用来从左到右做集合操作的,区别在于foldLeft可以提供默认值。...看下下面的例子: println(s"\nStep 3: Call Future.foldLeft to fold over futures results from left to right") val...futureFoldLeft = Future.foldLeft(futureOperations)(0){ case (acc, someQty) => acc + someQty.getOrElse...println(s"Error processing future operations, error = ${e.getMessage}") } 输出结果: Step 3: Call Future.foldLeft
$1 (scala.collection) apply:157, TraversableOnce$$anonfun$foldLeft$1 (scala.collection) foreach:891,...$1 (scala.collection) apply:157, TraversableOnce$$anonfun$foldLeft$1 (scala.collection) foreach:891,...$1 (scala.collection) apply:157, TraversableOnce$$anonfun$foldLeft$1 (scala.collection) foreach:891,...$1 (scala.collection) apply:157, TraversableOnce$$anonfun$foldLeft$1 (scala.collection) foreach:891,...$1 (scala.collection) apply:157, TraversableOnce$$anonfun$foldLeft$1 (scala.collection) foreach:891,
关于遍历,只要具备可遍历结构,都可以使用reduce解决,不管是数组、字符串、对象、set、map 1....用reduce实现数组一些api 给数组prototype加上基于reduce实现的api: Object.assign(Array.prototype, { myMap(cb, _this = this...不是数组怎么reduce 上面的测试也用了reduce,是对一个对象reduce。...只要是遍历某个数据结构,产生一个结果,那么都可以使用reduce解决: 普通对象:使用Object.keys,Object.values,Object.entries再reduce 类数组对象:使用[....reduce的感觉。
摘 要 基于Scala Acotor实现多线程单词统计(WordCount) package com.itunic.scala import scala.io.Source import scala.actors...{Actor, Future} import scala.collection.mutable /** * Created by itunic.com on 2016/12/9. */ class...Source.fromFile(fileName).getLines().flatMap(_.split("\t")).map((_, 1)).toList.groupBy(_._1).mapValues(_.foldLeft...100) } //计算最终结果 val count = resultList.flatMap(_.result).toList.groupBy(_._1).mapValues(_.foldLeft
中集合的基本概述以及常用集合的基本操作,本次住要分享Scala中集合更高级的操作。...相当于先进行 map 操作,在进行 flatten 操作 分组 groupBy(分组规则) 按照指定的规则对集合的元素进行分组 Reduce操作: 简化/规约 reduce 对所有数据做一个处理,规约得到一个结果...println(list.reduce(_ + _)) println(list.reduceLeft(_ + _)) val list2 = List(3, 4, 5, 7...- (5- (7- (8-90)))) //fold println(list.fold(10)(_ - _)) // 10-1-2-3-4-5 println(list.foldLeft...集合总结分享到这里就结束了,希望对大家学习Scala语言有所帮助!!!