标签:
今晚听了王家林老师3000门徒spark系列课程的第三课,讲述了scala函数,下面写一下心得:
普通函数:def fun1(name:String){println(name)}
函数赋值给变量:val fun1 = functionName _
匿名函数:val fun2 = (name:String) => prinln(name)
高阶函数:def bigData(func:(String) => Unit, content: String){func(content)}
返回值是函数的函数:def func_returned(content:String) = (message:String) => println(content+ " " +message)
curring:def sum_Currying(x:Int) = (y:Int) => x+y
另外课后题:统计一个目录下单词出现的总次数,纯scala来做,下面是我写的代码:
import scala.io.Source
import java.io.File
import scala.collection.mutable
object DirectoryWordCount {
val map = mutable.Map.empty[String, Int]
def wc(file:File) {
if(file.isFile()){
val fileContent = Source.fromFile(file)
for (line <- fileContent.getLines){
line.split(" ").foreach { x =>
if(map.contains(x)) map(x) = map(x)+1
else map(x) = 1
}
}
}
else file.listFiles().foreach(wc)
}
def main(args: Array[String]) {
wc(new File("d:/lian"))
for((key,value) <- map) println(key+":"+value)
}
}
标签:
原文地址:http://www.cnblogs.com/haitianS/p/5097547.html