Rei Matsushita
August 10, 2016
520

# Rx入門

Reactive Extensions入門。社内勉強会用。

August 10, 2016

## Transcript

ୡʹͳΓ͖Εͳ͍

5. ### ࿩͸໭ͬͯ3Y w 3Y͸ঢ়ଶͷมԽΛ4USFBNͱͯ͠දݱ͢Δ w ొ৔͢Δͷ͸0CTFSWBCMFͱ0CTFSWFSΠϯλʔϑΣʔεɻ0CTFSWBCMF͸ྲྀΕͯ ͘Δσʔλɺ0CTFSWFS͸ྲྀΕͯ͘Δσʔλͷ؍ଌऀ w Ұ൪؆୯ͳϦετͷத਎Λ4USFBNʹม׵ͯ͠දࣔ͢Δྫʢ+BWBʣ -JTU*OUFHFSMJTU"SSBZTBT-JTU 

    0CTFSWBCMFGSPN MJTU TVCTDSJCF 4ZTUFNPVUQSJOUMO     
6. ### 0QFSBUPSͰ஋ΛՃ޻͢Δ w ྲྀΕͯ͘ΔσʔλΛՃ޻͢Δྫʢ+BWBʣ w ΦϖϨʔλͷҰཡIUUQSFBDUJWFYJPEPDVNFOUBUJPOPQFSBUPSTIUNM List<Integer> list = Arrays.asList(1, 2,

3, 4, 5, 6);  Observable.from(list)  .filter(v -> (v % 2) == 1)  .map(v -> v * 2)  .subscribe(System.out::println); 2 6 10
7. ### 0CTFSWBCMFΛ߹੒͢Δ Observable<Integer> stream1 = Observable.from(Arrays.asList(1, 2, 3));  Observable<Integer> stream2 =

Observable.from(Arrays.asList(10, 20, 30));  Observable.concat(stream1, stream2).subscribe(System.out::println); 1 2 3 10 20 30 Observable<Integer> stream1 = Observable.from(Arrays.asList(1, 2, 3));  Observable<Integer> stream2 = Observable.from(Arrays.asList(10, 20, 30));  Observable.zip(stream1, stream2, ((v1, v2) -> {  return v1 + v2;  })).subscribe(System.out::println); 11 22 33
8. ### ΠϕϯτΛ0CTFSWBCMFͱͯ͠ѻ͏ w ͜͜·Ͱͷ಺༰͸-JTUΛ4USFBNͱͯ͠ѻ͚ͬͨͩ w ϢʔβʔͷΠϕϯτΛ0CTBSWBCMFͰྲྀ͢ private Subscription subscription;  @Override  public

View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { … // ͜͜ͰϘλϯΛ؂ࢹͯ͠ΠϕϯτΛྲྀ͢StreamΛ࡞Δ. subscription = RxView.clicks(view.findViewById(R.id.button)).map(v -> 1).subscribe(v -> {  System.out.println("ԡ͞Εͯ " + v + "͕ྲྀΕ͖ͯͨΑ");  }); } @Override  public void onDestroyView() {  super.onDestroyView();  subscription.unsubscribe(); // View͕ഁغ͞ΕΔλΠϛϯάͰStreamͷߪಡΛղআͯ͠؂ࢹΛղ͘  subscription = null;  }