- Chapter 1 and 2 — OOP, Functional Programming, and Pattern Matching (this post)
- Chapter 3 — Immutability
- Chapter 4 — Basic Elixir Types
- Chapter 5 — Anonymous Functions
- Chapter 6 — Modules and Named Functions (coming soon)
- Chapter 7 — Lists and Recursion (coming soon)
- Chapter 8 and 9 — Maps, Structs, and Keyword Lists in Detail (coming soon)
- Chapter 10 — Processing Collections (coming soon)
- Chapter 11 — Strings and Binaries (coming soon)
- Chapter 12 — Control Flow (coming soon)
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!
- 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.
- Pattern Matching in Elixir
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:
In my next blog post, we’ll go over Chapter 3 in Programming Elixir 1.6 which focuses on immutability in Elixir.