$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pattern Matching
Search
Wojtek Mach
April 24, 2015
Programming
1
310
Pattern Matching
Wojtek Mach
April 24, 2015
Tweet
Share
More Decks by Wojtek Mach
See All by Wojtek Mach
Writing an Ecto Adapter: Introducing MyXQL
wojtekmach
1
150
Hex Core
wojtekmach
0
140
Recurrences & Intervals
wojtekmach
2
470
Building an Umbrella Project
wojtekmach
21
6k
Advanced OOP in Elixir
wojtekmach
6
660
OOP in Elixir
wojtekmach
4
300
Formatting ruby code
wojtekmach
0
120
Other Decks in Programming
See All in Programming
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
380
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
260
connect-python: convenient protobuf RPC for Python
anuraaga
0
400
堅牢なフロントエンドテスト基盤を構築するために行った取り組み
shogo4131
8
2.3k
Building AI Agents with TypeScript #TSKaigiHokuriku
izumin5210
6
1.3k
TUIライブラリつくってみた / i-just-make-TUI-library
kazto
1
370
30分でDoctrineの仕組みと使い方を完全にマスターする / phpconkagawa 2025 Doctrine
ttskch
3
800
AIコーディングエージェント(Manus)
kondai24
0
170
ハイパーメディア駆動アプリケーションとIslandアーキテクチャ: htmxによるWebアプリケーション開発と動的UIの局所的適用
nowaki28
0
400
AIコーディングエージェント(skywork)
kondai24
0
160
LLMで複雑な検索条件アセットから脱却する!! 生成的検索インタフェースの設計論
po3rin
2
670
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
150
Featured
See All Featured
Building Applications with DynamoDB
mza
96
6.8k
Optimizing for Happiness
mojombo
379
70k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
720
Building Flexible Design Systems
yeseniaperezcruz
330
39k
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
A Tale of Four Properties
chriscoyier
162
23k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.6k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Six Lessons from altMBA
skipperchong
29
4.1k
Bash Introduction
62gerente
615
210k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.7k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Transcript
Pattern Matching Wojtek Mach
Card „Ace of Spades”
Card "As" „Ace of Spades”
Card "A♠" „Ace of Spades”
Card { :A, :s } „Ace of Spades”
Card { rank, suit } „Ace of Spades”
Hand { {:A,:s}, {:A,:h}, {6,:s}, {6,:d}, {2,:c} } „Aces and
Sixes”
Ranks •Straight Flush •Four of a Kind •Full House •Flush
•Straight •Three of a Kind •Two Pairs •One Pair •High Card
Conditionals Card = Struct.new :rank, :suit def rank(hand) if hand[0].rank
== hand[1].rank && hand[0].rank == hand[2].rank && hand[0].rank == hand[3].rank :four_of_a_kind elsif hand[0].rank == hand[1].rank && hand[0].rank == hand[2].rank && hand[3].rank == hand[4].rank :full_house # ... end end
OOP! Card = Struct.new :rank, :suit class Hand def four_of_a_kind?
cards[0].rank == cards[1].rank && cards[0].rank == cards[2].rank && cards[0].rank == cards[3].rank end def full_house? # ... end # ... end
Pattern matching case value do pattern1 -> result1 pattern2 ->
result2 # ... end
Pattern matching case hand do { card1, card2, card3, card4,
card5 } -> rank1 { card1, card2, card3, card4, card5 } -> rank2 # ... end
Pattern matching case hand do { card1, card2, _ ,
_ , card5 } -> rank1 { card1, card2, _ , _ , card5 } -> rank2 # ... end
Pattern matching case hand do { {r1,s1}, {r2,s2}, _ ,
_ , {r5,s5} } -> rank1 { {r1,s1}, {r2,s2}, _ , _ , {r5,s5} } -> rank2 # ... end
Pattern matching case hand do { {a,_}, {a,_}, {a,_}, {a,_},
_ } -> :four_of_a_kind end
Pattern matching case hand do { {a,_}, {a,_}, {a,_}, {a,_},
_ } -> :four_of_a_kind { {a,_}, {a,_}, {a,_}, {b,_}, {b,_} } -> :full_house end
Pattern matching case hand do { {a,_}, {a,_}, {a,_}, {a,_},
_ } -> :four_of_a_kind { {a,_}, {a,_}, {a,_}, {b,_}, {b,_} } -> :full_house { {_,a}, {_,a}, {_,a}, {_,a}, {_,a} } -> :flush end
Pattern matching case hand do { {a,_}, {a,_}, {a,_}, {a,_},
_ } -> {:four_of_a_kind, a} { {a,_}, {a,_}, {a,_}, {b,_}, {b,_} } -> {:full_house, a, b} { {_,a}, {_,a}, {_,a}, {_,a}, {_,a} } -> {:flush} end
Pattern matching case hand do { {a,_}, {a,_}, {a,_}, {a,_},
_ } -> {:four_of_a_kind, a} { {a,_}, {a,_}, {a,_}, {b,_}, {b,_} } -> {:full_house, a, b} { {_,a}, {_,a}, {_,a}, {_,a}, {_,a} } -> {:flush} # straights are tricky! end
Thanks! @wojtekmach @wojtekmach