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
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
150
Recurrences & Intervals
wojtekmach
2
470
Building an Umbrella Project
wojtekmach
21
6k
Advanced OOP in Elixir
wojtekmach
6
670
OOP in Elixir
wojtekmach
4
300
Formatting ruby code
wojtekmach
0
130
Other Decks in Programming
See All in Programming
Grafana:建立系統全知視角的捷徑
blueswen
0
330
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
380
CSC307 Lecture 03
javiergs
PRO
1
490
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1k
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
CSC307 Lecture 01
javiergs
PRO
0
690
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.8k
OSSとなったswift-buildで Xcodeのビルドを差し替えられるため 自分でXcodeを直せる時代になっている ダイアモンド問題編
yimajo
3
610
ThorVG Viewer In VS Code
nors
0
770
Fragmented Architectures
denyspoltorak
0
150
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
5
340
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
A Soul's Torment
seathinner
5
2.2k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
110
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
0
100
Navigating Weather and Climate Data
rabernat
0
100
Into the Great Unknown - MozCon
thekraken
40
2.2k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
63
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.3k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
180
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
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