Writing your first package in Elixir (Part 1)

Learning by doing

Syed Faraaz Ahmad
codeburst

--

Elixir is a programming language that is the combination of (mostly) all things awesome. It combines Ruby’s expressive and easy to follow syntax with the stability of the Erlang VM. If you’ve ever heard of Elixir, you might know that it is relatively new to the world of development, especially when you compare it with other popular languages like C++, Python etc.

Learning a new language can get boring really quickly if you’re just reading its syntax and relating it to the language(s) you already know. For me, this was the case with Elixir. It’s syntax is heavily inspired from Ruby (no surprise there! the guy who made it has been an active Ruby developer for a DECADE!). So learning the language features quickly got boring for me. Don’t get me wrong, Elixir is awesome! Its support for concurrency and pattern matching is quite breathtaking! However, I needed to use it in a real life scenario to truly appreciate its features.

I had done a similar thing to learn Ruby where I made this thing called ShellRB. It’s like a UNIX shell using Ruby so it runs on Windows, Mac OS and Linux. So I thought, why not rewrite that in Elixir!

What?! No! That would be super boring!

Doing that same thing over again but in a different language would not be exciting at all! Like Albert Einstein once said:

Insanity is doing the same thing over and over again and expecting different results.

Although the origin of this quote remains a debatable topic, what’s not debatable is that doing the same thing all over again but in a different language is pretty insane and super boring. Let’s do something much more fun instead.

We’ll write a package to print random Chuck Norris quotes

That’s right! Such a useful, real-life scenario. We’ll access the free Chuck Norris API and get Chuck Norris facts from there. For this I’m assuming you have a slight knowledge of Elixir, although I’ll explain the gist of it, you should at least have a tiny bit of knowledge about it. Elixir School and the official Elixir getting started guide are really good places to initially learn Elixir.

Lets Begin!

First off, open up your terminal and type this to create a new Elixir project using mix, lets call it norris and go into that folder:

$ mix new norris && cd norris

The mix.exs file in your directory is what contains all the info about your project, i.e. name, version number, description, authors, dependencies etc.

If you check out the lib folder, you can see the norris.ex file, which is where we’ll write all of our code. Open the norris.ex file and you’ll see something like this

defmodule Norris do
@moduledoc """
Documentation for Norris.
"""
@doc """
Hello world.
## Examplesiex> Norris.hello
:world
"""
def hello do
:world
end
end

@moduledoc and @doc are just descriptions about the current module and the following function respectively. The defmodule keyword defines a module. Get it? Module is just a big box to put all your functions inside. So now, from where do we get these Chuck Norris jokes? There’s an API for that! Head over to this link and you’ll see what I mean. With that out of the way, we can easily get a random Chuck Norris fact by sending an HTTP request to the API and parsing the JSON response. To do that, we’ll use Tesla (an HTTP client library for Elixir) and Poison (a library for parsing JSON).

To include these dependencies in our project, we head back to mix.exs and find the deps function and change it to look like this:

defp deps do
[
{:tesla, "~> 0.9.0"},
{:poison, "~> 3.1"}
]
end

You might have noticed the defp keyword, this is just to define a function private to a module, i.e. only functions inside this module can access our deps function. Getting back to the norris.ex , delete the hello function and make a function named random , like so:

def random do
"random joke"
end

Now go back to your terminal and:

  1. Install your dependencies:
$ mix deps.get

2. run your Elixir project in IEX (Interactive EliXir Shell)

$ iex -S mix

you’ll get an output that looks something like this:

Erlang/OTP 19 [erts-8.3.5.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]Interactive Elixir (1.4.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

Now if you call your random function, you should see your output:

iex(1)> Norris.random
"random joke"

Cool! Now we know it works.

Let’s quickly head over to part 2 on info about adding functionality and actually returning the chuck norris facts.

Part 2 >

--

--

Software Engineer. I like to build cool things and write about them.