Czar
03/19/2020, 6:11 PMfun Sequence<Int>.isOrderedAscending(): Boolean =
zipWithNext { prev, next -> prev <= next }
.fold(true) { acc, value -> acc && value }
Sebastian Aigner
03/19/2020, 6:15 PMwindowed
:
fun Sequence<Int>.isOrderedAscending(): Boolean =
this.windowed(2).all { (first, second) -> first <= second }
Sebastian Aigner
03/19/2020, 6:18 PMwindowed
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.Czar
03/19/2020, 6:29 PMzipWithNext().all { (first, second) -> first <= second }
or
windowed(2).all { (first, second) -> first <= second }
zip variant generates pairs, windowed - listsTsvetan Ovedenski
03/19/2020, 9:35 PMzipWithNext { prev, next -> prev <= next }.all { it }
That way you don't create pairs nor lists, esp. with Sequence.Czar
03/20/2020, 4:12 AM