Elixir — A Tincture for Functional Programming Part 1.2 Functions

Kyle Ledoux
3 min readFeb 2, 2022

Fun with Functions

As I continue to learn about and work with Elixir, more and more interesting tidbits continue to surface. Some of these are related to Functional Programming itself and others are specific to the language of Elixir. But, if I find it interesting and/or useful, I will write about it in this series that loosely follows my progress.

large lego blocks being placed down in a stack

Legos

This time around, I get into some more substantial Elixir: working with Functions. After all, they are the building blocks of all programs in Functional Programming, so it’s important to have a good baseline for how functions work in Elixir.

In the Functional Programming paradigm, we construct our programs not using a bevy of objects that carry state and a set of methods that interact with those internal states as we might when working with Object Oriented Programming, but rather with a set of functions that have no functional dependencies resulting from internal state.

Functions in Elixir do three things:

  1. Take data
  2. Operate on the data
  3. Return some values

We construct Elixir programs using short, simple functions that build on one another to create a data transformation pipeline. This circumvents many internal state dependency problems but also has the benefit of making programs easy to understand and maintain!

A Good Name

There are different types of functions in Elixir, but I have found it easy to break them into two very broad categories: named and anonymous.

Named functions are defined inside modules in Elixir. We can create our own modules and define named functions inside them for later reuse.

Elixir does have a host of useful named functions that we can leverage to accomplish a wide range of tasks.

There are a bunch, and you can probably find one that will cover most any simple task you encounter. Check here to find the different modules and the named functions they provide!

Lambdas Everywhere

In addition to defining and utilizing named functions in Elixir, we can also create Anonymous Functions, also known as lambdas.

Anonymous functions are so called because they have no global name and to use them, you must assign them to a variable.

Defining them is relatively simple, we just need to use the special form for doing so with the `fn` and `end` keywords. Special forms in Elixir cannot be overridden, so we know that this format should also work as we expect it to:

Extra Legroom

One last interesting feature of functions in Elixir is that they are treated as first-class citizens. This means that they can be passed into other functions as arguments. This is because all functions are values of type `function`, so they can be passed in and utilized in a function call.

This is really useful for adhering to the tenets of Functional Programming and creating declarative code:

That’s it for now! Go have some fun with functions!

--

--

Kyle Ledoux

I’m a software engineer with a talent for distractable curiosity and a passion for questionable humor.