https://kotlinlang.org logo
Title
n

Nikita Khlebushkin

10/02/2018, 9:34 AM
What is the advantage of using
asSequence
before `filter`s and `map`s?
k

karelpeeters

10/02/2018, 9:37 AM
Otherwise you're creating a temporary collection between every step.
n

Nikita Khlebushkin

10/02/2018, 9:38 AM
@karelpeeters thanks!
u

uhe

10/02/2018, 11:11 AM
I still find that new inspection a bit annoying and turned it off. 😛
n

nil2l

10/02/2018, 11:36 AM
There are good rules to decide when to use
asSequence
https://proandroiddev.com/sequences-a-pragmatic-approach-9d4296086a9d
💯 1
s

Sam

10/02/2018, 12:59 PM
And it behaves more like streams, an event flows through the entire steps before the next event is processed. Ex if you have take (2) and if your original input list had 1000 items, you would get performance improvements as well.
It does lack one feature when compared to Java streams, ability to schedule on multiple CPUs
n

nil2l

10/02/2018, 1:02 PM
Yes. Compare this:
(0..6000000).toList().find { it < 100 }
(about 3 seconds) and this:
(0..6000000).asSequence().find { it < 100 }
(a moment, asSequence is not needed here though)
a

adam-mcneilly

10/02/2018, 1:11 PM
Wow this is one of the first discussions where sequences really clicked for me. Thanks Alexander! 😄
🙂 1