Introduce new function 'takeIf' to replace value with null if it doesn't match the predicate.

#KT-7858
This commit is contained in:
Ilya Gorbunov
2017-01-14 08:00:32 +03:00
parent 1ac9815d76
commit 1ae56c374f
@@ -61,6 +61,13 @@ public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
/**
* Returns `this` value if it satisfies the given [predicate] or `null`, if it doesn't.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
/**
* Executes the given function [action] specified number of [times].
*