What is Elixir?
Elixir is a dynamically typed, compiled, functional programming language built on top of the Erlang virtual machine best used for applications that are highly concurrent. Erlang was created in 1986 by Ericcson to create more reliable phone switching. Elixir was created on top of the Erlang Virtual Machine called BEAM, which allows Elixir to have the same functionality as Erlang while having a different syntax and features. Elixir uses a very Ruby like syntax that you'll see in the examples below. To learn more about the difference between functional and object oriented programming I have resources linked at the bottom of the article.
To install Elixir, head over to this link and follow the instructions depending on your operating system. Since I'm on Mac and use Homebrew all I had to do was type
brew install elixir into the terminal to install everything I needed. If you use VSCode you can also install this extension to help with things like auto completion and syntax highlighting.
IEX in your terminal. After starting your REPL to make sure it's working try typing
IO.puts "Hello, world!" in the terminal, this should output two things, first you'll see the string
Hello, world! printed then you'll see
As I said earlier, Elixir uses a Ruby like syntax. If you have spent any amount of time building projects with Ruby, learning and reading Elixir will probably come pretty quickly. As an easy walk through I'll show you how we can create a program that solves FizzBuzz, the code for this demo came from here. I wish I could say I learned Elixir in 3 days but I've only just scraped the surface, check out that link if you're interested in another possible way to solve fizzbuzz and I'll also have a link with more solutions at the bottom of the article. This link has a working version of what we're after if you're not familiar with FizzBuzz.
First, create a new directory for this file. To do this you can type
mkdir elixir-fizzbuzz in your terminal. Next, you'll need to change directories into your new directory by running the following command
Now that you're in your new directory you can create a new Elixir file. There are a couple different file extensions you can use to create an Elixir file, each with a slightly different use case. This stack overflow question has a good answer for when to use which type of file extension. For this example we'll create a file called 'fizzbuzz.exs', to do this you'll need to run the command
After creating the file open the directory in your text editor of choice.
First we'll create a module
#Define module defmodule Solution do end
Next we'll start writing our functions
#Define module defmodule Solution do #Define type specs @spec fizz_buzz(n :: integer) :: [String.t] #Define FizzBuzz function def fizz_buzz(n) do #Loop through range of numbers for i <- 1..n do end end end
Now we can call a private function that checks whether or not each number is divisible by 3 and/or 5.
#Define module defmodule Solution do #Define type specs @spec fizz_buzz(n :: integer) :: [String.t] #Define FizzBuzz function def fizz_buzz(n) do #Loop through range of numbers for i <- 1..n do #Call Result() for each number in the range result(i) end end end #Handle divisble by 3 & 5 defp result(n) when rem(n, 15) == 0, do: IO.puts("FizzBuzz") #Handle divisble by 3 defp result(n) when rem(n, 3) == 0, do: IO.puts("Fizz") #Handle divisble by 5 defp result(n) when rem(n, 5) == 0, do: IO.puts("Buzz") #Handle not divisble by 3 or 5 defp result(n), do: IO.puts(Integer.to_string(n)) end #Call function fizz_buzz/1 with range of 100 from Solution module Solution.fizz_buzz(100)
Now that our functions are all defined lets run our code, to do that in your terminal type the command
Uses for Elixir
If you check the Stack Overflow developer survey you'll see there aren't really a lot of developers actually using Elixir professionally, with just 1.74% of respondants saying it's a language they use. It is however top 5 in the loved vs dreaded category, meaning most people that use it love it. Elixir is often used on projects that need to support a very large user base all at once, as well as projects that require fast responses. As of right now on there are 12 companies on the Elixir lang website that are currently using Elixir as a solution to a variety of problems. For example, Discord uses Elixir as the foundation of its messaging service which supports 100 million monthly active users. Change.org uses Elixir as they found it faster than the comptetion. There hasn't been a lot of uptake in Elixir or functional programming as a whole, so it may not be the most useful skill to learn if you want to get job ready for as many jobs as possible. If instead you are trying to figure out how you can create an app that is highly scalable and performant, Elixir may be a good option for you.
Here are a few resources that may come in handy as you learn more about Elixir:
Top comments (0)