___  ___    _ _    _  _ _____   _____
 / __|/ _ \  | | |  | || |_ _\ \ / / __|
| (_ | (_) | |_  _| | __ || | \ V /| _|
 \___|\___/    |_|  |_||_|___| \_/ |___|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

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

BY: @tensor | CREATED: May 12, 2019, 6:52 p.m. | VOTES: 235 | PAYOUT: $25.93 | [ VOTE ]

[IMAGE: https://cdn.steemitimages.com/DQmVq7k7SesVgh7jun87JhmfG9JBcC2gtJUCbngRyMiHCcr/elixir.jpg]

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

What Will I Learn?

Requirements

System Requirements:
OS Support for Elixir and Phoenix:
Required Knowledge
Resources for Elixir and Phoenix:
Sources:

Difficulty

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.

[IMAGE: https://cdn.steemitimages.com/DQma5a7gKJiaMbeyq5hVE1rwzNt6WyWtuDYSriztXuek3kD/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.

[IMAGE: https://cdn.steemitimages.com/DQmRS8p3Xe5GkgYHPbst3c4bd85aQoAzWFMuDUmEvKk3Q96/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

> - Phoenix 1.3 Chat Application Tutorial
> - Phoenix 1.3 Contexts and Ecto Schemas

GraphQL Series

> - Phoenix 1.3 GraphQL API - Part 1
> - Phoenix 1.3 GraphQL API - Part 2
> - Phoenix 1.3 GraphQL API - Part 3

TAGS: [ #utopian-io ] [ #video-tutorials ] [ #steemstem ] [ #technology ] [ #programming ]

Replies

@steem-plus | May 13, 2019, 6:03 a.m. | Votes: 0 | [ VOTE ]

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.

@rosatravels | May 15, 2019, 7:19 a.m. | Votes: 11 | [ VOTE ]

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]

@utopian-io | May 17, 2019, 8:30 p.m. | Votes: 0 | [ VOTE ]

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

@utopian-io | May 15, 2019, 7:12 p.m. | Votes: 0 | [ VOTE ]

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!

@steem-ua | May 15, 2019, 8:12 p.m. | Votes: 0 | [ VOTE ]

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

@steemitboard | May 16, 2019, 8:07 a.m. | Votes: 0 | [ VOTE ]

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!
[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>