Pattern Matching Summary
Congratulations on completing the Pattern Matching Course!
Putting it all together
Pattern matching is everywhere in Elixir. If thinking in patterns is a new experience for you, then out of habit you will still be writing code that is more imperative. That’s okay! As you spend more time in Elixir, you will see more opportunities to refactor your code to make it more declarative and use patterns more effectively.
A good exercise is to look at some code you just wrote and ask, “Does this feel like Elixir code?” If not, look at some examples of a good use of pattern matching and try a small refactor. Ask yourself again, “Does this feel like Elixir code?” After just a few iterations it can really begin to change! What really needs to change is not the code, it’s the way you think about your code. Pattern matching is a new tool you have to solve problems. You need to start thinking about your application as data, functions, and the patterns in your data.
Having gone through the exercises and practice code, you have a good foundation to build on. Let’s review some of the things we covered.
- The pattern goes on the left of the Match Operator. The data goes on the right.
- A Pattern Match can match the data’s type, shape, and bind variables to values all in a single statement.
- A Match Error occurs when no match could be made.
- Pattern matching goes from top to bottom. If the first pattern doesn’t match, the next pattern is checked and so on.
- The first pattern to match wins and takes the data.
- Make your top patterns more specific.
- The “
^
” Pin Operator lets you reference the value of a variable in a pattern. - The “
_
” lets you define shape without binding to the value. - A nested
if
statement is an anti-pattern - Lists are “linked lists” and it is cheaper to add to the front than it is to add to the end like an array.
- Lists are recursive, a list is made up of a “head” element and a tail that is itself a list.
- Strings can be pattern matched.
- Guard clauses are another level of a pattern match. They can be also be used to match type and shape.
- Guard clauses allow you to compare bound variables to each other in a pattern match.
- Pattern matching is most effective when you think differently about your code.
Pattern matching is awesome!
Pattern matching is an incredible tool! As with every new tool, there is a learning curve. By completing this coverage of pattern matching, you have dramatically sped up your learning! You have hands-on experience solving problems using this new tool. More important than learning the mechanics of pattern matching, you have learned how to think about your data and the patterns in your application!
You are on excellent footing now for continued building and growth. You are better able to read and understand Elixir code because pattern matching truly is everywhere in Elixir.
I love Elixir because I feel I’m a better developer when I’m working in it. Tools like pattern matching that we covered here feel like a super power! When I work in other languages where these tools don’t exist, I really miss it. The more you work with Elixir and pattern matching, the more natural it becomes.
You are ready for the next step and I can’t wait to share it with you!
Download reference resource
Now that you have completed the course, as a special “thank you”, I want you to have a ready, portable, handy reference as a resource of everything we covered together. This is a PDF download of the course information. It is indexed and searchable so you can easily jump around and find something you want to refer back to.
25 Comments
Comments are closed on this static version of the site.
Comments are closed
This is a static version of the site. Comments are not available.