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
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
540
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
130
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
Go の GC の不得意な部分を克服したい
taiyow
2
780
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
750
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
330
From Translations to Multi Dimension Entities
alexanderschranz
2
130
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
130
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
Beyond ORM
77web
5
650
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
Facilitating Awesome Meetings
lara
50
6.1k
Unsuck your backbone
ammeep
669
57k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Into the Great Unknown - MozCon
thekraken
33
1.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Building Adaptive Systems
keathley
38
2.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
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ͰϦΞΫςΟϒϓϩάϥϛϯά͍͚Δ! (ଟ)