Skip to content

Instantly share code, notes, and snippets.

@omkar-tenkale
Created August 26, 2022 19:31
Embed
What would you like to do?
interface MFlow<T>{
fun collect(collector: MCollector<T>)
}
interface MCollector<T>{
fun onEmit(item :T)
}
// fun mFlowOf(value: String):MFlow<String>{
// return object :MFlow<String>{
// override fun collect(collector: MCollector<String>) {
// collector.onEmit(value)
// }
// }
// }
fun <T> mFlowOf(value: T):MFlow<T>{
return object :MFlow<T>{
override fun collect(collector: MCollector<T>) {
collector.onEmit(value)
}
}
}
fun <T,R> MFlow<T>.map(transformer : (T)-> R):MFlow<R>{
val upstream = this
return object :MFlow<R>{
override fun collect(collector: MCollector<R>) {
upstream.collect(object :MCollector<T>{
override fun onEmit(item: T) {
collector.onEmit(transformer(item))
}
})
}
}
}
fun <T> mFlowOf(block: MCollector<T>.()-> Unit):MFlow<T>{
return object :MFlow<T>{
override fun collect(collector: MCollector<T>) {
block(collector)
}
}
}
fun test():MFlow<String>{
return mFlowOf{
onEmit("LALAL")
onEmit("LULULU")
onEmit("LELELEL")
}
mFlowOf("")
.map{ it + "<"}
.collect(object : MCollector<String>{
override fun onEmit(item: String) {
TODO("COLLECTED $item")
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment