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
Hiroaki Osawa
September 28, 2019
Programming
0
500
Elixirとパターンマッチ
Hiroaki Osawa
September 28, 2019
Tweet
Share
More Decks by Hiroaki Osawa
See All by Hiroaki Osawa
健康保険証がなくなるらしい!?
qwyng
0
910
My Gems for AtCoder
qwyng
0
70
Benefits of contributing to OSS
qwyng
0
500
Rustちょっと触ってみた
qwyng
1
280
Other Decks in Programming
See All in Programming
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
210
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
170
生成AI時代を勝ち抜くエンジニア組織マネジメント
coconala_engineer
0
40k
PostgreSQLで手軽にDuckDBを使う!DuckDB&pg_duckdb入門/osc25hi-duckdb
takahashiikki
0
250
[AtCoder Conference 2025] LLMを使った業務AHCの上⼿な解き⽅
terryu16
6
1k
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 2
philipschwarz
PRO
0
140
AIエージェントの設計で注意するべきポイント6選
har1101
6
3.1k
AI Agent Dojo #4: watsonx Orchestrate ADK体験
oniak3ibm
PRO
0
130
Vibe codingでおすすめの言語と開発手法
uyuki234
0
180
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
190
SQL Server 2025 LT
odashinsuke
0
160
CSC307 Lecture 05
javiergs
PRO
0
470
Featured
See All Featured
The Spectacular Lies of Maps
axbom
PRO
1
440
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.2k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
91
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.7k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
220
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
Rails Girls Zürich Keynote
gr2m
95
14k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
The browser strikes back
jonoalderson
0
310
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
83
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