• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2019 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 coroutines-guide-reactive.md by Knit tool. Do not edit.
6 package kotlinx.coroutines.rx2.guide.operators02
7 
8 import kotlinx.coroutines.*
9 import kotlinx.coroutines.reactive.*
10 import org.reactivestreams.*
11 import kotlin.coroutines.*
12 
fusedFilterMapnull13 fun <T, R> Publisher<T>.fusedFilterMap(
14     context: CoroutineContext,   // the context to execute this coroutine in
15     predicate: (T) -> Boolean,   // the filter predicate
16     mapper: (T) -> R             // the mapper function
17 ) = publish<R>(context) {
18     collect {                    // collect the source stream
19         if (predicate(it))       // filter part
20             send(mapper(it))     // map part
21     }
22 }
23 
<lambda>null24 fun CoroutineScope.range(start: Int, count: Int) = publish<Int> {
25     for (x in start until start + count) send(x)
26 }
27 
<lambda>null28 fun main() = runBlocking<Unit> {
29    range(1, 5)
30        .fusedFilterMap(Dispatchers.Unconfined, { it % 2 == 0}, { "$it is even" })
31        .collect { println(it) } // print all the resulting strings
32 }
33