Bernhard
07/24/2024, 4:06 PMRuckus
07/24/2024, 4:14 PMinline fun <T> T.repeat(
count: Int,
action: (T) -> T,
): T {
var x = this
kotlin.repeat(count) {
x = action(x)
}
return x
}
Bernhard
07/24/2024, 4:15 PMBernhard
07/24/2024, 4:16 PMfun <T> Int.repeat(initial: T, block: (T) -> T): T {
var accumulator = initial
kotlin.repeat(this) {
accumulator = block(accumulator)
}
return accumulator
}
Ruckus
07/24/2024, 4:17 PMrepeat
signature, but that's just a personal preference.Bernhard
07/24/2024, 4:18 PMRuckus
07/24/2024, 4:20 PMfold
(e.g. foldN
?) since that's probably closer in meaning to what you're doing.
(compare Kotlin's various fold
functions)Youssef Shoaib [MOD]
07/24/2024, 4:20 PM(0..2).fold(3) { it, _ -> it + 1 }
but it's kinda ugly because it passes the index inKlitos Kyriacou
07/24/2024, 4:41 PMgenerateSequence(3) { it + 1 }.take(1 + 2).last()
(it's 1 + 2 because the seed (3) is counted as the first element, and you want another 2).ephemient
07/25/2024, 4:26 AMgenerateSequence(3) { it + 1 }.elementAt(2)