Last active
July 30, 2017 03:20
-
-
Save MarioAriasC/5552074 to your computer and use it in GitHub Desktop.
First play with Rx for Kotlin
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
import rx.Observable | |
import rx.Subscription | |
import rx.util.functions.Action1 | |
/** | |
* Created by IntelliJ IDEA. | |
* @author Mario Arias | |
* Date: 9/05/13 | |
* Time: 21:11 | |
*/ | |
fun main(args: Array<String>) { | |
hello("uno", "dos", "tres") | |
} | |
fun hello(vararg names: String?) { | |
//Extension function + Function literal | |
names.toObservable().subscribeF { println("Hello ${it}!") } | |
//Extension function + SAM | |
names.toObservable().subscribe(Action1<String>{ | |
println("Hello ${it}!") | |
}) | |
} | |
fun<T> Array<T?>.toObservable(): Observable<T> { | |
return Observable.toObservable(*this)!! | |
} | |
fun<T> Observable<T>.subscribeF(action: (T)->Unit): Subscription { | |
return this.subscribe(Action1<T>{ | |
action(it!!) | |
})!! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment