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
Pattern Matching
Search
Wojtek Mach
April 24, 2015
Programming
1
300
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
140
Hex Core
wojtekmach
0
130
Recurrences & Intervals
wojtekmach
2
450
Building an Umbrella Project
wojtekmach
21
5.9k
Advanced OOP in Elixir
wojtekmach
6
640
OOP in Elixir
wojtekmach
4
300
Formatting ruby code
wojtekmach
0
120
Other Decks in Programming
See All in Programming
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
140
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
2
280
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
270
datadog dash 2025 LLM observability for reliability and stability
ivry_presentationmaterials
0
120
LT 2025-06-30: プロダクトエンジニアの役割
yamamotok
0
520
プロダクト志向なエンジニアがもう一歩先の価値を目指すために意識したこと
nealle
0
110
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
240
Deep Dive into ~/.claude/projects
hiragram
9
1.6k
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
210
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
100
エラーって何種類あるの?
kajitack
5
310
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
270
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
35
6.7k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
Code Review Best Practice
trishagee
68
18k
Facilitating Awesome Meetings
lara
54
6.4k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
17
940
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.8k
The Invisible Side of Design
smashingmag
300
51k
4 Signs Your Business is Dying
shpigford
184
22k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
The Language of Interfaces
destraynor
158
25k
Why Our Code Smells
bkeepers
PRO
337
57k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
800
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