# Sequence chunk or window with predicate?

**URL:** <https://discuss.kotlinlang.org/t/sequence-chunk-or-window-with-predicate/7339>\
**Category:** Support\
**Created:** [April 6, 2018, 1:22pm UTC](https://discuss.kotlinlang.org/t/sequence-chunk-or-window-with-predicate/7339 "2018-04-06T13:22:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [April 6, 2018, 1:22pm UTC](https://discuss.kotlinlang.org/t/sequence-chunk-or-window-with-predicate/7339/1 "2018-04-06T13:22:28Z")

</div>

Say if I have a file containing a long list of family data , starting with F (father) , M (mother) , and optional unknown length S (son) , D (daughter) data , like this :

```
F ...
M ...
S ...
F ...
M ...
S ...
D ...
F ...
M ...
S ...
S ...
S ...
F ...
M ...
D ...
D ...

```

I want to parse this file into a data structure containing father , mother , and list of children…

```
val iStream: InputStream = javaClass.getResourceAsStream(filename)
iStream.bufferedReader(Charsets.UTF_8).useLines { sequence -> 
}

```

I found it is very hard to use sequence or iterator to accomplish this job . Because the children size is unknown , I cannot use `seq.chunk(n:Int)` or `seq.windowed()` to do it.

As to iterator , I should depend on (peeking) next line , checking if it’s starting with ‘F’ , meaning new family . It will break `while(iterator.hasNext())` …

I try to implement a custom Iterator , but don’t know how to feed it into a sequence or an iterator.

Any ideas ? (except reading all into List , which consumes memory )

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [April 6, 2018, 2:54pm UTC](https://discuss.kotlinlang.org/t/sequence-chunk-or-window-with-predicate/7339/2 "2018-04-06T14:54:32Z")

</div>

With the help of coroutines you can chunk a sequence by families the same way as you do it with lists, but producing a lazy sequence instead, for example:

```auto
fun chunkByFamily(names: Sequence<String>): Sequence<List<String>> = sequence {
    val currentFamily = mutableListOf<String>()
    for (name in names) {
        if (startsNewFamily(name)) {
            yield(currentFamily.toList())
            currentFamily.clear()
        }
        currentFamily += name
    }
    yield(currentFamily)
}

```

---

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [April 6, 2018, 4:12pm UTC](https://discuss.kotlinlang.org/t/sequence-chunk-or-window-with-predicate/7339/3 "2018-04-06T16:12:46Z")

</div>

Wow , what a fantastic solution 👍👍👍  
Thanks, it works !  
( I was stuck in nested iterator like this : [java - How to implement this FilteringIterator? - Stack Overflow](https://stackoverflow.com/a/5475086/298430) )
