fix broken links for kotlin web site (#628)
* fix broken links for Kotlin web site
diff --git a/docs/select-expression.md b/docs/select-expression.md
index 9bfe56e..f41eabc 100644
--- a/docs/select-expression.md
+++ b/docs/select-expression.md
@@ -54,6 +54,8 @@
import kotlin.coroutines.experimental.*
-->
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun CoroutineScope.fizz() = produce<String> {
while (true) { // sends "Fizz" every 300 ms
@@ -63,8 +65,12 @@
}
```
+</div>
+
And the `buzz` produces "Buzz!" string every 500 ms:
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun CoroutineScope.buzz() = produce<String> {
while (true) { // sends "Buzz!" every 500 ms
@@ -74,10 +80,14 @@
}
```
+</div>
+
Using [receive][ReceiveChannel.receive] suspending function we can receive _either_ from one channel or the
other. But [select] expression allows us to receive from _both_ simultaneously using its
[onReceive][ReceiveChannel.onReceive] clauses:
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
suspend fun selectFizzBuzz(fizz: ReceiveChannel<String>, buzz: ReceiveChannel<String>) {
select<Unit> { // <Unit> means that this select expression does not produce any result
@@ -91,8 +101,12 @@
}
```
+</div>
+
Let us run it all seven times:
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun main(args: Array<String>) = runBlocking<Unit> {
val fizz = fizz()
@@ -104,6 +118,8 @@
}
```
+</div>
+
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-01.kt)
The result of this code is:
@@ -131,6 +147,8 @@
import kotlin.coroutines.experimental.*
-->
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): String =
select<String> {
@@ -149,9 +167,13 @@
}
```
+</div>
+
Let's use it with channel `a` that produces "Hello" string four times and
channel `b` that produces "World" four times:
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun main(args: Array<String>) = runBlocking<Unit> {
val a = produce<String> {
@@ -167,6 +189,8 @@
}
```
+</div>
+
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-02.kt)
The result of this code is quite interesting, so we'll analyze it in mode detail:
@@ -206,6 +230,8 @@
import kotlin.coroutines.experimental.*
-->
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun CoroutineScope.produceNumbers(side: SendChannel<Int>) = produce<Int> {
for (num in 1..10) { // produce 10 numbers from 1 to 10
@@ -218,8 +244,12 @@
}
```
+</div>
+
Consumer is going to be quite slow, taking 250 ms to process each number:
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun main(args: Array<String>) = runBlocking<Unit> {
val side = Channel<Int>() // allocate side channel
@@ -233,7 +263,9 @@
println("Done consuming")
coroutineContext.cancelChildren()
}
-```
+```
+
+</div>
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-03.kt)
@@ -265,6 +297,8 @@
import java.util.*
-->
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun CoroutineScope.asyncString(time: Int) = async {
delay(time.toLong())
@@ -272,8 +306,12 @@
}
```
+</div>
+
Let us start a dozen of them with a random delay.
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun CoroutineScope.asyncStringsList(): List<Deferred<String>> {
val random = Random(3)
@@ -281,11 +319,15 @@
}
```
+</div>
+
Now the main function awaits for the first of them to complete and counts the number of deferred values
that are still active. Note, that we've used here the fact that `select` expression is a Kotlin DSL,
so we can provide clauses for it using an arbitrary code. In this case we iterate over a list
of deferred values to provide `onAwait` clause for each deferred value.
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun main(args: Array<String>) = runBlocking<Unit> {
val list = asyncStringsList()
@@ -302,6 +344,8 @@
}
```
+</div>
+
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-04.kt)
The output is:
@@ -323,6 +367,8 @@
import kotlin.coroutines.experimental.*
-->
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun CoroutineScope.switchMapDeferreds(input: ReceiveChannel<Deferred<String>>) = produce<String> {
var current = input.receive() // start with first received deferred value
@@ -346,8 +392,13 @@
}
```
+</div>
+
To test it, we'll use a simple async function that resolves to a specified string after a specified time:
+
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun CoroutineScope.asyncString(str: String, time: Long) = async {
delay(time)
@@ -355,9 +406,13 @@
}
```
+</div>
+
The main function just launches a coroutine to print results of `switchMapDeferreds` and sends some test
data to it:
+<div class="sample" markdown="1" theme="idea" data-highlight-only>
+
```kotlin
fun main(args: Array<String>) = runBlocking<Unit> {
val chan = Channel<Deferred<String>>() // the channel for test
@@ -378,6 +433,8 @@
}
```
+</div>
+
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-05.kt)
The result of this code: