Kotlin常用的高阶函数(Filter、TakeWhile、Let、Apply、With......)

简介: 一、Filterpackage net.println.kotlin.chapter5.builtins/** * @author:wangdong * @description:Kotlin常见的高阶函数 */fun main(args: Array) { //求(0.

一、Filter

package net.println.kotlin.chapter5.builtins

/**
 * @author:wangdong
 * @description:Kotlin常见的高阶函数
 */

fun main(args: Array<String>) {

    //求(0..6)的阶乘
    (0..6).map(::factorial).forEach(::println)
    //阶乘过滤,只要阶乘是奇数的
    //public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
    //    return filterTo(ArrayList<T>(), predicate)
    //}
    println((0..6).map(::factorial).filter { it % 2 == 1 })  //[1, 1]
    //要处于奇数位上的阶乘
    println((0..6).map(::factorial).filterIndexed{ index, i ->  index % 2 == 1 })   //[1, 6, 120]
}

/**reduce求阶乘*/
//一个正整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,并且有0的阶乘为1。 自然数n的阶乘写作n!。
//例如3的阶乘:1*2*3 = 6
fun factorial(n: Int): Int{
    if (n == 0)return 1
    //如果n >0,本次的acc*下一次的acc
    return (1..n).reduce{acc,i -> acc * i}
}

二、TakeWhile

package net.println.kotlin.chapter5.builtins

/**
 * @author:wangdong
 * @description:Kotlin常见的高阶函数
 */

fun main(args: Array<String>) {

    //求(0..6)的阶乘
    (0..6).map(::factorial).forEach(::println)
    //1
    //1
    //2
    //6
    //24
    //120
    //720

    //现在一个程序是,要按照顺序取数,遇到不符合要求的,程序就停止,例如遇到第一个偶数就停止
    println((0..6).map(::factorial).takeWhile { it % 2 == 1 })  //[1, 1]
}

/**reduce求阶乘*/
//一个正整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,并且有0的阶乘为1。 自然数n的阶乘写作n!。
//例如3的阶乘:1*2*3 = 6
fun factorial(n: Int): Int{
    if (n == 0)return 1
    //如果n >0,本次的acc*下一次的acc
    return (1..n).reduce{acc,i -> acc * i}
}

三、Let的使用

package net.println.kotlin.chapter5.builtins


/**
 * @author:wangdong
 * @description:Kotlin常见的高阶函数
 */

fun main(args: Array<String>) {

    //let的使用
    findPerson() ?.let { persion ->
        persion.work()
    }
    //王栋 is working!!!
}

/**定义一个Person类*/
data class Person(var name:String, var age: Int){
    fun work(){
        println("$name is working!!!")
    }
}

fun findPerson(): Person ?{
    return Person("王栋",23)
}

四、Apply

package net.println.kotlin.chapter5.builtins
/**
 * @author:wangdong
 * @description:Kotlin常见的高阶函数
 */

fun main(args: Array<String>) {

    /**
     * Calls the specified function [block] with `this` value as its receiver and returns `this` value.
     */
    /*@kotlin.internal.InlineOnly
    public inline fun <T> T.apply(block: T.() -> Unit): T {
        contract {
            callsInPlace(block, InvocationKind.EXACTLY_ONCE)
        }
        block()
        return this
    }*/
    //apply的使用,可以直接调用work()方法,可以使用Persion中的成员变量
    findPerson() ?.apply {
        work()
        print(age)
    }

}

/**定义一个Person类*/
data class Person(var name:String, var age: Int){
    fun work(){
        println("$name is working!!!")
    }
}

fun findPerson(): Person ?{
    return Person("王栋",23)
}

五、With和Apply的区别
With用来传参、Apply用来调用

/**
 * Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return receiver.block()
}

/**
 * Calls the specified function [block] with `this` value as its receiver and returns `this` value.
 */
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block()
    return this
}

六、两种方式读文件内容

package net.println.kotlin.chapter5.builtins

import java.io.BufferedReader
import java.io.FileReader


/**
 * @author:wangdong
 * @description:Kotlin常见的高阶函数
 */

fun main(args: Array<String>) {

    //1.推荐的输出方式
    val br = BufferedReader(FileReader("hello.txt")).readText()
    println(br)
    /*2.同样可以输出
    with(br){
        var line: String?
        while (true){
            line = readLine() ?: break
            println(line)
        }
        close()
    }*/

    //hello 王栋
}

七、Use
看看Use的源码

/**
 * Executes the given [block] function on this resource and then closes it down correctly whether an exception
 * is thrown or not.
 *
 * @param block a function to process this [Closeable] resource.
 * @return the result of [block] function invoked on this resource.
 */
@InlineOnly
@RequireKotlin("1.2", versionKind = RequireKotlinVersionKind.COMPILER_VERSION, message = "Requires newer compiler version to be inlined correctly.")
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
    var exception: Throwable? = null
    try {
        return block(this)
    } catch (e: Throwable) {
        exception = e
        throw e
    } finally {
        when {
            apiVersionIsAtLeast(1, 1, 0) -> this.closeFinally(exception)
            this == null -> {}
            exception == null -> close()
            else ->
                try {
                    close()
                } catch (closeException: Throwable) {
                    // cause.addSuppressed(closeException) // ignored here
                }
        }
    }
}

八、Use的小例子

package net.println.kotlin.chapter5.builtins

import java.io.BufferedReader
import java.io.FileReader


/**
 * @author:wangdong
 * @description:Kotlin常见的高阶函数
 */

fun main(args: Array<String>) {

    //1.推荐的输出方式
    val br = BufferedReader(FileReader("hello.txt")).use {
        var line: String?
        while (true) {
            line = it.readLine() ?: break
            println(line)
        }
    }
    //hello 王栋
}

好了,结束了!

目录
相关文章
|
8月前
|
Kotlin
Kotlin中标准库函数(apply、let、run、with、also、takeIf、takeUnless)的使用详解
Kotlin中标准库函数(apply、let、run、with、also、takeIf、takeUnless)的使用详解
56 0
|
8月前
|
Java Kotlin
Kotlin中匿名函数(又称为Lambda,或者闭包)和高阶函数的详解
Kotlin中匿名函数(又称为Lambda,或者闭包)和高阶函数的详解
76 0
|
9月前
|
XML Java Android开发
Kotlin作用域函数let、with、run、apply、also
Kotlin作用域函数let、with、run、apply、also
65 0
|
Serverless Kotlin
Kotlin | 高阶函数reduce()、fold()详解
在 `Kotlin` 中,`reduce()` 和 `fold()` 是函数式编程中常用的高阶函数。它们都是对集合中的元素进行聚合操作的函数,将一个集合中的元素缩减成一个单独的值。它们的使用方式非常相似,但是返回值略有不同
106 0
|
Kotlin
Kotlin 作用域函数之let、with、run、also、apply的使用笔记
`Kotlin` 标准库包含几个函数,目的是在对象的上下文中执行代码块。**当对一个对象调用这样的函数并提供一个 `lambda` 表达式时,会形成一个临时作用域。在此作用域中,可以访问该对象而无需其名称。这些函数称为作用域函数**。共有以下五种:`let、run、with、apply 以及 also`。
139 0
|
存储 缓存 算法
Kotlin | 扩展函数(终于知道为什么 with 用 this,let 用 it)
Kotlin | 扩展函数(终于知道为什么 with 用 this,let 用 it)
226 0
Kotlin | 扩展函数(终于知道为什么 with 用 this,let 用 it)
|
C++ Kotlin Windows
Kotlin标准函数run with let also apply的区别
Kotlin标准函数run with let also apply的区别
Kotlin标准函数run with let also apply的区别
|
Java C++ Kotlin
【Kotlin】Kotlin 高阶函数 ( 高阶函数当做参数 | 高阶函数定义 | 高阶函数调用 )
【Kotlin】Kotlin 高阶函数 ( 高阶函数当做参数 | 高阶函数定义 | 高阶函数调用 )
121 0
|
Kotlin
Kotlin高阶函数概念
一、高阶函数的基本概念 1.传入或者返回函数的函数(传入是函数,返回也是函数) 2.函数引用最常见的方式,”:: println” 3.
908 0
|
Java Kotlin
Kotlin常用的高阶函数(ForEach、Map、Flatmap、Fold、Reduce......)
一、ForEach 类型于Java的传统的写法 package net.println.kotlin.chapter5.
10063 2