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 Programming in Perl6
Search
Shoichi Kaji
June 29, 2016
Programming
1
1.2k
Reactive Programming in Perl6
Gotanda.pm #9, 2016.06.29
Shoichi Kaji
June 29, 2016
Tweet
Share
More Decks by Shoichi Kaji
See All by Shoichi Kaji
宿泊予約サイトにおける検索と料金計算の両立
skaji
2
3.6k
昔のGo, 今のGo ~比較, ソート編~
skaji
0
180
Perl Quiz
skaji
0
130
cpm at PerlCon 2019
skaji
0
110
Perl5 Package Manager
skaji
0
660
Internal of cpm
skaji
0
580
How to execute external programs in Perl
skaji
3
580
Writing Perl with w0rp/ale
skaji
0
2.2k
nginx unit
skaji
0
44
Other Decks in Programming
See All in Programming
nekko cloudにおけるProxmox VE利用事例
irumaru
3
430
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
430
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
130
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
440
Go の GC の不得意な部分を克服したい
taiyow
3
790
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
6
1.1k
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
330
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
260
Jakarta EE meets AI
ivargrimstad
0
250
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
テストコード書いてみませんか?
onopon
2
120
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Adopting Sorbet at Scale
ufuk
73
9.1k
Fireside Chat
paigeccino
34
3.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
520
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Into the Great Unknown - MozCon
thekraken
33
1.5k
The Language of Interfaces
destraynor
154
24k
4 Signs Your Business is Dying
shpigford
181
21k
Practical Orchestrator
shlominoach
186
10k
Transcript
Reactive Programming in Perl6 Shoichi Kaji
Me • Shoichi Kaji • pause/github: skaji • Perl5: cpm,
App::FatPacker::Simple, Mojo::SlackRTM • Perl6: mi6, Frinfon, evalbot in Slack
Perl6ʹ Plack͋Δͷ?
Yes
Crust https://github.com/tokuhirom/p6-Crust
$ crustup -e '-> $e {200, [], ["hello world"]}' http
server is ready: http://127.0.0.1:5000/ $ curl http://localhost:5000 hello world
# app.p6 use v6; use Frinfon; get "/" => sub
($c) { $c.render-text("hello world\n"); }; get "/:user" => sub ($c) { my $user = $c.captured<user>; $c.render-json: { message => "hello $user!" }; }; app; $ crustup app.p6 http server is ready: http://127.0.0.1:5000/ minimal sinatra͋Δ
ͱ͜ΖͰ
Perl6ͱPerl5ͷ ໌֬ͳҧ͍ʁ
Perl6ΈࠐΈͰ ฒߦɾඇಉظϓϩάϥϛϯάΛ αϙʔτ͍ͯ͠Δ
ΞδΣϯμ • Perl6ͰϦΞΫςΟϒϓϩάϥϛϯά͕Ͱ͖Δʁ • ͦͦϦΞΫςΟϒϓϩάϥϛϯάͬͯʁ • ྫ • Perl6Ͱ ετϦʔϜ
= Supply • Supplyৄࡉ • ࣮͢͜͠ફతͳྫ • Perl-ish • ݁
Perl6ͰϦΞΫςΟϒϓ ϩάϥϛϯά͕Ͱ͖Δʁ
Ͱ͖Δ
ϦΞΫςΟϒ ϓϩάϥϛϯάͬͯʁ • ࢀߟURL • (A) ͳͥϦΞΫςΟϒϓϩάϥϛϯάॏཁ͔ɻhttp://d.hatena.ne.jp/ pokarim/20101226 • (B)
The introduction to Reactive Programming you've been missing https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 • (C) What is (functional) reactive programming? http:// stackoverflow.com/questions/1028250/what-is-functional-reactive- programming • ͜ΕΒʹΑΔͱϦΞΫςΟϒϓϩάϥϛϯάͱ ࣌ؒͱͱʹมԽ͠͏Δ = ࣌ؒॱʹฒΜͩΠϕϯτͷྻ = ετϦʔϜ Λத৺ʹͦ͑ͯϓϩάϥϛϯά͢Δ͜ͱɻ
࣍ͷࣜΛߟ͑Δɻ a=2Ͱb=3͔ͩΒɺc=2+3=5Ͱ͋Δɻ ͯ͞গ͕࣌ؒ͠ܦ͔ͬͯΒa=6ͱೖͯ͠ ͪΖΜcมΘΒͣ5ͷ··Ͱ͋Δɻ ͜͜ͰϦΞΫςΟϒϓϩάϥϛϯάͷཱʹཱͬͯ ߟ͑Δͱɺa=6ʹม͑ͨͱ͖c9ʹͳΔɻ ྫ a = 2
b = 3 c = a + b
͢ͳΘͪϦΞΫςΟϒϓϩάϥϛϯάͷཱͰ a,b,cΛ࣌ؒͱͱʹมԽ͠͏Δ=ετϦʔϜͱ ଊ͑Δɻઌ΄ͲͷࣜΛ͏গ͠આ໌తʹॻ͘ͱ ྫ a = ࣌ؒͱͱʹมԽ͠͏ΔͰॳظ2ɺগ͔ͯ͠͠Β6ʹมԽ b = ࣌ؒͱͱʹมԽ͠͏ΔͰॳظ3
c = ࣌ؒͱͱʹมԽ͠͏Δaͱbͷ a b c ԣ࣌ؒ࣠ 2 5 3 6 9
͏Ұॻ͘ͱɺϦΞΫςΟϒϓϩάϥϛϯάͱ ԼهͷΑ͏ͳετϦʔϜΛத৺ਓͱͯ͠ ϓϩάϥϛϯά͢Δख๏ͷ͜ͱɻ ࣮ࡍʹɺͦͷετϦʔϜʹର͠ * ෳͷετϦʔϜͷmerge,zip,combineLatest * ετϦʔϜͷfilter, map *
ετϦʔϜΛsubscribe͢Δ ͳͲͷૢ࡞Λͯ͠ϓϩάϥϛϯά͍ͯ͘͠
Perl6ͰετϦʔϜ=Supply • Perl6ͰSupplyܕ͕ετϦʔϜʹରԠ͢Δ • ྫ • γάφϧSupplyͱͯ͠ఏڙ͞ΕΔ • ֎෦ίϚϯυͷstdout/err͕Supplyͱͯ͠ఏڙ͞ΕΔ my
$signal-supply = signal(SIGINT); say $signal-supply ~~ Supply; # True my $proc = Proc::Async.new("dstat"); say $proc.stdout ~~ Supply; # True
Supplyৄࡉ - ࡞Γํ # ϑΝΠϧγεςϜͷมߋ௨Supply my $fs-supply = IO::Notification.watch-path(%*ENV<HOME>); #
10ඵ͝ͱʹeventΛemit͢ΔSupply my $interval-supply = Supply.interval(10); # supplyϒϩοΫͰࣗͰ࡞Δ͜ͱՄೳ my $supply = supply { emit "value1"; sleep 1; emit "value2"; };
Supplyৄࡉ - ૢ࡞ • ઌ΄ͲετϦʔϜͷૢ࡞ͱͯ͠filter, mapΛڍ͛ ͨɻPerl6ͩͱgrep, mapͰͰ͖Δɻ # dotfileΛൈ͍ͨSupply
my $not-dotfiles = IO::Notification.watch-path(%*ENV<HOME>).grep: -> $e { $e.path.IO.basename !~~ /^'.'/; }; # 10ඵ͝ͱʹ࣌ࠁΛemit͢ΔSupply my $time-supply = Supply.interval(10).map({DateTime.now});
Supplyৄࡉ - ૢ࡞ • ઌ΄ͲετϦʔϜΛsubscribe͢Δͱड़ͨɻ Perl6ͰtapɻtapͰɺSupplyʹ͕emit͞Ε ͨͱ͖ͷಈ࡞ΛొͰ͖Δɻ my $signal =
signal(SIGTERM); $signal.tap: -> $v { note "catch $v" };
Supplyৄࡉ - ૢ࡞ • ઌ΄ͲෳͷετϦʔϜͷૢ࡞ͱͯ͠merge, zip,combineLatestΛڍ͛ͨɻPerl6ͩͱmerge, zip,zip-latestͰ͖Δɻ Supply.merge($supply1, $supply2); Supply.zip($supply1,
$supply2); Supply.zip-latest($supply1, $supply2);
࣮͢͜͠ફతͳྫ • tail -fϑΝΠϧʹه͞ΕΔͱ(จࣈྻ)͕ emit͞ΕΔͨΊετϦʔϜ=Supplyͱͯ͠ଊ͑ ΒΕΔ • ࡞ͬͨ https://github.com/skaji/perl6-tail •
͜ͷperl6-tailΛͬͯΈΔ
use IO::TailFile; my $tail = IO::TailFile.watch("access.log", :chomp); access.logΛtail
͔ࣾΒͷΞΫηεΛআ͖ 1͝ͱʹrequest/secΛग़͢ -> ͬͱ͍͍ॻ͖ํ͕͋Δͣ use IO::TailFile; my $count = 0;
my $promise = Promise.new; my $tail = IO::TailFile.watch("access.log", :chomp) .grep(-> $line { $line !~~ / '192.168' / }) .map(-> $line { 1 }); $tail.tap: -> $i { $count += $i }, done => { $promise.keep(Nil) }; my $interval = Supply.interval(60); $interval.tap: -> $v { say "rps " ~ ($count / 60); $count = 0 }; await $promise;
Perl-ish
use v6; use IO::TailFile; my $tail = IO::TailFile.watch("access.log"); # infinite
list! my @line = $tail.list.lazy; for @line -> $line { # ... } # react, whenever block! react { whenever $tail -> $line { # ... }; whenever Supply.ineterval(10) { # ... }; };
݁ • ϦΞΫςΟϒϓϩάϥϛϯάͱετϦʔϜΛ த৺ͱͯ͠ϓϩάϥϛϯά͢Δख๏ • Perl6Ͱ ετϦʔϜ = Supply •
SupplyͷϝιουͦΖ͓ͬͯΓ Perl6ͰϦΞΫςΟϒϓϩάϥϛϯά͍͚Δ! (ଟ)