# How splitWhen works?

**URL:** <https://discuss.akka.io/t/how-splitwhen-works/6601>\
**Category:** Akka Streams & Alpakka\
**Created:** [May 26, 2020, 10:54pm UTC](https://discuss.akka.io/t/how-splitwhen-works/6601 "2020-05-26T22:54:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ailinykh](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@ailinykh](https://discuss.akka.io/u/ailinykh)\
**Post date:** [May 26, 2020, 10:54pm UTC](https://discuss.akka.io/t/how-splitwhen-works/6601/1 "2020-05-26T22:54:36Z")

</div>

Hello everybody!  
I’m playing with akka streams and got something I don’t understands. I have simple code:  
Source(1 to 100)  
.throttle(1, 100.millis)  
.map(x =\> {println("raw "+x);x})  
.groupBy(10, \_ % 2, false)  
.concatSubstreams  
.map(x =\> {println("concat "+x);x})  
.to(Sink.ignore).run()

When I run it I get:  
raw 1  
concat 1  
raw 2

And after this, the app is stuck, which makes perfect sense. ‘concatSubstreams’ tries to read everything from first substream, this substream is never ending, so second substream is backpressed.  
But if I add ‘splitWhen’ right after groupBy it starts working!  
Source(1 to 100)  
.throttle(1, 100.millis)  
.map(x =\> {println("raw "+x);x})  
.groupBy(10, \_ % 2, false)  
.splitWhen(\_ =\> false)  
.concatSubstreams  
.map(x =\> {println("concat "+x);x})  
.to(Sink.ignore).run()

raw 1  
concat 1  
raw 2  
concat 2  
raw 3  
concat 3  
raw 4  
concat 4  
raw 5  
…

‘splitWhen’ actually doesn’t split anything, the function I pass always return false. So, there should be the same never ending stream. Why it works now? What do i miss?

Thank you,  
Andrey

---

<div class="post-metadata">

**Author:** ![ennru](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/ennru/32/1495_2.png) [@ennru](https://discuss.akka.io/u/ennru)\
**Post date:** [June 1, 2020, 12:52pm UTC](https://discuss.akka.io/t/how-splitwhen-works/6601/2 "2020-06-01T12:52:18Z")

</div>

Hi Andrey,

What trips you here is that both `grouped` and `splitWhen` create sub-flows.

When you introduce `splitWhen` the `concatSubstreams` applies to the `splitWhen` substream and the elements in that stream are passed to `Sink.ignore` which makes the backpressure go away.

As there is never more than one substream to this `splitWhen` the `concatSubstreams` does not have to wait for a stream to complete.

Does this explain the inspected behaviour?

Cheers,  
Enno/

---

<div class="post-metadata">

**Author:** ![ailinykh](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@ailinykh](https://discuss.akka.io/u/ailinykh)\
**Post date:** [June 1, 2020, 4:10pm UTC](https://discuss.akka.io/t/how-splitwhen-works/6601/3 "2020-06-01T16:10:18Z")

</div>

Ahh. Substreams. I see now.  
Thank you very much!

Andrey
