September 8, 2020

Learning Elixir, a Functional Language

So, I have a weird fascination with checking out new languages. I'm a Ruby developer by trade. But, that's not enough for me. So, I often find myself dabbling in other languages to see what they have to offer. Nothing against Ruby either, I love it.

My latest forays have been into Crystal (which is nearly identical to Ruby, but compiled), and Elixir. I wanted to give Elixir a shot because the syntax is kinda-sorta similar to Ruby in some ways, and it is a purely functional language which is something I have yet to really dive into.

What is Elixir?

José Valim is the creator of the Elixir programming language, a research and development project of Plataformatec. His goals were to enable higher extensibility and productivity in the Erlang VM while keeping compatibility with Erlang's ecosystem.

José Valim aimed to create a programming language for large-scale sites and apps. Being a Ruby developer, he used features of Ruby, Erlang, and Clojure to develop a high-concurrency and low-latency language. Elixir was designed to handle large data volumes. Its speed and capabilities spread Elixir in telecommunication, eCommerce, and finance industries.

Elixir was first introduced in 2012. It is a general-purpose, functional language designed for building scalable and maintainable applications.

The language complies with the bytecode seen on the Erlang VM (also known as BEAM). Its syntax is often compared to the ever popular Ruby on Rails development framework, and it arguably exceeds it, given that the productivity Elixir allows for is not counterbalanced by lagging performance, as it is with Rails. But, other than syntax, it's a pretty different world as it is a functional language.

  • Good syntax (just my opinion)
  • The API is pretty stable now and I believe the core language is pretty much complete. Because of, upgrading is fairly easy.
  • Community is friendly and inclusive.
  • Scales well and a great fit for real time web.
  • The main ecosystem (Phoenix, Ecto, etc) are excellent.
  • Great macros support.

The main downsides for me are: - There aren't as many third party libraries compared to PHP/Ruby, so development tends to take longer. - Deployment is better than before, but not as simple as PHP. - No static types.

My experience with the language thus far is pretty minimal. I have made a CLI number guessing game and I have played around with the Phoenix Framework a bit, which is very interesting.

Here is my simple guessing game if you want to see how Elixir looks:

defmodule GuessingGame do
  # Guess between range of numbers.
  # Tell user guess.

  def start do
    range = IO.gets("What range is your number in(ex. 1-100)? \n")
            |> String.trim
            |> String.split("-")
    low = List.first(range)
    high = List.last(range)

    guess(String.to_integer(low), String.to_integer(high))
  end

  def guess(low, high) do
    answer = IO.gets("Is your number #{mid(low, high)}? \n")

    case String.trim(answer) do
      "bigger" -> bigger(low, high)
      "smaller" -> smaller(low, high)
      "yes" -> "I'm always right!"
      _ -> IO.puts ~s(Please type "bigger", "smaller", or "yes".)
           guess(low, high)
    end
  end

  def mid(low, high) do
    div(high + low, 2)
  end

  def bigger(low, high) do
    new_low = min(high, mid(low, high) + 1)
    guess(new_low, high)
  end

  def smaller(low, high) do
    new_high = max(low, mid(low, high) - 1)
    guess(low, new_high)
  end
end

I'm planning to keep on exploring and improving in this language. The functional approach to programming is quite different from what I'm used to with Ruby. No objects or state to deal with. Just functions and more functions.

Anyone else here have experience with Elixir or functional programming? If so, what do you find as the up and down sides of it?

tags: programming elixir code