怖くないReactive Extensions

1d1580fb0945b0ffadff18e28bead3c5?s=47 いも
December 12, 2017

怖くないReactive Extensions

社内の新卒向け座学の資料から社内情報を削ったりしたものです

1d1580fb0945b0ffadff18e28bead3c5?s=128

いも

December 12, 2017
Tweet

Transcript

  1. ReactiveExtensions

  2. Imoto Hiroto (2012~) (2013/1 ~ 2017/2) API minne (2017/3~) Android

    Android https://adarapata.com/
  3. None
  4. ReactiveExtensions(Rx) Rx Rx Scheduler, Hot&Cold, Subject Reactive Streams RP, FRP,

  5. Rx

  6. User[] users = getUsers(); for(User user : users) { if

    (user.getAge() >= 20) { String displayName = user.getName() + ":" user.getAge() System.out.println(displayName); } } Observable.fromArray(getUsers()) .filter(user -> user.getAge() >= 20) .map(user -> user.getName() + ":" user.getAge() + " ") .subscribe(name -> System.out.println(name));
  7. getUserObservable() .map(response -> response.getBody()) .flatMap(user -> getFollowersObservable(user.getId())) .map(response -> response.getBody())

    .subscribe(followersList -> { // }); getUserAsync(userResponse -> { User user = userResponse.getBody(); getFollowersAsync(user.getId(), followersResponse -> { List<User> followers = followersResponse.getBody(); // }) })
  8. private Boolean isClicked = false; button.setOnClickListener(view -> { if(!isClicked) {

    doSomething(); isClicked = true; } } RxView.clicks(button) .take(1) .subscribe(view -> doSomething());
  9. None
  10. Reactive Extensions(Rx) .NET Framework http://reactivex.io/ RxJava,RxJS,RxSwift etc... React.js

  11. int a = 10; int b = a * 2;

    a = 20; System.out.println(b); // => 40 https://www.slideshare.net/ytakano/ss-63454513
  12. Rx LINQ .NET LINQ(Language Integrated Query) Objects, XML, MySQL etc..

    http://www.atmarkit.co.jp/fdotnet/chushin/roadto linq_01/roadtolinq_01_01.html
  13. LINQ (C#) List<User> users; // List<String> youngNames = users.Where(user =>

    user.age < 20 .Select(user => user.name).ToList(); foreach(String n in youngNames) { // Console.WriteLine(n); }
  14. LINQ LINQ Rx LINQ to Events, LINQ to Asynchronous Rx

    LINQ Rx
  15. before System.out.println("Hello World"); after Observable.just("Hello World") .subscribe(System::out::println);

  16. before String hello = "Hello World"; System.out.println(hello.toUpperCase()); // HELLO WORLD

    after Observable.just("Hello World") .map(toUpperCase) // .subscribe(System::out::println); // HELLO WORLD
  17. before after String hello = "Hello World"; if(new Random().nextInt(10) <

    3) { System.out.println(hello.toUpperCase()); // HELLO WORLD } Observable.just("Hello World") .filter(hello -> new Random().nextInt(10) < 3) .map(toUpperCase) // .subscribe(System::out::println); // HELLO WORLD
  18. None
  19. Rx --1---2---3---4--|--> Observable.just(1,2,3,4); // 1,2,3,4

  20. OnNext OnError OnComplete = OnNext OnComplete ↓ ↓ just(1,2,3,4) --1--2--3--4--|-->

  21. (Subscribe) Observable.just("Hello World") .subscribe(System::out::println); just("HelloWorld") ----H---|--> subscribe(println); "Hello World" println

  22. Rx

  23. 2 20 just(10,20,30) ---10----11-----12----|--> map(data * 2) ---20----22-----24----|--> filter(data >

    20) ---------22-----24----|--> Observable.just(10, 11, 12) // .map(data -> data * 2) // .filter(data -> data > 20)//
  24. Rx

  25. Rx

  26. List<User> users = getUsers(); for(User user : users) { List<Tweet>

    tweets = user.getTweets(); for(Tweet tweet : tweets) { System.out.println(tweet.getBody()); } }
  27. List<User> users = getUsers(); for(User user : users) { List<Tweet>

    tweets = user.getTweets(); for(Tweet tweet : tweets) { System.out.println(tweet.getBody()); } }
  28. Observable.fromIterator(getUsers()) // .flatmap(user -> Observable .fromIterator(user.getTweets()) .map(Tweet::getBody) // .subscribe(System::out::println); //

  29. 20 List<User> users = getUsers(); for(User user : users) {

    int tweetCount = 0; // List<Tweet> tweets = user.getTweets(); for(Tweet tweet : tweets) { if(tweetCount > 20) { // break; } System.out.println(tweet.getBody()); tweetCount++; // } }
  30. 20 Observable.fromIterator(getUsers()) // .flatmap(user -> Observable .fromIterator(user.getTweets()) .take(20)) // .map(Tweet::getBody)

    // .subscribe(System::out::println); //
  31. User Tweet

  32. lter map atmap etc.. +

  33. fromArray, fromIterator int[] array = { 1,2,3,4 } Observable.fromArray(array) .subscribe(System::out::println);

    // => 1,2,3,4 ---1---2---3---4---------|--->
  34. lter int[] array = { 1,2,3,4 } Observable.fromArray(array) .filter(data ->

    data > 2) .subscribe(System::out::println); // => 3,4 -----------3---4---------|--->
  35. map int[] array = { 1,2,3,4 } Observable.fromArray(array) .map(data ->

    data * 2) .subscribe(System::out::println); // => 2,4,6,8 ----2---4--6---8---------|--->
  36. skip int[] array = { 1,2,3,4 } Observable.fromArray(array) .skip(2) .subscribe(System::out::println);

    // => 3,4 -----------3---4---------|--->
  37. take int[] array = { 1,2,3,4 } Observable.fromArray(array) .take(2) .subscribe(System::out::println);

    // => 1,2 -----------1---2---------|--->
  38. delay int[] array = { 1,2,3,4 } Observable.fromArray(array) .delay(1, TimeUnit.SECOND)

    .subscribe(System::out::println); // => 1,2,3,4 -----------1---2---3---4-|--->
  39. Zip int[] array = { "A","B","C","D" } Observable a =

    Observable.fromArray(array); Observable b = Observable.just(" "," "," "); Observable.zip(a,b, (left, right) -> left + right) .subscribe(System::out::println); // => A ,B ,C ---A---B---C---D---------|---> --- --- --- -----------|---> ZIP! ---A ---B ---C --------|--->
  40. http://reactivex.io/documentation/operators.html

  41. Rx minne Android Rx

  42. Rx

  43. Observable Observer Push Observer (Observable) (Observer) Observable Observer Observer @gomi_ningen

  44. Observable.just("Hello") .subscribe(data -> println(data + globalHoge));

  45. Observable <-> Observer

  46. 10^-6 concat, merge, zip

  47. None
  48. RP FRP

  49. Rx Rx Rx

  50. Rx Observer

  51. RxJS Marbles Rx Hot Cold Hot Cold