Skip to content

Commit a4196d2

Browse files
Add playground files for flow terminal operators
1 parent 37c65b2 commit a4196d2

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.terminal_operators
22

3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.flow
5+
36
fun main() {
47

8+
val flow = flow {
9+
delay(100)
10+
11+
println("Emitting first value")
12+
emit(1)
13+
14+
delay(100)
15+
16+
println("Emitting second value")
17+
emit(2)
18+
}
19+
20+
val list = buildList {
21+
add(1)
22+
println("add 1 to list")
523

24+
add(2)
25+
println("add 2 to list")
26+
}
627

728
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.terminal_operators
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.flow
5+
import kotlinx.coroutines.runBlocking
6+
7+
fun main() {
8+
9+
val flow = flow {
10+
delay(100)
11+
12+
println("Emitting first value")
13+
emit(1)
14+
15+
delay(100)
16+
17+
println("Emitting second value")
18+
emit(2)
19+
}
20+
21+
runBlocking {
22+
flow.collect { receivedValue ->
23+
println("Received value $receivedValue")
24+
}
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.terminal_operators
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.first
5+
import kotlinx.coroutines.flow.flow
6+
import kotlinx.coroutines.runBlocking
7+
8+
fun main() {
9+
10+
val flow = flow {
11+
delay(100)
12+
13+
println("Emitting first value")
14+
emit(1)
15+
16+
delay(100)
17+
18+
println("Emitting second value")
19+
emit(2)
20+
}
21+
22+
runBlocking {
23+
val item = flow.first { it > 1 }
24+
println("Received $item")
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.terminal_operators
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.flow
5+
import kotlinx.coroutines.flow.last
6+
import kotlinx.coroutines.runBlocking
7+
8+
fun main() {
9+
10+
val flow = flow {
11+
delay(100)
12+
13+
println("Emitting first value")
14+
emit(1)
15+
16+
delay(100)
17+
18+
println("Emitting second value")
19+
emit(2)
20+
}
21+
22+
runBlocking {
23+
val item = flow.last()
24+
println("Received $item")
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.terminal_operators
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.flow
5+
import kotlinx.coroutines.flow.single
6+
import kotlinx.coroutines.runBlocking
7+
8+
fun main() {
9+
10+
val flow = flow {
11+
delay(100)
12+
13+
println("Emitting first value")
14+
emit(1)
15+
16+
delay(100)
17+
18+
println("Emitting second value")
19+
// emit(2)
20+
}
21+
22+
runBlocking {
23+
val item = flow.single()
24+
println("Received $item")
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.terminal_operators
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.flow
5+
import kotlinx.coroutines.flow.toList
6+
import kotlinx.coroutines.flow.toSet
7+
import kotlinx.coroutines.runBlocking
8+
9+
fun main() {
10+
11+
val flow = flow {
12+
delay(100)
13+
14+
println("Emitting first value")
15+
emit(1)
16+
17+
delay(100)
18+
19+
println("Emitting second value")
20+
emit(2)
21+
}
22+
23+
runBlocking {
24+
val item = flow.toSet()
25+
println("Received $item")
26+
}
27+
28+
runBlocking {
29+
val item = flow.toList()
30+
println("Received $item")
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.terminal_operators
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.flow
5+
import kotlinx.coroutines.flow.fold
6+
import kotlinx.coroutines.runBlocking
7+
8+
fun main() {
9+
10+
val flow = flow {
11+
delay(100)
12+
13+
println("Emitting first value")
14+
emit(1)
15+
16+
delay(100)
17+
18+
println("Emitting second value")
19+
emit(2)
20+
}
21+
22+
runBlocking {
23+
val item = flow.fold(5) { accumulator, emittedItem ->
24+
accumulator + emittedItem
25+
}
26+
println("Received $item")
27+
}
28+
}

0 commit comments

Comments
 (0)