Skip to content

Commit 0abfcc4

Browse files
committed
WIP flows
1 parent 1755487 commit 0abfcc4

File tree

48 files changed

+531
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+531
-0
lines changed

Flow/Buffering/Buffer/src/Main.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import kotlinx.coroutines.*
2+
import kotlinx.coroutines.flow.*
3+
import kotlin.system.measureTimeMillis
4+
5+
fun main() = runBlocking {
6+
val numbers = flow {
7+
for (i in 1..5) {
8+
delay(100)
9+
emit(i)
10+
}
11+
}
12+
13+
val time = measureTimeMillis {
14+
numbers.buffer().collect {
15+
delay(300)
16+
println(it)
17+
}
18+
}
19+
20+
println("time used: $time ms")
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: edu
2+
files:
3+
- name: src/Main.kt
4+
visible: true
5+
placeholders:
6+
- offset: 698
7+
length: 9
8+
placeholder_text: ""
9+
- name: test/Test.kt
10+
visible: false

Flow/Buffering/Buffer/task.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Buffer
2+
3+
Assume that the production and the consumption of values takes both some time. Normally emitting new values
4+
will be triggered if the collection of the last one is finished.
5+
So we need in this example for each value approx. 400ms. With *buffer* we can collect the next value(s) before the
6+
current value is processed.
7+
8+
Try it out.

Flow/Buffering/Buffer/test/Test.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import org.junit.jupiter.api.Test
2+
import kotlin.system.measureTimeMillis
3+
import kotlin.test.assertTrue
4+
5+
6+
class Test {
7+
@Test
8+
fun testSolution() {
9+
val time = measureTimeMillis { main() }
10+
11+
assertTrue(time < 2000)
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import kotlinx.coroutines.*
2+
import kotlinx.coroutines.flow.*
3+
4+
fun main() = runBlocking {
5+
val numbers = flow {
6+
for (i in 1..5) {
7+
delay(100)
8+
emit(i)
9+
}
10+
}
11+
12+
numbers.conflate().collect {
13+
delay(300)
14+
println(it)
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: output
2+
files:
3+
- name: src/Main.kt
4+
visible: true
5+
placeholders:
6+
- offset: 490
7+
length: 11
8+
placeholder_text: ""
9+
- name: test/output.txt
10+
visible: false

Flow/Buffering/Conflation/task.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Conflation
2+
3+
4+
Sometimes it is not necessary to process each value (if they are produced faster than collected) but the
5+
most recent ones.
6+
7+
Use *conflate* to buffer the collection and have a look to the result.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
3
3+
5

Flow/Buffering/Latest/src/Main.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import kotlinx.coroutines.*
2+
import kotlinx.coroutines.flow.*
3+
4+
fun main() = runBlocking {
5+
val numbers = flow {
6+
for (i in 1..5) {
7+
delay(100)
8+
emit(i)
9+
}
10+
}
11+
12+
numbers.collectLatest {
13+
println("collect $it")
14+
delay(300)
15+
println(it)
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: output
2+
files:
3+
- name: src/Main.kt
4+
visible: true
5+
placeholders:
6+
- offset: 524
7+
length: 6
8+
placeholder_text: ""
9+
- name: test/output.txt
10+
visible: false

0 commit comments

Comments
 (0)