Youssef Shoaib [MOD]
03/24/2023, 1:16 PMforEachWithNext
, analogous to zipWithNext
. Could be a nice addition to stdlib. Here's an impl based on `zipWithNext`:
inline fun <T> Iterable<T>.forEachWithNext(block: (a: T, b: T) -> Unit) {
val iterator = iterator()
if (!iterator.hasNext()) return
var current = iterator.next()
while (iterator.hasNext()) {
val next = iterator.next()
block(current, next)
current = next
}
}
jw
03/24/2023, 1:23 PMwindowed(2)
, no?jw
03/24/2023, 1:26 PMYoussef Shoaib [MOD]
03/24/2023, 1:54 PMzipWithNext().forEach { (cur, next) -> }
or windowed(2).forEach { (cur, next) -> }
but those objects in the middle are totally unnecessary. It's similar to how forEachWithIndex
exists even though withIndex().forEach
is semantically equivalent to it.jw
03/24/2023, 2:01 PMjw
03/24/2023, 2:03 PMYoussef Shoaib [MOD]
03/24/2023, 2:05 PMwithIndex()
and zipWithNext()
be optimized by default, hence eliminating the need for all those extra functions. Thank you Jake!ephemient
03/24/2023, 4:02 PMYoussef Shoaib [MOD]
03/24/2023, 4:44 PM