Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ in the stdlib such as:
- [isNotNullAndNotEmpty/Blank](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/isNotNullAndNot.kt)
- [identity](https://github.com/robstoll/kbox/tree/main/src/commonMain/kotlin/ch/tutteli/kbox/identity.kt)
- [Tuple4(Like) - Tuple9(Like)](https://github.com/robstoll/kbox/tree/main/src/commonMain/generated/kotlin/ch/tutteli/kbox)
(including functions mapComponentX, append, glue)
(including functions mapA1 - mapA9, append, glue)
- [Triple/Pair.mapFirst/Second/Third](https://github.com/robstoll/kbox/tree/main/src/commonMain/generated/kotlin/ch/tutteli/kbox/tupleMap.kt#L12)
- [toVararg](https://github.com/robstoll/kbox/tree/main/src/commonMain/generated/kotlin/ch/tutteli/kbox/toVararg.kt), [mapVararg](https://github.com/robstoll/kbox/tree/main/src/commonMain/generated/kotlin/ch/tutteli/kbox/mapVararg.kt), [varargToList (glue)](https://github.com/robstoll/kbox/tree/main/src/commonMain/generated/kotlin/ch/tutteli/kbox/varargToList.kt)

Expand Down
15 changes: 13 additions & 2 deletions src/commonMain/kotlin/ch/tutteli/kbox/takeIf.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package ch.tutteli.kbox

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* Calls the [provider] only if the [predicate] holds, returns `null` otherwise.
*
* This function shall complement `takeIf` from the stdlib of Kotlin for cases where the cost to call the provider
* This function shall complement [kotlin.takeIf] for cases where the cost to call the provider
* is high or the code involved to define the provider is long and a postfix `takeIf` is less readable
* than stating it at the beginning.
*
* @since 1.1.0
*/
inline fun <R> takeIf(predicate: Boolean, provider: () -> R): R? = if (predicate) provider() else null
//TODO move to xIf with 4.0.0
@OptIn(ExperimentalContracts::class)
inline fun <R> takeIf(predicate: Boolean, provider: () -> R): R? {
contract {
callsInPlace(provider, InvocationKind.AT_MOST_ONCE)
}
return if (predicate) provider() else null
}
21 changes: 21 additions & 0 deletions src/commonMain/kotlin/ch/tutteli/kbox/xIf.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ch.tutteli.kbox

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* Delegates to [let] in case the given [predicate] holds, returns `this` otherwise.
*
* This function shall complement [let] for cases such as invoking a builder method conditionally which without [letIf]
* would be written as follows `if (predicate) t.let{...} else t`
*
* @since 3.2.0
*/
@OptIn(ExperimentalContracts::class)
inline fun <T : U, R : U, U> T.letIf(predicate: Boolean, block: (T) -> R): U {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return if (predicate) let(block) else this
}
Loading