mkobit
04/19/2019, 6:27 PMSequence
based on attributes of the data?
for example, if i have
sequenceOf(1,2,3,5,6,8,10,14,15)
i'd like to "merge" contiguous elements together into a sequence that is
sequenceOf([1,2,3], [5,6], [8], [10], [14,15])
i'd like to retain the lazy evaluation, such that [1,2,3]
is yielded only when it is neededShawn
04/19/2019, 6:45 PMmkobit
04/19/2019, 6:46 PMShawn
04/19/2019, 6:46 PMlouiscad
04/19/2019, 7:20 PMfold
do this?elizarov
04/21/2019, 1:26 PMfun Sequence<Int>.contiguous() = sequence<List<Int>> {
val cur = mutableListOf<Int>()
for (x in this@contiguous) {
if (cur.isNotEmpty() && x != cur.last() + 1) {
yield(cur.toList())
cur.clear()
}
cur += x
}
if (cur.isNotEmpty()) yield(cur)
}
mkobit
04/22/2019, 1:39 PMfold
would be terminal and consume all of the elements (unless i'm missing something)
@elizarov that's close to how i did it, except i used Sequence.iterator
with hasNext
as my looping condition
i think your approach is cleaner, and im going to change mine to more closely follow your model
thanks for the help everybody!