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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Wojtek Mach
April 24, 2015
Programming
320
1
Share
Pattern Matching
Wojtek Mach
April 24, 2015
More Decks by Wojtek Mach
See All by Wojtek Mach
Writing an Ecto Adapter: Introducing MyXQL
wojtekmach
1
160
Hex Core
wojtekmach
0
160
Recurrences & Intervals
wojtekmach
2
500
Building an Umbrella Project
wojtekmach
21
6.1k
Advanced OOP in Elixir
wojtekmach
6
680
OOP in Elixir
wojtekmach
4
310
Formatting ruby code
wojtekmach
0
140
Other Decks in Programming
See All in Programming
1人1案件のプロダクトエンジニア時代に、"プロセス監督"としてチャレンジしたこと
non0113
0
160
[BalkanRuby 2026] Drop your app/services!
palkan
3
630
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
310
Zod v4 Codec でスキーマに型変換を埋め込む REST API 設計 #TSKaigi2026
ryutaro_yako
0
100
Import assertionsが消えた日~ECMAScriptの仕様はどう決まり、なぜ覆るのか~
bicstone
2
190
UaaL×Androidアプリのメモリ計測 — Memory Profilerの先へ
rio432
0
170
Hive Metastoreを通して学ぶIceberg REST Catalog ― 仕様から実装まで
okumin
0
250
横断組織出身のQAEがインプロセスQAEでつまずいたこと・活かせたこと
ty89
0
170
Migrations : C'est une question d'hygiène !
vinceamstoutz
0
880
プロパティの順序で型推論が壊れる!? TypeScript6.0の修正からContext-Sensitivityの仕組みを追う
bicstone
2
800
Skillは並べた。動かなかった。契約で繋いだ。— 65個のSkillから、自走する開発サイクルへ
junholee
0
690
色即是空、空即是色、データサイエンス
kamoneggi
1
140
Featured
See All Featured
New Earth Scene 8
popppiees
3
2.2k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
570
We Have a Design System, Now What?
morganepeng
55
8.1k
Technical Leadership for Architectural Decision Making
baasie
3
370
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
330
How to make the Groovebox
asonas
2
2.2k
WCS-LA-2024
lcolladotor
0
600
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
450
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
460
The SEO identity crisis: Don't let AI make you average
varn
0
470
Building Applications with DynamoDB
mza
96
7k
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