From 8a6b4bb49ebb16446f8328310070a1a5f06bce74 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 22 Jan 2023 23:01:06 +0600 Subject: [PATCH] add alsoIfTrue/alsoIfFalse/letIfTrue/letIfFalse --- .../dev/inmo/micro_utils/common/IfBoolean.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/IfBoolean.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/IfBoolean.kt index b953f423eb6..e6401389c4e 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/IfBoolean.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/IfBoolean.kt @@ -1,5 +1,31 @@ package dev.inmo.micro_utils.common +inline fun Boolean.letIfTrue(block: () -> T): T? { + return if (this) { + block() + } else { + null + } +} + +inline fun Boolean.letIfFalse(block: () -> T): T? { + return if (this) { + null + } else { + block() + } +} + +inline fun Boolean.alsoIfTrue(block: () -> Unit): Boolean { + letIfTrue(block) + return this +} + +inline fun Boolean.alsoIfFalse(block: () -> Unit): Boolean { + letIfFalse(block) + return this +} + inline fun Boolean.ifTrue(block: () -> T): T? { return if (this) { block()