Introduction to Elixir - Pattern Matching and Control Flow - Part Three

in #utopian-io6 years ago

elixir.jpg

Repository: https://github.com/elixir-lang/elixir

What Will I Learn?

  • You will learn the basics of Pattern Matching
  • You will learn how to Pattern Match off of Tuples
  • You will learn how to Pattern Match on Lists
  • You will learn how to destruct data using Pattern Matching
  • You will learn how to create multi-clause functions
  • You will learn how to use Constants in a Pattern Match
  • You will learn about the Pin Operator

Requirements

System Requirements:
  • Elixir v1.8 requires Erlang 20.0 or later
OS Support for Elixir and Phoenix:
  • Mac OSx
  • Unix and Linux
  • Windows
  • Raspberry Pi
  • Docker
Required Knowledge
  • Some basic Programming Knowledge
  • An Elixir installation
  • VsCode or any other Text Editor
Resources for Elixir and Phoenix:
Sources:

Difficulty

  • Beginner

Description

In this Elixir Video tutorial, we introduce the concept of Pattern Matching and we look at examples of both basic Pattern Matching and Complex Pattern Matching. Pattern Matching is a concept that is central to the Elixir language; developers can create functions that have multiple heads and the equals operator (=) is the a match operator not the assignment operator. Mastering this concept of Pattern matching will allow the developer to create robust applications that can respond to structured data.

Pattern Matching on Basic Structures and Types

Pattern matching is an important construct in Elixir. It's a feature that allows developers to manipulate complex values such as Tuples and Lists with ease. It also allows you to write elegant and declarative conditionals and loops. This concept permeates every aspect of the Language from how the datatypes are handled to how the functions are structured in a module.


matching.png

All of the lines in the Above image are pattern matching statements. At runtime, the left side of the = operator is matched to the right side. the left side is called a pattern and the right side is the expression that evaluates to an Elixir term. Our most basic example takes the term 10 and matches it to a variable x. More complex examples allow us to specifically use the structure of the data to our advantage. Calling the :calendar.local_time() function returns a Tuple of two Tuples. Knowing this we are able to bind each of those tuples to their own independent variables date and time. We can then break each of these tuples down into their individual elements by using more pattern matching.

Pattern Matching with Function Clauses

In a more formal sense, Pattern matching is an expression that consists of two parts, the pattern and the term. During the match you can assert you expectations about the right-side term and you may bind some parts of the term to variables from the pattern. This idea can also be applied to functions by way of the function arguments. Each function in Elixir is identified through the function's argument count (arity) and the function name and this allows us to create multiple function causes with the same name and arity.


geo-module.png

In the above image we have a module defined with a single function inside of it. This function area/1 is a multi-clause function that makes use of pattern matching to determine which clause will execute. The match statement starts from the top and works towards the bottom. First it checks to see if the incoming arguments are inside of a tuple of length 3 and contain a :rectangle atom as the first term. If this is the case then the first clause is executed. It than moves on to the next clause and checks the incoming arguments compared to the parameters of the clause. If none of the first three arguments are matched then the final clause is automatically executed as a catch all. This clause needs to come at the bottom of the module otherwise it will block the other clauses from matching and render the code dead.

Full Github Source Code can be found here: https://github.com/tensor-programming/intro-to-elixir/tree/tensor-programming-part-3

Video Tutorial

Curriculum

Intro to Elixir
Related Phoenix Videos
GraphQL Series
Sort:  

Hi @tensor

Excellent work.

These tutorials will certainly help beginners starting on Elixir. Your concepts are clear and to the point and thank you again for your contribution to the video category.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Hi, @tensor!

You just got a 6.13% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Congratulations @tensor! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 19000 upvotes. Your next target is to reach 20000 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

New japanese speaking community Steem Meetup badge
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 96483.87
ETH 3356.14
USDT 1.00
SBD 3.20