9512.net

# 03 3000门徒 第三课 SCALA函数式编程彻底精通及SPARK源码阅读

1.

2.

3.

4.

5.

6.

val arr2 = Array("A", "B", "C") println(arr1.corresponds(arr2)(_ equalsIgnoreCase _)) //柯里化实例 val arr1 = Array("a", "b", "c") val arr2 = Array("A", "B", "C") def eq(a:String,b:String)={ a.toLowerCase() == b.toLowerCase() } println(arr1.corresponds(arr2)(eq(_,_)))

1.

def wordCount(dir: String) = { val wordCountMap = scala.collection.mutable.Map[String, Int]() val fileList = ListBuffer[File]() getAllFilesFromDir(dir, fileList) def seqop = (m1:scala.collection.mutable.HashMap[String,Int],word:(String,Int))=>{ m1(word._1) = m1.getOrElse(word._1,0)+word._2 m1 } def combop = (m1:scala.collection.mutable.HashMap[String,Int],m2:scala.collection.mutable.HashMap[String,Int])=>{

for((k,v)<-m1){ m2(k) = m2.getOrElse(k, 0)+v } m2 } val wordcount = fileList.flatMap(file => { val lines = Source.fromFile(file).getLines val words = lines.flatMap(line => line.split(" ")) words }).map((_,1)).aggregate(scala.collection.mutable.HashMap[String,Int]())(seqop, combop) println(wordcount) }