Skip to content

Instantly share code, notes, and snippets.

@fuzzyweapon
Created October 10, 2018 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzyweapon/00098a064fd46ca5a5f8a3cd5cb5bcd7 to your computer and use it in GitHub Desktop.
Save fuzzyweapon/00098a064fd46ca5a5f8a3cd5cb5bcd7 to your computer and use it in GitHub Desktop.
takeIf
/**
* 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