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.

Comments are closed

This is a static version of the site. Comments are not available.

25 Comments

  1. Duonghienan.dha@gmail.com on August 5, 2020 at 9:48 pm

    Thank you very much for this course Mark, after finish this once, I realize that there is many places in my project could be done with pattern matching and guard :D. Great course 🙂

    BTW I knew about this course through your podcast 😉

    • Mark Ericksen on August 6, 2020 at 5:57 am

      Thanks for trying it out and for the feedback! I’m glad you found it helpful!

  2. anhtuanb1@gmail.com on October 21, 2020 at 1:33 am

    Thank you very much Mr. Mark Ericksen! I’ve learn a lot about the way of thinking in Elixir thank to your course.

  3. Ali ELBaitam on November 22, 2020 at 1:38 pm

    Thank you so much for offering this course; very well done.

  4. Francisco Quintero on November 30, 2020 at 8:00 pm

    Awesome course, lessons, and exercises. I really gained more confidence in Elixir and about using pattern matching. Previously to this course I was blocked in my self learning path in Elixir, now I now lots of things I did wrong which didn’t let me advance.

    Thank you so much!

    • Mark Ericksen on December 1, 2020 at 5:41 am

      Thanks! I’m glad you found it so helpful!

  5. romenigld on December 15, 2020 at 3:10 pm

    I was observing my self on try to do the Advent of Code 2020 and these pattern match on functions was what I’m thinking I need something to help me on this and some day listening your podcast I saw this course and was like a Gift of the God’s.
    Thank you Mark Ericksen, this course is a treasure.

    • Mark Ericksen on December 17, 2020 at 7:51 pm

      Ha! That’s great! Have fun with Advent of Code!

  6. William Martin on February 14, 2021 at 12:42 pm

    Thanks Mark, it was a breeze to take this course as, for myself an Elixir refresher.
    But I get a better understanding of PM by coming back to Elixir with your course.
    Already advocate for it to my colleagues!

  7. Mark Johnson on February 24, 2021 at 9:41 pm

    Thanks, Mark, for putting all this information together and making it understandable and fun. Great job!

    • Mark Ericksen on February 25, 2021 at 4:37 am

      Thanks! I’m glad you enjoyed it!

  8. Uzo Enudi on March 14, 2021 at 6:09 am

    Thank you, Mark, your teaching style is superb. I look forward to taking on Flow– the next course when I’m ready.

  9. Alister Sibbald on April 16, 2021 at 6:19 am

    Mark,

    I want to thank you for this superb course. For the first time, the whole Elixir pattern matching “thing” has really gelled and I am very confident in my ability to use it now. A brilliant course design and deliver!

    Now I’m off to do the code flow course, with high expectations!

    • Mark Ericksen on April 16, 2021 at 8:38 pm

      Thank you! I’m so glad you’ve enjoyed it!

  10. Aejaz Muslim on May 24, 2021 at 12:09 pm

    Superb course & very well designed.

  11. Peng Li on January 17, 2022 at 3:44 am

    Thanks, Mark! Your course helps a lot of understanding and writing better code in Elixir!

  12. Maksym Kosenko on January 17, 2022 at 1:29 pm

    It’s an awesome course! Thanks for your efforts, Mark!

  13. James Schorr on July 20, 2022 at 9:05 am

    Great course; I’ve been using Elixir for quite some time but still found it valuable.

    • Mark Ericksen on July 20, 2022 at 9:31 am

      Great! Glad to hear you still found it valuable even though you are already experienced with Elixir!

  14. Caleb Josue Ruiz Torres on September 15, 2023 at 4:34 pm

    Amazing, thanks for putting this together.
    I will certainly recommend this course right away.

    Thanks again!!!


    Caleb

    • Mark Ericksen on September 17, 2023 at 5:22 am

      Thanks! I’m glad you enjoyed it!

  15. Eric Watjen on September 27, 2023 at 11:49 am

    Hi Mark,

    Thanks for providing this great learning resource.
    I learned some good pattern matching tricks and guard usage.

    I liked that you have a pdf reference on this page, however, I get an xml error response when clicking the link:

    AccessDenied
    Request has expired
    1800
    2023-09-27T17:19:57Z
    2023-09-27T17:42:35Z
    RME0VTA7CWRPZ4QB
    Em09ZXP5bigTvjYTZHqa/1orPBIL4QNuHDj7EvFekd9CDaG57mdnjup+BXqRjL+pnmGqstbl5B3kSFkt7ADUzA==

    • Mark Ericksen on September 27, 2023 at 5:56 pm

      Hi Eric! I’m glad you enjoyed the course! Sorry you’re having an issue getting the download. I just tested it and it should work. Please try refreshing the page then click the link. I think it may have sat too long on the page before clicking the link. If that doesn’t work I’ll email it to you.

      Thanks!

  16. Aaron Zhang on August 15, 2024 at 2:59 pm

    Great course! I found this website on elixir website. But I found you on youtube – Mark Ericksen – Mental model for understanding Elixir GenServers,
    ElixirConf 2021 – Mark Ericksen – Globally Distributed Elixir Apps on Fly.io

    • Mark Ericksen on August 18, 2024 at 8:44 am

      Thanks! I’m glad you enjoyed the course!

Comments are closed on this static version of the site.