From fee676a2815c6ff9cbe0a20af80d89b55522ad84 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 9 Feb 2017 22:24:45 +0300 Subject: [PATCH] Introduce 'takeUnless' function which is like 'takeIf' but with the predicate inverted. #KT-7858 --- .../testData/weighers/smart/SmartPriority.kt | 2 ++ libraries/stdlib/src/kotlin/util/Standard.kt | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/idea/idea-completion/testData/weighers/smart/SmartPriority.kt b/idea/idea-completion/testData/weighers/smart/SmartPriority.kt index 12408c8ca63..aa3b06f690a 100644 --- a/idea/idea-completion/testData/weighers/smart/SmartPriority.kt +++ b/idea/idea-completion/testData/weighers/smart/SmartPriority.kt @@ -23,4 +23,6 @@ class C { // ORDER: nullableX // ORDER: takeIf // ORDER: takeIf +// ORDER: takeUnless +// ORDER: takeUnless // ORDER: C diff --git a/libraries/stdlib/src/kotlin/util/Standard.kt b/libraries/stdlib/src/kotlin/util/Standard.kt index 8230397d539..ab1835108af 100644 --- a/libraries/stdlib/src/kotlin/util/Standard.kt +++ b/libraries/stdlib/src/kotlin/util/Standard.kt @@ -68,6 +68,13 @@ public inline fun T.let(block: (T) -> R): R = block(this) @SinceKotlin("1.1") public inline fun T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null +/** + * Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does. + */ +@kotlin.internal.InlineOnly +@SinceKotlin("1.1") +public inline fun T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null + /** * Executes the given function [action] specified number of [times]. *