Created
August 2, 2017 21:47
-
-
Save jdvp/e13424887733e1226abd7c11b2add071 to your computer and use it in GitHub Desktop.
RxJava Fibonacci Sequence
This file contains hidden or 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
public class Utils { | |
public static Observable<Integer> fibonacci(int f1, int f2) { | |
return Observable.range(1, 1). | |
repeat(). | |
scan(new ArrayList<>(Arrays.asList(f1, f2)), | |
(list, tick) -> { | |
list.add(list.get(list.size() - 2) + list.get(list.size() - 1)); | |
list.remove(0); //or else this becomes pretty memory inefficient eventually, right? | |
return list; | |
} | |
).map(list -> list.get(list.size() - 1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maybe instead of using ArrayList, Tuple will be more readable?