zain
02/15/2022, 9:53 AMprivate fun simulateFixtures(list: List<List<Team>>) = list.map { fixture ->
fixture.toList().random()
}.shuffled().chunked(2)
Sam
02/15/2022, 10:11 AMSam
02/15/2022, 10:11 AMzain
02/15/2022, 10:18 AMSam
02/15/2022, 10:24 AMfoo.let {
if (somePredicate) it.doSomething() else it
}
Sam
02/15/2022, 10:24 AMzain
02/15/2022, 10:25 AMSam
02/15/2022, 10:27 AMlet
+ `if`/`else`Sam
02/15/2022, 10:29 AMSam
02/15/2022, 10:29 AMelse it
on the end is particularly cumbersomeJoffrey
02/15/2022, 10:39 AMJoffrey
02/15/2022, 10:41 AMList<List<Team>>
, you don't need toList()
before using random()
on an element. It's already a list, and you don't need a copy just to choose a random element out of it.Michael de Kaste
02/15/2022, 10:45 AMcondition ? onTrue : onFalse
ephemient
02/15/2022, 12:05 PMArthur Khazbulatov
09/01/2023, 12:55 PMsomething
.let { if (condition1) block1(it) else it }
.let { if (condition2) block2(it) else it }
I suggest adding these custom scope extension functions to your arsenal:
inline fun <T> T.letIf(condition: Boolean, block: (T) -> T): T =
if (condition) this.let(block) else this
inline fun <T> T.runIf(condition: Boolean, block: T.() -> T): T =
if (condition) this.run(block) else this
inline fun <T> T.alsoIf(condition: Boolean, block: (T) -> Unit): T =
if (condition) this.also(block) else this
inline fun <T> T.applyIf(condition: Boolean, block: T.() -> Unit): T =
if (condition) this.apply(block) else this
Then you could use them like this:
import my.cool.extensions.letIf
something
.letIf (condition1) { block1(it) }
.letIf (condition2) { block2(it) }
ephemient
09/10/2023, 2:14 AMinline fun <T : R, R> T.letIf(condition: Boolean, block: (T) -> R): R =
if (condition) this.let(block) else this
second, that type of extension is waiting for https://youtrack.jetbrains.com/issue/KT-32993