bj0
12/11/2024, 5:20 PMKlitos Kyriacou
12/11/2024, 5:32 PMfun <T> Sequence<T>.repeat(count: Int) = this.let { seq ->
sequence { repeat(count) { yieldAll(seq) } }
}
Alternatively:
fun <T> Sequence<T>.repeat(count: Int) = generateSequence { this }.take(count).flatten()
bj0
12/11/2024, 5:36 PMbj0
12/11/2024, 5:39 PMKlitos Kyriacou
12/11/2024, 5:55 PMthis@repeat
can only refer to the Sequence<T>.repeat
function. It can't possibly refer to the regular Kotlin repeat
function because its second parameter (i.e. the lambda) doesn't take a receiver - it has only an it
, not a this
.bj0
12/11/2024, 8:50 PMbj0
12/11/2024, 8:51 PMfun <T> Sequence<T>.repeat(count: Int) = sequence { repeat(count) { yieldAll(this@repeat) } }
and the ide (idea ultimate) is highlighting it as in the screenshotephemient
01/01/2025, 7:38 PMsequence { repeat(count) repeat@{ yieldAll(this@repeat) } }
in this case I don't think that's the behavior you want thoughbj0
01/24/2025, 8:54 PM