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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Wojtek Mach
April 24, 2015
Programming
330
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
170
Hex Core
wojtekmach
0
160
Recurrences & Intervals
wojtekmach
2
510
Building an Umbrella Project
wojtekmach
21
6.1k
Advanced OOP in Elixir
wojtekmach
6
680
OOP in Elixir
wojtekmach
4
320
Formatting ruby code
wojtekmach
0
140
Other Decks in Programming
See All in Programming
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
150
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
3.1k
AIで効率化できた業務・日常
ochtum
0
100
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
12
3.5k
AutonomyとControlのあいだ:Graflowで記述するAIエージェント協調
myui
0
110
エージェンティックRAGにAWSで入門しよう!
har1101
8
1.2k
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
110
AIチームを指揮するOSS「TAKT」活用術 / How to Use “TAKT,” an OSS Tool for Orchestrating AI Teams
nrslib
6
840
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
250
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
170
Oxlintのカスタムルールの現況
syumai
6
1k
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
Thoughts on Productivity
jonyablonski
76
5.2k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
360
Ruling the World: When Life Gets Gamed
codingconduct
0
250
Paper Plane (Part 1)
katiecoart
PRO
0
8.5k
We Have a Design System, Now What?
morganepeng
55
8.2k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Practical Orchestrator
shlominoach
191
11k
How to Talk to Developers About Accessibility
jct
2
220
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
600
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
380
Become a Pro
speakerdeck
PRO
31
6k
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