Elixir: the new kid in town

Functional Programming strikes back!

Theorem
Theorem in Theorem
Sep 8, 2015 · 8 min read

What is Elixir?

Elixir is a dynamic, functional language created by José Valim which runs on top of the Erlang Virtual Machine.

Dummy Application

For better understanding, let’s build a dummy application which parses an external API response.

Installation

I’m using OS X, so the installation is very straightforward:

$ brew install elixir
$ brew install node
$ mix local.hex 
$ mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v0.15.0/phoenix_new-0.15.0.ez

Creating the application

Bootstrap

$ mix phoenix.new /tmp/random_movies 
$ mix phoenix.new random_movies
$ mix phoenix.new random_movies --no-ecto 
$ cd random_movies 
$ mix do deps.get, compile
  • A view, the PageView
  • A template, index.html.eex
  • Providing functions which take raw data and make it easier for templates to consume.
  1. handling the response
  2. converting the JSON object into a Map

1. Fetching the OMDB API

We will receive the title to search from the controller, but only the title. We want to convert that string into a valid API resource.

2. Handling JSON response

To tackle the second step, I need to introduce you a not-so-new concept: Pattern Matching

3. Converting JSON to Movie Struct

Finally we got here! We hit the API, we got a successful result and we have a valid JSON waiting to be parsed and displayed in our front page.

get "/", PageController, :index
get "/:query", PageController, :index

Use the application

Play around with the dummy application we built. Here is a list of my favorite movies if you are lazy and don’t want to look out for movies:

Source Code

If you just want to review the code, or download and run the server immediately here is the source code.


Theorem

We are an engineering and innovation firm. We solve complex business problems through optimization of process, technology, and culture.

6

6 claps
Theorem

Written by

Theorem

Innovate like a startup even if you aren’t one

Theorem

Theorem

We are an engineering and innovation firm. We solve complex business problems through optimization of process, technology, and culture.