Hi, does this make sense? I know it's generalizabl...
# getting-started
c
Hi, does this make sense? I know it's generalizable with comparables, I'm just interested if this simple version can be written better.
Copy code
fun Sequence<Int>.isOrderedAscending(): Boolean =
	zipWithNext { prev, next -> prev <= next }
		.fold(true) { acc, value -> acc && value }
s
You could use
windowed
:
Copy code
fun Sequence<Int>.isOrderedAscending(): Boolean =
    this.windowed(2).all { (first, second) -> first <= second }
windowed
returns a
Sequence
of `List`s, which you’re then destructuring in the lambda to access the first and second elements and to do the comparison on all of them.
c
ok, so which is better I wonder:
Copy code
zipWithNext().all { (first, second) -> first <= second }
or
Copy code
windowed(2).all { (first, second) -> first <= second }
zip variant generates pairs, windowed - lists
1️⃣ 1
t
@Czar maybe also:
Copy code
zipWithNext { prev, next -> prev <= next }.all { it }
That way you don't create pairs nor lists, esp. with Sequence.
1
👍 2
c
Cool, looked at the bytecode, makes sense. Thank you!