Michael Böiers
09/29/2021, 11:17 AM(1..n).asSequence()
and it’s really annoying that you always have to clutter the code with the asSequence()
call to make it lazy (read: perform well). EDIT: Yes, that came across the wrong way … lazy evaluation isn’t always better. 🙂Michael Böiers
09/29/2021, 11:29 AMfun Int.untilIncluding(t: Int) = (this..t).asSequence()
but 1.untilIncluding(n)
is still a mouthful compared to 1..n
…Michael Böiers
09/29/2021, 11:31 AMfun Int.lazyRangeTo(t: Int) = (this..t).asSequence()
, that’s actually not bad. Now if there only was syntactical support for it. Something like (1…n)
😉Mărcuţ Andrei
09/29/2021, 1:31 PMfun IntRange.lazyToRange() = (this.start..this.endInclusive).asSequence()
and then to
(1..10).lazyToRange()
?Paul Griffith
09/29/2021, 2:26 PMxrange(start: Int, stop: Int)
🤷rook
09/29/2021, 5:30 PM1 lazyToRange 10
😛Michael Böiers
09/29/2021, 5:33 PMMichael Böiers
09/29/2021, 5:37 PMinfix fun Int.seqTo(to: Int) = (this..to).asSequence()
infix fun Int.seqUntil(to: Int) = (this until to).asSequence()
(1 seqTo 10).forEach { println(it) }
Mărcuţ Andrei
09/29/2021, 6:20 PMephemient
09/29/2021, 6:44 PMMichael Böiers
09/29/2021, 7:09 PMephemient
09/29/2021, 7:15 PMMichael Böiers
09/29/2021, 7:43 PMMichael Böiers
09/29/2021, 7:45 PMprivate fun String.reactOne() = when {
length < 2 -> this
else -> 0.lazyRangeTo(length - 2)
.filter { this[it] == this[it + 1].otherCase() }
.map { withoutSubstring(it, 2) }
.firstOrNull() ?: this
}
Mărcuţ Andrei
09/29/2021, 7:55 PMMichael Böiers
09/29/2021, 8:02 PMMichael Böiers
09/29/2021, 8:11 PMMichael Böiers
09/29/2021, 8:25 PMephemient
09/30/2021, 9:01 AMephemient
09/30/2021, 9:01 AMephemient
09/30/2021, 9:01 AMMichael Böiers
09/30/2021, 9:06 AMephemient
09/30/2021, 9:10 AMobject RangeToSequence {
operator fun Int.rangeTo(Int): Sequence<Int>
// etc.
}
inline fun <T> seq(builder: RangeToSequence.() -> T): T
seq { 1..n }.map {}.filter {}.etc()
could look neat. but maybe too much extra relative to a simple infix fun.Michael Böiers
09/30/2021, 9:07 PMMărcuţ Andrei
10/01/2021, 12:04 PM