Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
怖くないReactive Extensions
Search
いも
December 12, 2017
Technology
0
610
怖くないReactive Extensions
社内の新卒向け座学の資料から社内情報を削ったりしたものです
いも
December 12, 2017
Tweet
Share
More Decks by いも
See All by いも
UnityプログラミングバイブルR6号宣伝&Unity Logging小話
adarapata
0
360
Unityテスト活動のふりかえり
adarapata
1
500
Gather.townはいいぞ その後
adarapata
1
1.4k
Unityでの開発事例
adarapata
3
22k
どこのご家庭にもあるシーンマネージャーの話
adarapata
1
7.1k
Gather.townはいいぞ
adarapata
2
2.3k
宴はいいぞ
adarapata
0
1.2k
わかった気になるモブプログラミング
adarapata
1
79
モブワークっぽいのをやっている話/Trying mobwork
adarapata
2
1.2k
Other Decks in Technology
See All in Technology
UE5の雑多なテク
ryuichikawano
0
330
【shownet.conf_】持続可能な次世代Wi-Fi運用に向けて
shownet
PRO
0
330
分析者起点の企画を成功させた連携面の工夫
lycorptech_jp
PRO
1
240
【shownet.conf_】ローカル5Gを活用したウォーキングツアーの体感向上
shownet
PRO
0
320
XPを始める新人に伝えたい近道の鍵
nakasho
1
300
How CERN serves 1EB of data via FUSE
ennael
PRO
0
16k
ITエンジニアとして知っておいてほしい、電子メールという大きな穴
logica0419
6
1k
LINEヤフー新卒採用 コーディングテスト解説 アルゴリズム問題編
lycorp_recruit_jp
0
13k
Hazard pointers with reference counter
ennael
PRO
0
120
Causal Impactを用いたLINE Pay UIの効果検証とABテスト実施への貢献
lycorptech_jp
PRO
3
550
Tracking down sources of kernel errors with retsnoop
ennael
PRO
0
150
Assisted reorganization of data structures
ennael
PRO
0
240
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
31
2.8k
The Pragmatic Product Professional
lauravandoore
31
6.2k
Teambox: Starting and Learning
jrom
131
8.7k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
327
21k
Scaling GitHub
holman
458
140k
Unsuck your backbone
ammeep
668
57k
Documentation Writing (for coders)
carmenintech
65
4.3k
Making the Leap to Tech Lead
cromwellryan
130
8.8k
From Idea to $5000 a Month in 5 Months
shpigford
380
46k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
How to Ace a Technical Interview
jacobian
275
23k
How GitHub Uses GitHub to Build GitHub
holman
473
290k
Transcript
ReactiveExtensions
Imoto Hiroto (2012~) (2013/1 ~ 2017/2) API minne (2017/3~) Android
Android https://adarapata.com/
None
ReactiveExtensions(Rx) Rx Rx Scheduler, Hot&Cold, Subject Reactive Streams RP, FRP,
Rx
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));
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(); // }) })
private Boolean isClicked = false; button.setOnClickListener(view -> { if(!isClicked) {
doSomething(); isClicked = true; } } RxView.clicks(button) .take(1) .subscribe(view -> doSomething());
None
Reactive Extensions(Rx) .NET Framework http://reactivex.io/ RxJava,RxJS,RxSwift etc... React.js
int a = 10; int b = a * 2;
a = 20; System.out.println(b); // => 40 https://www.slideshare.net/ytakano/ss-63454513
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
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); }
LINQ LINQ Rx LINQ to Events, LINQ to Asynchronous Rx
LINQ Rx
before System.out.println("Hello World"); after Observable.just("Hello World") .subscribe(System::out::println);
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
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
None
Rx --1---2---3---4--|--> Observable.just(1,2,3,4); // 1,2,3,4
OnNext OnError OnComplete = OnNext OnComplete ↓ ↓ just(1,2,3,4) --1--2--3--4--|-->
(Subscribe) Observable.just("Hello World") .subscribe(System::out::println); just("HelloWorld") ----H---|--> subscribe(println); "Hello World" println
Rx
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)//
Rx
Rx
List<User> users = getUsers(); for(User user : users) { List<Tweet>
tweets = user.getTweets(); for(Tweet tweet : tweets) { System.out.println(tweet.getBody()); } }
List<User> users = getUsers(); for(User user : users) { List<Tweet>
tweets = user.getTweets(); for(Tweet tweet : tweets) { System.out.println(tweet.getBody()); } }
Observable.fromIterator(getUsers()) // .flatmap(user -> Observable .fromIterator(user.getTweets()) .map(Tweet::getBody) // .subscribe(System::out::println); //
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++; // } }
20 Observable.fromIterator(getUsers()) // .flatmap(user -> Observable .fromIterator(user.getTweets()) .take(20)) // .map(Tweet::getBody)
// .subscribe(System::out::println); //
User Tweet
lter map atmap etc.. +
fromArray, fromIterator int[] array = { 1,2,3,4 } Observable.fromArray(array) .subscribe(System::out::println);
// => 1,2,3,4 ---1---2---3---4---------|--->
lter int[] array = { 1,2,3,4 } Observable.fromArray(array) .filter(data ->
data > 2) .subscribe(System::out::println); // => 3,4 -----------3---4---------|--->
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---------|--->
skip int[] array = { 1,2,3,4 } Observable.fromArray(array) .skip(2) .subscribe(System::out::println);
// => 3,4 -----------3---4---------|--->
take int[] array = { 1,2,3,4 } Observable.fromArray(array) .take(2) .subscribe(System::out::println);
// => 1,2 -----------1---2---------|--->
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-|--->
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 --------|--->
http://reactivex.io/documentation/operators.html
Rx minne Android Rx
Rx
Observable Observer Push Observer (Observable) (Observer) Observable Observer Observer @gomi_ningen
Observable.just("Hello") .subscribe(data -> println(data + globalHoge));
Observable <-> Observer
10^-6 concat, merge, zip
None
RP FRP
Rx Rx Rx
Rx Observer
RxJS Marbles Rx Hot Cold Hot Cold