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
2.5k
昔のGo, 今のGo ~比較, ソート編~
skaji
0
170
Perl Quiz
skaji
0
130
cpm at PerlCon 2019
skaji
0
110
Perl5 Package Manager
skaji
0
660
Internal of cpm
skaji
0
570
How to execute external programs in Perl
skaji
3
570
Writing Perl with w0rp/ale
skaji
0
2.2k
nginx unit
skaji
0
44
Other Decks in Programming
See All in Programming
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
CSC509 Lecture 09
javiergs
PRO
0
140
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
690
Jakarta EE meets AI
ivargrimstad
0
200
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Jakarta EE meets AI
ivargrimstad
0
650
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
190
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
100
Outline View in SwiftUI
1024jp
1
330
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Faster Mobile Websites
deanohume
305
30k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Adopting Sorbet at Scale
ufuk
73
9.1k
4 Signs Your Business is Dying
shpigford
180
21k
Six Lessons from altMBA
skipperchong
27
3.5k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Site-Speed That Sticks
csswizardry
0
27
Building Adaptive Systems
keathley
38
2.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
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ͰϦΞΫςΟϒϓϩάϥϛϯά͍͚Δ! (ଟ)