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
470
Elixirとパターンマッチ
Hiroaki Osawa
September 28, 2019
Tweet
Share
More Decks by Hiroaki Osawa
See All by Hiroaki Osawa
健康保険証がなくなるらしい!?
qwyng
0
710
My Gems for AtCoder
qwyng
0
61
Benefits of contributing to OSS
qwyng
0
480
Rustちょっと触ってみた
qwyng
1
260
Other Decks in Programming
See All in Programming
状態遷移図を書こう / Sequence Chart vs State Diagram
orgachem
PRO
2
210
[SRE NEXT] 複雑なシステムにおけるUser Journey SLOの導入
yakenji
0
150
NPOでのDevinの活用
codeforeveryone
0
900
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
200
AIエージェントはこう育てる - GitHub Copilot Agentとチームの共進化サイクル
koboriakira
0
760
スタートアップの急成長を支えるプラットフォームエンジニアリングと組織戦略
sutochin26
1
7.3k
おやつのお供はお決まりですか?@WWDC25 Recap -Japan-\(region).swift
shingangan
0
140
React は次の10年を生き残れるか:3つのトレンドから考える
oukayuka
12
3.7k
AIともっと楽するE2Eテスト
myohei
8
3k
Startups on Rails in Past, Present and Future–Irina Nazarova, RailsConf 2025
irinanazarova
0
250
Git Sync を超える!OSS で実現する CDK Pull 型デプロイ / Deploying CDK with PipeCD in Pull-style
tkikuc
4
350
Modern Angular with Signals and Signal Store:New Rules for Your Architecture @enterJS Advanced Angular Day 2025
manfredsteyer
PRO
0
270
Featured
See All Featured
Being A Developer After 40
akosma
90
590k
The Straight Up "How To Draw Better" Workshop
denniskardys
235
140k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
How STYLIGHT went responsive
nonsquared
100
5.6k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Side Projects
sachag
455
42k
BBQ
matthewcrist
89
9.7k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Typedesign – Prime Four
hannesfritz
42
2.7k
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