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
140
Recurrences & Intervals
wojtekmach
2
460
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
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
490
フロントエンドのmonorepo化と責務分離のリアーキテクト
kajitack
2
110
Claude Code と OpenAI o3 で メタデータ情報を作る
laket
0
130
STUNMESH-go: Wireguard NAT穿隧工具的源起與介紹
tjjh89017
0
380
実践 Dev Containers × Claude Code
touyu
1
210
エンジニアのための”最低限いい感じ”デザイン入門
shunshobon
0
110
[DevinMeetupTokyo2025] コード書かせないDevinの使い方
takumiyoshikawa
2
280
DynamoDBは怖くない!〜テーブル設計の勘所とテスト戦略〜
hyamazaki
1
200
書き捨てではなく継続開発可能なコードをAIコーディングエージェントで書くために意識していること
shuyakinjo
1
280
バイブコーディング × 設計思考
nogu66
0
120
Webinar: AI-Powered Development: Transformiere deinen Workflow mit Coding Tools und MCP Servern
danielsogl
0
130
Google I/O Extended Incheon 2025 ~ What's new in Android development tools
pluu
1
290
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
70
11k
Side Projects
sachag
455
43k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Bash Introduction
62gerente
614
210k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Balancing Empowerment & Direction
lara
2
570
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
GitHub's CSS Performance
jonrohan
1031
460k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
183
54k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.6k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Site-Speed That Sticks
csswizardry
10
780
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