object collection_t1 { def flatMap1(): Unit = { val li = List(,,) val res = li.flatMap(x => x match { => List('a','b') ) }) println(res) } def map1(): Unit = { val li = List(,,) val res = li.map(x => x match { => List('a','b') }) println(res)…
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support functional higher-order operations such as map, filter, and reduce that let you use expression-oriented programming in collections. Higher-order operatio…
欢迎关注我的新博客地址:http://cuipengfei.me/blog/2014/10/31/spark-fold-aggregate-why-not-foldleft/ 大家都知道Scala标准库的List有一个用来做聚合操作的foldLeft方法. 比方我定义一个公司类: 1 case class Company(name:String, children:Seq[Company]=Nil) 它有名字和子公司. 然后定义几个公司: 1 val companies = List(Compa…
Given that sequence, use reduceLeft to determine different properties about the collection. The following example shows how to get the sum of all the elements in the sequence: scala> a.reduceLeft(_ + _) res0: Int = 64 Don’t let the underscores throw…
1 详细信息 User class threw exception: java.lang.IllegalStateException: Cannot call methods on a stopped SparkContext. This stopped SparkContext was created at: org.apache.spark.SparkContext.<init>(SparkContext.scala:76) com.wm.bigdata.spark.etl.RentO…
先从一道题开始看: Eliminate consecutive duplicates of list elements. If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed. Example: scala> compress(List('a, 'a, 'a, 'a…
在函数式语言中,函数作为一等公民,可以在任何地方定义,在函数内或函数外,可以作为函数的参数和返回值,可以对函数进行组合.由于命令式编程语言也可以通过类似函数指针的方式来实现高阶函数,函数式的最主要的好处主要是不可变性带来的.没有可变的状态,函数就是引用透明(Referential transparency)的和没有副作用(No Side Effect). 任何一种函数式语言中,都有map函数与faltMap这两个函数,比如Python虽然不是纯函数式语言,也有这两个函数.再比如在jdk1.8之后…