Created
October 10, 2018 18:38
-
-
Save fuzzyweapon/00098a064fd46ca5a5f8a3cd5cb5bcd7 to your computer and use it in GitHub Desktop.
takeIf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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? { | |
contract { | |
callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) | |
} | |
return if (predicate(this)) this else null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment