高阶函数
传入或者返回函数的函数
1.常见高阶函数
forEach
val list = listOf(1,2,3)
list.forEach(::println)
map
val list = listOf(1,2,3)
val newList = list.map {
it*2+1
}
newList.forEach(::println)
flatMap
val mlist = listOf(
1..20,
2..5
)
val flatList = mlist.flatMap{
it.map {
"No. $it"
}
}
flatList.forEach(::println)
reduce
累积数据处理
val mlist = listOf(
1..20,
2..5
)
val flatList = mlist.flatMap{ it }
println(flatList.reduce{ acc,i -> acc + i })
fold
添加初始值
(0..6).map(::factorial).fold(5){acc, i -> acc+i}
//n的阶乘
fun factorial(n:Int):Int{
if (n==0) return 1
return (1..n).reduce{acc, i -> acc*i }
}
filter
println((0..6).map(::factorial).filter { it%2 == 1 })
takeWhile
当遇到不符合条件的退出
println((0..6).map(::factorial).takeWhile { it%2 == 1 })
let/apply
data class Person(val name:String, val age:Int){
fun work(){
println("$name is working!!")
}
}
fun main(args: Array<String>) {
findPerson()?.let { (name, age)->
println(name)
println(age)
}
findPerson()?.apply {
work()
println(age)
}
}
fun findPerson():Person?{
return null
}
with
val br = BufferedReader(FileReader("hello.txt"))
with(br){
var line:String?
while (true){
line = readLine()?:break
println(line)
}
close()
}
2.尾递归优化
- 递归的一种特殊形式
- 调用自身后物其他操作
- tailrec关键字提示编译器尾递归优化
- 尾递归与迭代的关系
//尾递归
tailrec fun findListNode(head:ListNode?, value:Int):ListNode?{
head?:return null
if (head.value == value) return head
return findListNode(head.next, value)
}
//非尾递归
fun factorial(n:Long):Long{
return n* factorial(n-1)
}
3.闭包
- 函数运行的环境
- 持有函数运行状态
- 函数内部可以定义函数,也可以定义类
//fun add(x:Int) = fun(y:Int) = x+y
fun add(x:Int):(Int)->Int{
return fun(y:Int):Int{
return x+y
}
}
4.函数复合
val add5 = {i:Int -> i+5}
val multiplyBy2 = {i:Int -> i*2}
infix fun <P1, P2, R> Function1<P1, P2>.andThen(function:Function1<P2,R>):Function1<P1,R>{
return fun(p1:P1):R{
return function.invoke(this.invoke(p1))
}
}
fun main(args: Array<String>) {
println(multiplyBy2(add5(8)))
val infixFun = add5 andThen multiplyBy2
println(infixFun(8))
}
5.颗粒化
- 简单说就是多元函数变换成一元函数调用链
fun log(tag:String, target:OutputStream, message:Any?){
target.write("[$tag]$message\n".toByteArray())
}
//颗粒化
fun log(tag:String)
= fun(target:OutputStream)
= fun(message:Any?)
= target.write("[$tag]$message\n".toByteArray())
fun main(args: Array<String>) {
log("benny", System.out, "helloWorld")
log("benny")(System.out)("HelloWorld Again!")
}
6.偏函数
- 传入部分参数得到的新函数
fun log(tag:String, target:OutputStream, message:Any?){
target.write("[$tag]$message\n".toByteArray())
}
fun main(args: Array<String>) {
log("benny", System.out, "helloWorld")
val consoleWithTag = (::log.curried())("benny")(System.out)
consoleWithTag("Hello World Again Again!")
consoleWithTag("Hello World Again Again Again!")
}
fun <P1,P2,P3,R> Function3<P1,P2,P3,R>.curried()
= fun(p1:P1) = fun(p2:P2) = fun(p3:P3) = this(p1, p2, p3)