Created
August 26, 2022 19:31
-
-
Save omkar-tenkale/6e6ff3196de59eacb4aedb1c36a1a59f 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
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