Last active
August 14, 2017 10:01
-
-
Save benjchristensen/a962c4bfa8adae286daf to your computer and use it in GitHub Desktop.
Fibonacci + Rx
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 java.util.HashMap; | |
import rx.Observable; | |
import rx.Subscriber; | |
public class Fibonacci { | |
public static void main(String... args) { | |
// via Observable.create | |
fib.take(13).forEach(System.out::println); | |
// via scan | |
Observable.range(0, 10).scan(new HashMap<String, Integer>(), (m, n) -> { | |
Integer f1 = m.get("f1"); | |
Integer f2 = m.get("f2"); | |
f1 = f1 == null ? 0 : f1; | |
f2 = f2 == null ? 1 : f2; | |
int fn = f1 + f2; | |
m.put("f1", f2); | |
m.put("f2", fn); | |
return m; | |
}) | |
.filter(m -> m.size() == 2) | |
.map(m -> m.get("f1") + m.get("f2")) | |
.startWith(Observable.from(0, 1, 1)) | |
.forEach(System.out::println); | |
} | |
static Observable<Integer> fib = Observable.create((Subscriber<? super Integer> s) -> { | |
int f1 = 0, f2 = 1, fn; | |
s.onNext(0); | |
s.onNext(1); | |
while (!s.isUnsubscribed()) { | |
fn = f1 + f2; | |
f1 = f2; | |
f2 = fn; | |
s.onNext(fn); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This outputs: