Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava での Observable のつくりかた

842aaadff6e13fbf372c9b17b49b031c?s=47 Pine Mizune
November 18, 2015

RxJava での Observable のつくりかた

社内 ReactiveX 勉強会での発表資料
ソースコード: https://github.com/pine613/rxnight

842aaadff6e13fbf372c9b17b49b031c?s=128

Pine Mizune

November 18, 2015
Tweet

More Decks by Pine Mizune

Other Decks in Programming

Transcript

  1. RxJava での Observable のつくりかた Pine Mizune

  2. もくじ • just • empty • from • range •

    repeat • create
  3. ソースコードはこちら • Java 7 ~ • ./gradlew run • https://github.com/pine613/rxnight

  4. just (1/2) Observable .just("yano") .subscribe(new Action1<String>() { @Override public void

    call(String name) { // Hello yano System.out.println("Hello, " + name + "!"); } });
  5. just (2/2) Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(final Subscriber<?

    super String> observer) { observer.onNext("yano"); observer.onCompleted(); } }).subscribe(new Action1<String>() { @Override public void call(String name) { System.out.println("Hello, " + name + "!"); // Hello yano } });
  6. empty Observable.<String>empty().doOnCompleted(new Action0() { @Override public void call() { System.out.println("Completed");

    } }).subscribe(new Action1<String>() { @Override public void call(String name) { // not performed ... System.out.println("Hello, " + name + "!"); } });
  7. range Observable .range(1, 10) .subscribe(new Action1<Integer>() { @Override public void

    call(Integer value) { System.out.println(value); // 1, 2, ..., 10 } });
  8. range Observable.range(1, 10).map(new Func1<Integer, Integer>() { @Override public Integer call(Integer

    value) { return value * 2; // 2, 4, 6, ... } }) .reduce(0, new Func2<Integer, Integer, Integer>() { // sum @Override public Integer call(Integer sum, Integer value) { return sum + value; } }).subscribe(new Action1<Integer>() { @Override public void call(Integer value) { System.out.println(value); // 110 } });
  9. repeat (1/2) Observable .just(1) .repeat(3) .subscribe(new Action1<Integer>() { @Override public

    void call(Integer value) { System.out.println(value); // 1, 1, 1 } });
  10. repeat (2/2) Observable .from(new Boolean[]{ true, false }) .repeat(3) .subscribe(new

    Action1<Boolean>() { @Override public void call(Boolean value) { System.out.println(value); // true, false, ... } });
  11. create (1/2) Observable<Integer> random = Observable.create( new Observable.OnSubscribe<Integer>() { @Override

    public void call(final Subscriber<? super Integer> observer) { Random random = new Random(); observer.onNext(random.nextInt(10)); // 0 ~ 9 observer.onCompleted(); } });
  12. create (2/2) Observable<Integer> random = Observable.create(new Observable.OnSubscribe<Integer>() { @Override public

    void call(final Subscriber<? super Integer> observer) { Random random = new Random(); observer.onNext(random.nextInt(10)); // 0 ~ 9 observer.onCompleted(); } }); random.repeat().take(10).subscribe(new Action1<Integer>() { @Override public void call(Integer value) { System.out.println(value); // 7, 0, 8, ... } });
  13. THANKS