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

怖くないReactive Extensions

いも
December 12, 2017

怖くないReactive Extensions

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

いも

December 12, 2017
Tweet

More Decks by いも

Other Decks in Technology

Transcript

  1. Rx

  2. 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));
  3. 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(); // }) })
  4. private Boolean isClicked = false; button.setOnClickListener(view -> { if(!isClicked) {

    doSomething(); isClicked = true; } } RxView.clicks(button) .take(1) .subscribe(view -> doSomething());
  5. int a = 10; int b = a * 2;

    a = 20; System.out.println(b); // => 40 https://www.slideshare.net/ytakano/ss-63454513
  6. 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
  7. 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); }
  8. 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
  9. 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
  10. Rx

  11. 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)//
  12. Rx

  13. Rx

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

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

    tweets = user.getTweets(); for(Tweet tweet : tweets) { System.out.println(tweet.getBody()); } }
  16. 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++; // } }
  17. lter int[] array = { 1,2,3,4 } Observable.fromArray(array) .filter(data ->

    data > 2) .subscribe(System::out::println); // => 3,4 -----------3---4---------|--->
  18. 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---------|--->
  19. 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-|--->
  20. 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 --------|--->
  21. Rx