您的位置:首页 > 其它

Scala深入浅出实战经典:20,Scala中的本地函数与作为语言一等公民的函数详解

2015-09-19 13:14 344 查看
package com.dt.scalaInAction.demo_020

import scala.io.Source

/**
* Scala中的本地函数与作为语言一等公民的函数详解
*/
object FunctionOps {
def main(args: Array[String]): Unit = {

val width = args(0).toInt
for(arg <- args.drop(1))
processData(arg, width)

/**
* 函数是有值的 函数可以赋值给变量
* (x: Int) => x+1   (x: Int)是匿名的  "=>"为函数的实现
*/
var increase = (x: Int) => x+1
println(increase(5))     //row result: 6
//变量可以接受 其他函数值
increase = (x: Int) => x*2
println(increase(5))     //row result: 10
println("-----------------------------------")

val someNumbers = List(-11, -10, -5, 0, 5, 10)
someNumbers.foreach( (x: Int) => print(x + "\t") )  //row result:-11 -10 5 0 5 10
println()
someNumbers.filter( (x: Int) => x > 0 ).foreach( (x: Int) => print(x + "\t") )  //row result:5 10
println()
someNumbers.filter( (x) => x > 0 ).foreach( (x: Int) => print(x + "\t") )       //row result:5 10
println()
someNumbers.filter( x => x > 0).foreach( (x: Int) => print(x + "\t") )          //row result:5 10
println()
someNumbers.filter( _ > 0 ).foreach( (x: Int) => print(x + "\t") )              //row result:5 10
val f = (_: Int) + (_: Int)
println()
println(f(5, 10))     //row result: 15
}

/**
* 本地函数  line20-23
* (1) 本地函数所在的函数的外界不可以访问本地函数
* (2) 本地函数可以访问 所在函数传进来的参数
*
* Scala中允许在函数中定义函数  定义的函数在私有函数 外界不可以访问   这样的函数被称之为"本地函数"也成为"内部函数"
* 这样的做理由是因为 函数在Scala中是一等公明  函数可以看成变量一样 可以赋值
*/
def processData(filename: String, width: Int) {
//该函数为processData的私有函数 外部不可以访问
def processLine(line: String) {
if (line.length > width)
println(filename + ": " + line)
}

val source = Source.fromFile(filename)
for(line <- source.getLines)
processLine(line)
}
}


以上内容是从王家林老师DT大数据课程第20讲的学习笔记和个人整理。

DT大数据微信公众账号:DT_Spark

王家林老师QQ:1740415547

王家林老师微信号:18610086859

第20讲视频网站地址:http://pan.baidu.com/s/1kT3nIoZ
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: