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
620
怖くないReactive Extensions
社内の新卒向け座学の資料から社内情報を削ったりしたものです
いも
December 12, 2017
Tweet
Share
More Decks by いも
See All by いも
UnityプログラミングバイブルR6号宣伝&Unity Logging小話
adarapata
0
380
Unityテスト活動のふりかえり
adarapata
1
500
Gather.townはいいぞ その後
adarapata
1
1.5k
Unityでの開発事例
adarapata
3
22k
どこのご家庭にもあるシーンマネージャーの話
adarapata
1
7.3k
Gather.townはいいぞ
adarapata
2
2.3k
宴はいいぞ
adarapata
0
1.3k
わかった気になるモブプログラミング
adarapata
1
83
モブワークっぽいのをやっている話/Trying mobwork
adarapata
2
1.2k
Other Decks in Technology
See All in Technology
Engineering at LY Corporation
lycorp_recruit_jp
0
260
データ活用促進のためのデータ分析基盤の進化
takumakouno
2
160
Windows Autopilot Deployment by OSD Guy
tamaiyutaro
0
300
Forget efficiency – Become more productive without the stress
ufried
0
240
音声×Copilot オンコパの世界
kasada
1
110
家具家電付アパートの冷蔵庫をIoT化してみた!
scbc1167
0
140
第23回Ques_タイミーにおけるQAチームの在り方 / QA Team in Timee
takeyaqa
0
180
プロポーザルのつくり方 〜個人技編〜 / How to come up with proposals
ohbarye
4
310
GraphRAGを用いたLLMによるパーソナライズド推薦の生成
naveed92
0
190
Railsで4GBのデカ動画ファイルのアップロードと配信、どう実現する?
asflash8
1
190
mikroBus HAT を用いた簡易ベアメタル開発
tarotene
0
260
「 SharePoint 難しい」ってよく聞くけど、そんなに言うなら8歳の息子に試してもらった
taichinakamura
2
790
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
For a Future-Friendly Web
brad_frost
175
9.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
15
2k
Side Projects
sachag
452
42k
YesSQL, Process and Tooling at Scale
rocio
168
14k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
42
2.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Automating Front-end Workflow
addyosmani
1366
200k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
810
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