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
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
240
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
flutterkaigi_2024.pdf
kyoheig3
0
150
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
RubyLSPのマルチバイト文字対応
notfounds
0
120
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
930
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
140
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
Featured
See All Featured
Bash Introduction
62gerente
608
210k
Become a Pro
speakerdeck
PRO
25
5k
Writing Fast Ruby
sferik
627
61k
Facilitating Awesome Meetings
lara
50
6.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Thoughts on Productivity
jonyablonski
67
4.3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Faster Mobile Websites
deanohume
305
30k
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ͰϦΞΫςΟϒϓϩάϥϛϯά͍͚Δ! (ଟ)