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
Elixirとパターンマッチ
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Hiroaki Osawa
September 28, 2019
Programming
520
0
Share
Elixirとパターンマッチ
Hiroaki Osawa
September 28, 2019
More Decks by Hiroaki Osawa
See All by Hiroaki Osawa
健康保険証がなくなるらしい!?
qwyng
0
1k
My Gems for AtCoder
qwyng
0
80
Benefits of contributing to OSS
qwyng
0
520
Rustちょっと触ってみた
qwyng
1
300
Other Decks in Programming
See All in Programming
TAKTでAI駆動開発の品質を設計する
j5ik2o
5
700
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
さぁV100、メモリをお食べ・・・
nilpe
0
130
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
3.3k
TypeSpec で繋ぐ複数プロダクトの型安全
maroon8021
1
340
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
1.8k
AIエージェントの隔離技術の徹底比較
kawayu
0
460
LLM Plugin for Node-REDの利用方法と開発について
404background
0
160
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
Moments When Things Go Wrong
aurimas
3
140
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.3k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
240
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
30 Presentation Tips
portentint
PRO
1
310
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
200
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
550
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
300
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
320
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
160
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Transcript
Elixir とパターンマッチ Created by Hiroaki Osawa(@QWYNG)
Hiroaki Osawa Twitter @qwyngg GitHub https://github.com/QWYNG ブログ https://sasa5740.hatenablog.com Ruby とか
Rails とか
None
Elixir の特徴 Erlang の仮想マシン上で実⾏される 関数型⾔語 変数がイミュータブル Ruby の⽂法の⽪を被った Erlang といわれている。
シンプルな⽂法で⾒やすい IO.puts "Hello World!" -module(hello). -export([main/0]). main() -> io:format("Hello World!\n").
Elixir の根幹、パターンマッチ
Elixir の = はパターンマッチ iex(1)> a = 1 # 左辺が変数なら値を束縛します
>1 iex(2)> a + 3 >4 iex(3)> 1 = a # 代⼊ではない、 1 = 1 は真 > 1 iex(4)> 2 = a # 2 = 1 は偽ですね ** (MatchError) no match of right hand side value: 1
= の別の⾒⽅ ⽅程式 x = a + 1 において x
に a + 1 を代⼊する意味ではない。 x と a + 1 は等しいとしているだけ
= の別の⾒⽅ ⽅程式 x = a + 1 において x
に a + 1 を代⼊する意味ではない。 x と a + 1 は等しいとしているだけ って Erlang 作者の Joe Armstrong さんがいっていたそうです
再代⼊はできません iex(1)> i = 42 42 iex(2)> i = 99
99 ⼀⾒できているように⾒えるが…
In Ruby irb(main):001:0> i = 42 => 42 irb(main):002:0> f
= -> { i } => #<Proc:0x00007fded21ce108@(irb):2 (lambda)> irb(main):003:0> f.call => 42 irb(main):004:0> i = 99 => 99 irb(main):005:0> f.call => 99 変数の参照先が変わっている
In Elixir iex(1)> i = 42 42 iex(2)> f =
fn -> i end # 無名関数 #Function<21.91303403/0 in :erl_eval.expr/5> iex(3)> f.() # 無名関数の呼び出し 42 iex(4)> i = 99 # ここでやっているのは変数のシャドウイング 99 iex(5)> f.() 42 同じ名前の別の変数扱い 元の i の参照先は変えない
関数でのパターンマッチ 1からnまでの整数の合計を出す関数 defmodule Inject do def sum(0), do: 0 def
sum(n), do: n + sum(n - 1) end Inject.sum(3) >6
defmodule Inject do def sum(0), do: 0 # 引数が0 の時は0
を返す def sum(n), do: n + sum(n - 1) #0 以外ならn + sum(n - 1) を返して再帰 end sum/1 #Elixir での関数の表し⽅、名前と引数の数が⼀緒なら⼀つの関数 この関数が引数を与えられると sum(0 = 3) # ✖ sum(n = 3) # ◯ ここにあるブロックを実⾏! という処理が⾏われているイメージ
まとめ Elixir の根幹は= は代⼊ではないこと 関数でも条件でもこの考え⽅は⼀緒 プログラミングを最初に覚えたときには= の認識が変わったが、 Elixir は理解にはまたそれを忘れる必要がある
参考 プログラミング Elixir ( DaveThomas ( 著), 笹⽥耕⼀ ( 翻訳),
⿃井雪 ( 翻訳)) とんでもない神本です。プロを⽬指すための Ruby ⼊⾨とメタプログ ラミング Ruby の中間くらいの内容で、Elixir を完全理解できます 変数の再定義の解説は@cedretaber さんの Qiita 記事をリスペクトし ました https://qiita.com/cedretaber/items/4a7b3572acaeafe070e2
最後に宣伝 Ruby の Gem をいくつか公開しているのでぜひ使ってみてください pry-singular https://github.com/QWYNG/pry-singular クラスメソッドを Pry のコマンドにできる
Gem D4C https://github.com/QWYNG/d4c rails console にオプションを⾜せるようになる Gem