Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Elixir

You're reading from   Learning Elixir Unveil many hidden gems of programming functionally by taking the foundational steps with Elixir

Arrow left icon
Product type Paperback
Published in Jan 2016
Publisher
ISBN-13 9781785881749
Length 286 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Kenneth Ballou Kenneth Ballou
Author Profile Icon Kenneth Ballou
Kenneth Ballou
Kenny Ballou Kenny Ballou
Author Profile Icon Kenny Ballou
Kenny Ballou
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Introducing Elixir – Thinking Functionally FREE CHAPTER 2. Elixir Basics – Foundational Steps toward Functional Programming 3. Modules and Functions – Creating Functional Building Blocks 4. Collections and Stream Processing 5. Control Flow – Occasionally You Need to Branch 6. Concurrent Programming – Using Processes to Conquer Concurrency 7. OTP – A Poor Name for a Rich Framework 8. Distributed Elixir – Taking Concurrency to the Next Node 9. Metaprogramming – Doing More with Less Index

Invariable variables and pattern matching

One of the most misunderstood concepts in functional programming is that of assignment. Or, said another way, assignment doesn't exist.

Let's try to dispel this misconceived idea. In iex, we might see some code like this:

iex(1)> a = 2
2
iex(2)> a + 4
6

We may be tempted to explain the preceding code snippet with something like, "So we assign 2 to a and then add 4 to a giving us 6." However, in Elixir, this is incorrect. Elixir does not define = as an assignment operator, but rather a match operator. That is, Elixir attempts to match the left side of the = operator to that of the right.

In step 1, for Elixir to make the match succeed, we bind the value of 2 to the variable, a. Then later, when we perform the addition, we are substituting 2 for a, yielding an expression that looks like 2 + 4, which obviously equals 6.

This is a really different way to think about what is going on internally. Take a moment to let it sink in.

Back...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime