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
1.6k
昔のGo, 今のGo ~比較, ソート編~
skaji
0
150
Perl Quiz
skaji
0
130
cpm at PerlCon 2019
skaji
0
110
Perl5 Package Manager
skaji
0
650
Internal of cpm
skaji
0
570
How to execute external programs in Perl
skaji
3
560
Writing Perl with w0rp/ale
skaji
0
2.2k
nginx unit
skaji
0
44
Other Decks in Programming
See All in Programming
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.3k
qmuntal/stateless のススメ
sgash708
0
110
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
Vue SFCのtemplateでTypeScriptの型を活用しよう
tsukkee
3
1.4k
WEBエンジニア向けAI活用入門
sutetotanuki
0
290
EventSourcingの理想と現実
wenas
5
2k
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
250
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
660
レガシーな Android アプリのリアーキテクチャ戦略
oidy
1
170
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
1.2k
デプロイを任されたので、教わった通りにデプロイしたら障害になった件 ~俺のやらかしを越えてゆけ~
techouse
51
31k
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Art, The Web, and Tiny UX
lynnandtonic
296
20k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Gamification - CAS2011
davidbonilla
80
5k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
231
17k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
The Cult of Friendly URLs
andyhume
78
6k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Designing for Performance
lara
604
68k
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
Thoughts on Productivity
jonyablonski
67
4.3k
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ͰϦΞΫςΟϒϓϩάϥϛϯά͍͚Δ! (ଟ)