Last active
December 3, 2016 15:45
-
-
Save colintheshots/f09659edb4c2e64d803a to your computer and use it in GitHub Desktop.
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
package com.octo.android.robospice.sample.retrofit; | |
import rx.Observable; | |
import rx.Subscriber; | |
import rx.functions.Action1; | |
/** | |
* Created by colintheshots on 6/16/14. | |
*/ | |
public class RxFibonacci { | |
public static void main(String[] args) { | |
rxFibN(20).subscribe(new Action1<Integer>() { | |
@Override | |
public void call(Integer result) { | |
System.out.println(result); | |
} | |
}); | |
} | |
static Observable<Integer> rxFibN(final int n) { | |
return Observable.create(new Observable.OnSubscribe<Integer>() { | |
@Override | |
public void call(Subscriber<? super Integer> subscriber) { | |
int [] n1 = {0,1}; | |
for (int i = 1; i<n; i++) { | |
n1 = new int[]{n1[1], n1[1] + n1[0]}; | |
subscriber.onNext(n1[1]); | |
} | |
subscriber.onCompleted(); | |
} | |
}).last(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Colin,
I tried implementing fib sequence for kicks on RxJava2, here's what I came up with:
This allows me to request the number of fib sequences as desired without having to create a new stream. Thought I'd post it here as I've found this when looking for an example (I was trying to find how to request n items idiomatically.)