Jul 6, 2016 · 5 min read

Functional Programming & Elixir — Pt. 1, The Basics

It might be difficult to get into functional programming because of the complexity and the terminology. The goal of these articles is to explain the terminology in a simple manner, reduce the complexity and at the same time, give code samples written in Elixir and show how Elixir helps us with functional programming. This first article is all about functions, variables and patterns.

# Functions

## First-class functions

This doesn’t say much, but it basically just means that functions can be stored in variables, that you can pass them to other functions and invoke them there. In Elixir these are called anonymous functions.

You also have named functions.

These cannot be passed down to other functions. But you can convert them to anonymous functions.

There’s also another syntax which you’ll see sometimes, for example:

So what is this `&(&2 <> &1)` thing? It’s a concise anonymous function, a so-called partially applied function. Where `&1` is the first argument of the function and `&2` the second.

To fully explain the example, `<>` concats the two strings (ie. the two function arguments). Check the Elixir docs for more info about the `Enum.reduce` function.

## Composing

Composing, function composition, is essentially executing a sequence of functions. That is, the result of each function is passed as the argument of the next.

Elixir provides a way to make it more clear what we’re trying to do here. This operator is called the pipe operator.

This is the syntax for anonymous functions, which looks a bit odd, if I were using named functions I could omit the trailing `.()`

## Closures

A closure is a function which has the following properties:

• Is a first-class function
• Remembers the values of all the variables in scope when the function was created

`:math` is a Erlang module, which in Elixir are represented by atoms. Elixir runs on top of the Erlang VM, which makes it possible to use Erlang code with Elixir

## Higher-order functions

A higher-order function (HoF) is a function which takes one or more functions as arguments and/or a function that returns a new function, which is a closure.

Example

This example sums it up, we have:

• First-class functions
• Function composition in the form of a reducer
• A closure, the function returned by the `wrap` function
• A higher-order function named `wrap`

# Pattern matching & Immutability

In Elixir the `=` operator doesn’t only do assignment, but also pattern matching. That’s why it’s called the match operator. This also means that variables aren’t really places in memory that values are stored in, but rather
labels for the values. That is, Elixir/Erlang allocates memory for the values, not the variables.

We can also do destructuring:

Why this is useful

Say you have a function that returns a tuple, but you don’t want to do this all the time:

Wouldn’t it be way easier if we could just write:

Yes, it is.

## Immutability

With that in mind, in Elixir, all data types are immutable.

Why this is useful

Imagine if we didn’t have immutable date types.

`%{}` is a key-value store, a map

In this case `func_b` would break, because it depends on the `example` property and we deleted that property in `func_a`.

If we apply immutability, for example, `Map.delete` produces an error,
or as in Elixir, `Map.delete` returns a new map and doesn’t touch the original map, `func_b` will work fine.

# Conclusion

I think we have discussed all the basics of functional programming and Elixir. Except for some basic Elixir types, but you can look those up easily. Elixir has some pretty good getting-started guides.

Next up in the functional programming category is what to do and what not do with functions. Or to put in technical terms, limiting side-effects, data-first and using map and reduce instead of a loop.

Thanks to Brooklyn Zelenka and Izaak Schroeder for all the help!

Written by

Written by