Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bad6b20

Browse files
committed
sequence, channel, and flow examples.
1 parent d875dfd commit bad6b20

File tree

51 files changed

+654
-106
lines changed

Some content is hidden

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

51 files changed

+654
-106
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
// This file was automatically generated from channels.md by Knit tool. Do not edit.
6+
package com.smarttoolfactory.tutorial1_1coroutinesbasics.playground
7+
8+
import kotlinx.coroutines.channels.Channel
9+
import kotlinx.coroutines.launch
10+
import kotlinx.coroutines.runBlocking
11+
12+
/*
13+
Rendezvous channel example
14+
*/
15+
16+
17+
fun main() = runBlocking {
18+
val channel = Channel<String>()
19+
20+
launch {
21+
println("Sending A1")
22+
channel.send("A1")
23+
println("Sending A2")
24+
channel.send("A2")
25+
println("A done")
26+
}
27+
28+
launch {
29+
println("Sending B1")
30+
channel.send("B1")
31+
println("B done")
32+
}
33+
34+
launch {
35+
repeat(3) {
36+
println("Calling receive()")
37+
val x = channel.receive()
38+
println("receive is done $x")
39+
}
40+
}
41+
42+
println("Done!")
43+
}
44+
45+
/*
46+
* Output :
47+
48+
Done!
49+
Sending A1
50+
Sending B1
51+
Calling receive()
52+
receive is done A1
53+
Calling receive()
54+
receive is done B1
55+
Calling receive()
56+
Sending A2
57+
A done
58+
B done
59+
receive is done A2
60+
61+
* */
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@
66
package com.smarttoolfactory.tutorial1_1coroutinesbasics.playground
77

88
import kotlinx.coroutines.channels.Channel
9+
import kotlinx.coroutines.delay
910
import kotlinx.coroutines.launch
1011
import kotlinx.coroutines.runBlocking
1112

12-
fun main() = runBlocking {
13+
fun main() = runBlocking { // coroutine 1
14+
1315
val channel = Channel<Int>()
14-
launch {
16+
launch {// coroutine 2
1517
// this might be heavy CPU-consuming computation or async logic, we'll just send five squares
16-
for (x in 1..5) channel.send(x * x)
18+
for (x in 1..5) {
19+
println("send $x ")
20+
channel.send(x * x)
21+
}
22+
1723
}
1824
// here we print five received integers:
19-
repeat(5) { println(channel.receive()) }
25+
repeat(5) {
26+
delay(3000)
27+
println(channel.receive())
28+
}
2029
println("Done!")
2130
}
31+
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ import kotlinx.coroutines.launch
1010
import kotlinx.coroutines.runBlocking
1111

1212
fun main() = runBlocking {
13+
1314
val channel = Channel<Int>()
1415
launch {
15-
for (x in 1..5) channel.send(x * x)
16+
for (x in 1..5)
17+
channel.send(x * x)
1618
channel.close() // we're done sending
1719
}
20+
1821
// here we print received values using `for` loop (until the channel is closed)
22+
// 1st way of receiving data from channel
1923
for (y in channel) println(y)
24+
// 2nd way of receiving data from channel
25+
/*repeat(5) {
26+
println(channel.receive()) }*/
27+
2028
println("Done!")
2129
}
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,37 @@ import kotlinx.coroutines.CoroutineScope
99
import kotlinx.coroutines.channels.ReceiveChannel
1010
import kotlinx.coroutines.channels.consumeEach
1111
import kotlinx.coroutines.channels.produce
12+
import kotlinx.coroutines.delay
1213
import kotlinx.coroutines.runBlocking
1314

1415
fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
1516
for (x in 1..5) send(x * x)
1617
}
1718

1819
fun main() = runBlocking {
20+
21+
// 1st way of sending integers to the channel.
22+
/*val squares = Channel<Int>()
23+
launch {
24+
for (x in 1..5)
25+
squares.send(x * x)
26+
squares.close() // we're done sending
27+
}*/
28+
// 2nd way of sending integers to the channel.
1929
val squares = produceSquares()
20-
squares.consumeEach { println(it) }
30+
31+
32+
// 1st way of receiving integers from the channel.
33+
squares.consumeEach {
34+
delay(3000)
35+
println(it)
36+
}
37+
// 2nd way of receiving integers from the channel.
38+
/*repeat(5) {
39+
delay(3000)
40+
println(squares.receive())
41+
}*/
42+
2143
println("Done!")
2244
}
45+

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/playground/example-flow-01.kt‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/playground/example-flow-02.kt‎

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
// This file was automatically generated from flow.md by Knit tool. Do not edit.
6+
package com.smarttoolfactory.tutorial1_1coroutinesbasics.playground
7+
8+
fun main() {
9+
10+
withoutSequence()
11+
12+
withSequence()
13+
}
14+
15+
private fun withoutSequence() {
16+
val result = listOf("a", "b", "ac", "d", "e", "f", "g", "h", "i", "j", "ak")
17+
.filter {
18+
println("filter: $it")
19+
it.startsWith("a", ignoreCase = true)
20+
}
21+
.map {
22+
println("map: $it")
23+
it.toUpperCase()
24+
}
25+
.take(2)
26+
.toList()
27+
28+
println("size: ${result.size}")
29+
}
30+
31+
private fun withSequence() {
32+
val result = listOf("a", "b", "ac", "d", "e", "f", "g", "h", "i", "j", "ak")
33+
.asSequence()
34+
.filter {
35+
println("filter: $it")
36+
it.startsWith("a", ignoreCase = true)
37+
}
38+
.map {
39+
println("map: $it")
40+
it.toUpperCase()
41+
}
42+
.take(2)
43+
.toList()
44+
45+
println("size: ${result.size}")
46+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fun numbers(): Flow<Int> = flow {
1717
emit(2)
1818
println("This line will not execute")
1919
emit(3)
20+
} catch (ex: Exception) {
21+
println(ex.message)
2022
} finally {
2123
println("Finally in numbers")
2224
}

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/playground/example-flow-11.kt‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,20 @@ import kotlinx.coroutines.flow.reduce
1111
import kotlinx.coroutines.runBlocking
1212

1313
fun main() = runBlocking<Unit> {
14-
val sum = (1..5).asFlow()
14+
val sum1 = (1..5).asFlow()
15+
.reduce { a, b ->
16+
println("$a + $b = ${a + b}")
17+
a + b
18+
} // sum them (terminal operator)
19+
println(sum1)
20+
21+
println("----")
22+
23+
val sum2 = (1..5).asFlow()
1524
.map { it * it } // squares of numbers from 1 to 5
16-
.reduce { a, b -> a + b } // sum them (terminal operator)
17-
println(sum)
25+
.reduce { a, b ->
26+
println("$a + $b = ${a + b}")
27+
a + b
28+
} // sum them (terminal operator)
29+
println(sum2)
1830
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /