/** * Calls the specified function [block] and returns its result. */ @kotlin.internal.InlineOnly publicinlinefun<R>run(block: () -> R): R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() }
/** * Calls the specified function [block] with `this` value as its receiver and returns its result. */ @kotlin.internal.InlineOnly publicinlinefun<T, R> T.run(block: T.() -> R): R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() }
/** * Calls the specified function [block] with the given [receiver] as its receiver and returns its result. */ @kotlin.internal.InlineOnly publicinlinefun<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 publicinlinefun<T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() returnthis }
/** * Calls the specified function [block] with `this` value as its argument and returns `this` value. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") publicinlinefun<T> T.also(block: (T) -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block(this) returnthis }
/** * Calls the specified function [block] with `this` value as its receiver and returns its result. */ @kotlin.internal.InlineOnly publicinlinefun<T, R> T.run(block: T.() -> R): R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() }
/** * Calls the specified function [block] with `this` value as its argument and returns its result. */ @kotlin.internal.InlineOnly publicinlinefun<T, R> T.let(block: (T) -> R): R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block(this)
val original = "abc" // Evolve the value and send to the next chain original.let { println("The original String is $it") // "abc" it.reversed() // evolve it as parameter to send to next let }.let { println("The reverse String is $it") // "cba" it.length // can be evolve to other type }.let { println("The length of the String is $it") // 3 } // Wrong // Same value is sent in the chain (printed answer is wrong) original.also { println("The original String is $it") // "abc" it.reversed() // even if we evolve it, it is useless }.also { println("The reverse String is ${it}") // "abc" it.length // even if we evolve it, it is useless }.also { println("The length of the String is ${it}") // "abc" } // Corrected for also (i.e. manipulate as original string // Same value is sent in the chain original.also { println("The original String is $it") // "abc" }.also { println("The reverse String is ${it.reversed()}") // "cba" }.also { println("The length of the String is ${it.length}") // 3 }
/** * Returns `this` value if it satisfies the given [predicate] or `null`, if it doesn't. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") publicinlinefun<T> T.takeIf(predicate: (T) -> Boolean): T? { contract { callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) } returnif (predicate(this)) thiselsenull }
/** * Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does. */ @kotlin.internal.InlineOnly @SinceKotlin("1.1") publicinlinefun<T> T.takeUnless(predicate: (T) -> Boolean): T? { contract { callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) } returnif (!predicate(this)) thiselsenull }
/** * Executes the given function [action] specified number of [times]. * * A zero-based index of current iteration is passed as a parameter to [action]. * * @sample samples.misc.ControlFlow.repeat */ @kotlin.internal.InlineOnly publicinlinefunrepeat(times: Int, action: (Int) -> Unit) { contract { callsInPlace(action) }
//斐波那契数列 funfibonacci():()->Long{ var first = 0L var second = 1L returnfun():Long{ //返回返回值为Long类型的函数 val result = second second += first first = second - first return result } }