https://kotlinlang.org logo
#coroutines
Title
# coroutines
u

ubu

11/01/2019, 11:56 PM
Hi there. I have a
Flow
emitting
1
,
2
,
3
. Is there a way to transform this flow to emit:
[1]
,
[1,2]
,
[1,2,3]
. I know there’s this
fold
operator, but it’s terminal. In my case I need a value to be emitted on each accumulation step. Is there any appropriate operator out-of-the-box?
o

octylFractal

11/02/2019, 12:00 AM
probably not out-of-the-box, as flow is designed to have minimal operators. you should build your own using
transform
scan
operator is close to what you want, but it would include an empty list due to its design
although I suppose you could
scan(...).skip(1)
z

Zach Klippenstein (he/him) [MOD]

11/02/2019, 12:01 AM
Copy code
flow.scan(emptyList<Int>()) { list, value -> list + value }
    .drop(1)
👍 1
o

octylFractal

11/02/2019, 12:01 AM
yes, that
u

ubu

11/02/2019, 12:03 AM
thanks, guys!
5 Views