Skip to content

Instantly share code, notes, and snippets.

@MarioAriasC
Last active July 30, 2017 03:20
Show Gist options
  • Save MarioAriasC/5552074 to your computer and use it in GitHub Desktop.
Save MarioAriasC/5552074 to your computer and use it in GitHub Desktop.
First play with Rx for Kotlin
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