Image for post
Image for post
Learn Elixir — chapter by chapter explanation of Programming Elixir 1.6 (by Dave Thomas)

Programming Elixir 1.6 — Chapter 1 and 2 Summaries

Willson Mock
Nov 12, 2018 · 4 min read
  1. Chapter 1 and 2 — OOP, Functional Programming, and Pattern Matching (this post)

In my day to day, I write a lot of code in both JavaScript and Ruby. I’ve always enjoyed learning new programming languages — I’m a firm believer that new languages and paradigms can really expand the way you think about code. So my goal with this new series of blog posts is to share my own learning of a programming language that I’ve heard a lot about recently: Elixir.

Image for post
Image for post

Here’s a little background behind why I chose Elixir. As someone who writes a lot of Ruby code for a Rails 5 application, Elixir share a lot of the clean syntax that Rubyists have come to expect and love. This should be no surprise though because Elixir was created by a former Ruby core developer, José Valim. Another reason Elixir has piqued my interest is because unlike JavaScript and Ruby, it’s a functional programming language with a focus on concurrent applications.

As part of my goal for learning Elixir, I’ve purchased Programming Elixir 1.6 by Dave Thomas and plan on providing my highlights for each chapter. I plan on completing most of the exercises and sharing them here as well. So let’s get started!

Chapter 1

  1. Object Oriented Programming

Object oriented programming languages (like Ruby) focus on classes and objects. Classes represent the templates from which objects can be instantiated into existence. In addition to being the blueprint for objects, classes also define the behavior that each object can perform (i.e. think methods). Objects, on the other hand, are used to maintain its own internal state and at any given time, can be asked for its current state (i.e. think instance variables).

The difficulty with OOP is that as your application grows in size, it becomes more and more difficult to remember what state your objects are in. Since the object’s methods mutate its state, you can not reliably predict the outcome of calling a function without knowing the history of what has been called before.

2. Functional Programming

Functional programming is all about transforming data. There are no classes or objects, and instead at the most basic level, you only have raw data and the functions that transform it. In this regard, you can think of it like an assembly line where you start with your initial data, and apply many small functions to get your final data. In functional programming, these functions are often described as being pure, which just means that given only the inputs you provide to the function, you’ll know exactly what the output of the function will be each and every time. One interesting point that David makes is that in functional programming, functions maintain all the state in the arguments that are captured when a function is called.

Similar to how classes can be thought of as the organizational structure for objects in OOP, functions are organized in modules in functional programming languages. However, modules don’t imbue any new abilities to the functions — they just act as a name-spacing and grouping for the functions.

Chapter 2

  1. Pattern Matching in Elixir
Image for post
Image for post
Pattern matching in Elixir — where you match by both structure and value!

Unlike other programming languages, the = sign does NOT mean assignment. In Elixir, the = sign is the match operator and it’s more of an assertion between the 2 expressions on the left and right hand sides of the = sign. In order for a match to happen successfully, the left hand side of the match operator must be able to match both the structure and value on the right hand side. Take a look at the examples and explanations below:

Pattern Matching examples in Elixir (with explanations)

In my next blog post, we’ll go over Chapter 3 in Programming Elixir 1.6 which focuses on immutability in Elixir.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store