Created
April 16, 2018 04:56
-
-
Save SwiftyWang/36bfa89eed48dcc4de20bb248c88e49b to your computer and use it in GitHub Desktop.
Best Rx Fibonacci
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 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