Skip to content

Instantly share code, notes, and snippets.

@SwiftyWang
Created April 16, 2018 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SwiftyWang/36bfa89eed48dcc4de20bb248c88e49b to your computer and use it in GitHub Desktop.
Save SwiftyWang/36bfa89eed48dcc4de20bb248c88e49b to your computer and use it in GitHub Desktop.
Best Rx Fibonacci
import io.reactivex.Observable
import org.junit.Test
import java.util.*
class Fibonacci {
fun main(args: Array<String>) {
val fibonacci = fibonacci(7)
val disposable = fibonacci.subscribe { integer -> println(integer) }
}
private fun fibonacci(index: Int): Observable<Int> {
return Observable.range(1, 1).repeat(index.toLong()).scan(ArrayList(Arrays.asList(0, 1))
) { list, _ ->
list.add(list[list.size - 2] + list[list.size - 1])
list.removeAt(0)
list
}.map { list -> list[list.size - 1] }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment