Brief Intro Into Elixir

Ian Charles
Nov 1 · 3 min read

As a programmer the goal is always to write maintainable and scalable code. Following the Functional Programming paradigm can help assist in reaching those goals. Before diving into elixir lets take a quick detour into functional programming. So what is Functional Programming ? Functional Programming is a paradigm or style that focuses on immutability and pure functions. It evolved from lambda calculus, which is a mathematical system built around function abstraction and generalization. Now lets get back on track with Elixir.

So first thing first. What is Elixir? Elixir is a functional programming language that runs on the Erlang virtual machine. It’s open source and provides design and production tools. By leveraging the Erlang Virtual Machine, it shares similar abstraction like distributed and fault-tolerant systems, and also being used in web development and the embedded software domain.

Lets look at some code in Elixir to get a better picture of how it works.

All methods in Elixir are created using the def keyword, a name, and a code block. Instead of using curly braces to determine when a method starts and finishes, Elixir uses do and end. Anything that goes inside the do and end is considered inside of the code block. Above we have a method called create_deck which is meant to create a small deck of cards. Inside of the method we have two lists/arrays. One in which we will store our value of each card and the other which will store the suit. In order to iterate through each collection we have to write a loop. Similar to a JavaScript for of loop, Elixir uses a backwords arrow instead of “of” a name for each value and the collection itself. So with the code above we can generate a small deck of playing cards .

So now we can move on to actually shuffling our deck of cards. Here is where immutability comes into play major. Elixir doesn’t know anything about a instance of or inheritance. The Elixir standard library has a ton of modules with methods that can be used to help do what you need to. In this instance we are using the Enum module that has a set of algorithms that enumerate over enumerables. So instead of having to write our own function that shuffles a list of values we can just use this already built in method.

So every time we create a new deck of cards, we pass in our list of values and suits, and the shuffle methods makes a copy of that list and spits out the newly shuffled deck, not modifying our original list.

Elixir has tons and tons of more features that i haven’t covered, but this was just an icebreaker for those who haven’t yet used this dynamic programming language. However i encourage anyone who enjoys learning and using different technologies to give it a try. Hope this article was helpful for anyone who is just beginning to use and learn 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