kotlin 常用高阶函数

package kotlinall.chapter5

import java.io.BufferedReader
import java.io.FileReader
import java.lang.StringBuilder

//高阶函数 传入的参数是函数 或者返回是函数
class Hello{
    fun world(){
        println("Hello World")
    }
}
fun test(){

}
class PdfPrinter{
    fun println(any:Any){
        kotlin.io.println(any)
    }
}
//高阶函数基础
fun baseFun(vararg args:String){
    //1.引用类里面的函数
    val hellowrold = Hello::world//拿到函数的引用
    args.filter(String::isNotEmpty)//接收的函数类型 T类型的参数 返回boolean值的函数 isNotEmtpy有一个隐形的参数就是String实例本身

    //2.引用包级函数 直接::函数名称
    val print1 = ::test

    //3.引用调用者的函数
    val pdfPrinter:PdfPrinter = PdfPrinter()
    args.forEach(pdfPrinter::println)//
    args.forEach(::println)
}

//常见高阶函数
fun commonFun(){
    val list = listOf<Int>(1,2,3,4,5)
    val newList = ArrayList<Int>()
    list.forEach{
        val newElement = 2*it + 3
        newList.add(newElement)
    }
    newList.forEach(::println)

    val newList1 = list.map { it*2+3 }//map 对每一个值进行处理添加一个新的list里面返回
    val newList2 = list.map { it.toDouble() }
    val newList3 = list.map(Int::toDouble)//采用函数引用的方式去传递 因为使用类的实例调用 所以有一个默认实例的参数

    val list4 = listOf(1..20,2..5,100..322)
    val flatList1 = list4.flatMap{//把集合的集合打平 可以对集合里的元素继续进行map
        it
    }
    val flatList = list4.flatMap{//把集合的集合打平 可以对集合里的元素继续进行map
        it.map {
            "NO.$it"
        }
    }
    flatList.forEach(::println)

    //
    flatList1.reduce { acc, i -> acc+i }//acc 当前运算结果 和当前元素的值
    (0..6).map(::factorial) //打印0,6的 阶乘的值
    //给reduce acc 添加初始值
    flatList1.fold(5){acc,i->acc+i}
    //redule acc 必须是 flatList迭代元素类型的父类
    //fold acc可以传入任意类型
    flatList1.fold(StringBuilder()){acc,i->acc.append(i).append(",")}
    println((0..6).joinToString(","))//拼接字符串

    flatList1.foldRight(StringBuilder()){i,acc->acc.append(i).append(",")}//倒叙拼接

    (0..6).map(::factorial).filter {
        it %2 ==1
    } //打印0,6的 阶乘的值 过滤出基数的值

    (0..6).map(::factorial).filterIndexed { index, i ->index%2==1  }//奇数位的结果
    (0..6).map(::factorial).takeWhile {  it%2==0}//取到第一个不是偶数则终止条件 (取到一个不满足条件的 终止循环)



}

fun factorial(n:Int):Int{
    if(n == 0) return 1
    return (1..n).reduce { acc, i ->acc*i  }
}

class Person(val name:String,val age:Int){
    fun work(){
        println("$name is wroking")
    }
}
fun findPerson():Person?{
    return null
}
fun commonFun1(){
     val person = findPerson()
     println(person?.name)//不为空则打印name 空显示空字符串
    findPerson()?.let { //不为空则调用let
        println(it.name)
        println(it.age)
    }
    findPerson()?.apply { //apply 相当于处于person类中 可以直接调用方法 和字段
        work()
        println(age)
    }
     val br = BufferedReader(FileReader("hello.txt"))
    with(br){//with 接收一个参数 作为apply使用
        var line:String?
        while (true){
            line = readLine()?:break
            println(line)
        }
        close()
    }
    val br1 = BufferedReader(FileReader("hello.txt")).readText()
    BufferedReader(FileReader("hello.txt")).use {//对于需要close的 使用的模板方法 可以使用use去处理
        var line:String?
        while (true){
            line = it.readLine()?:break
            println(line)
        }
    }

}

fun main(args: Array<String>) {

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值