dave08
04/08/2021, 10:09 AMtrue
only if all functions return true
? (without putting them all on the same line... there's some code in between them)
var success = func1()
success = success && func2()
success = success && func3()
return success
Big Chungus
04/08/2021, 10:11 AMdave08
04/08/2021, 10:12 AM=&&
... but there isn't any.Big Chungus
04/08/2021, 10:13 AMdave08
04/08/2021, 10:15 AMfunc1().takeUnless { it } ?: return false
😛Michael Böiers
04/08/2021, 11:12 AMfun List<Boolean>.allTrue() = all { it }
fun foo() = true
fun bar() = false
fun baz() = true
val results = mutableListOf<Boolean>()
results += foo()
results += bar()
results += baz()
val endResult = results.allTrue()
dave08
04/08/2021, 11:20 AMclass CompoundSuccessResult(var result: Boolean) {
operator fun plusAssign(other: Boolean) { result = result && other }
}
fun Boolean.toCompoundSuccess() = CompoundSuccessResult(this)
val results = foo().toCompoundSuccess()
results += bar()
results += baz()
return results.result
Michael Böiers
04/08/2021, 12:19 PMclass Results {
var allOk: Boolean = true
private set
operator fun plusAssign(execution: Boolean) {
allOk = allOk && execution
}
}
fun foo() = true
fun bar() = false
fun baz() = true
val results = Results()
results += foo()
results += bar()
results += baz()
val endResult = results.allOk
dave08
04/08/2021, 12:29 PMMichael Böiers
04/08/2021, 12:41 PMRoukanken
04/08/2021, 1:18 PMreturn
of the function or if
that uses em, knows exactly what is required for it to pass/return true
eg
val isFeature1 = func1()
val isFeature2 = func2()
val isFeature3 = func3()
return isFeature1 && isFeature2 && isFeature3
ofc, this is not optimal in terms of performance, as all would need to be executed everytime
and does not work if the function calls are variable amountdave08
04/08/2021, 1:24 PMisFeature.takeUnless { it } ?: true
in that return statement...Michael Böiers
04/08/2021, 1:51 PMclass Runner {
var allOk: Boolean = true
private set
fun check(task: () -> Boolean) = also { allOk = allOk && task() }
}
fun foo() = true
fun bar() = false
fun baz() = true
fun main() {
val endResult = Runner()
.also {
// arbitrary code
}
.check { foo() }
.also {
// arbitrary code
}
.check { bar() }
.also {
// arbitrary code
}
.check { baz() }
.also {
// arbitrary code
}
.allOk
}