-
-
Save NoNews/59b65b0b4bf88eeef740c7b0534680e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flowOf("Bojack Horseman!") | |
.map { value -> "$value Modified1" } | |
.map { value -> "$value Modified2" } | |
.map { value -> "$value Modified3" } | |
.collect(object : Collector<String> { | |
override fun emit(value: String) { | |
//value will be: Bojack Horseman Modified1 Modified2 Modified3 | |
} | |
}) | |
private inline fun <T, R> Flow<T>.map(crossinline transform: (value: T) -> R): Flow<R> { | |
val upstream = this | |
return object : Flow<R> { | |
override fun collect(collector: Collector<R>) { | |
upstream.collect(object : Collector<T> { | |
override fun emit(value: T) { | |
val newValue = transform.invoke(value) | |
collector.emit(newValue) | |
} | |
}) | |
} | |
} | |
} | |
fun flowOf(value: String): Flow<String> = object : Flow<String> { | |
override fun collect(collector: Collector<String>) { | |
collector.emit(value) | |
} | |
} | |
interface Collector<in T> { | |
fun emit(value: T) | |
} | |
interface Flow<out T> { | |
fun collect(collector: Collector<T>) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment