Elixir Programming Language

2611 VIEWS

· ·

For every letter of the alphabet in the English language, there are a number of programming languages. Some programming languages need no introduction, like Java, C++, Python, Go, and JavaScript. But there are many others you may have never heard of, let alone used. Here are just a few : C–, E, Xtend, Yoix, Golo, Qalb, Boo, Citrine, Nu, and Alice (congrats if you’re familiar with any of these). Programming languages may differ in many ways, such as syntax, purpose, support, programming paradigm, and even execution.

Why on Earth Do We Need All These Languages?

Most programming languages—at least, the widely used ones—tackle issues evident in their precursors or solve a problem completely from scratch. Take C++ programming language. It is inspired by C and has inspired many other languages, including Java. C++ brought the concept of object-oriented programming to C, at the same time maintaining the speed of the C language. Java, in turn, borrowed many concepts from C/C++ with cross-platform support in mind. PHP is another popular programming language that was written in C by Lerdorf Rasmus. It began as a project to maintain his personal homepage, but now is used extensively by Facebook.

No one language has it all. C is powerful, yet it’s not object-oriented. Java is good for many tasks, but Java has verbose syntax even for small programs. Go(lang) is a fairly new programming language with strong support for concurrency; however, it is still young and does not have much support compared to older languages like Java. Often, the use of a language is purely a matter of preference. I find Java interesting and tend to use it for most of my work. My friend, however, prefers Python as he doesn’t usually have to type as much as I do for a similar task.

Another Language: Elixir

Elixir is a functional programming language created by José Valim that runs on the Erlang Virtual Machine. The language first appeared in 2011, and is already being used by some big corporations in production. Bleacher Report, for instance, has reported a major boost in performance after switching to Elixir. Pinterest also uses Elixir extensively in dealing with spam.

Elixir promises and delivers scalability. With Elixir, you can run hundreds and even thousands of processes concurrently on a single system, and on distributed systems while maintaining communication between them. As a functional programming language, it is terse. Another plus for Elixir is that it is fault-tolerant. The language provides a mechanism to deal with malfunctions, allowing a system to be restored to a known working state. Moreover, Elixir has awesome documentation to get you started.

We’ll examine Elixir syntax in this section to see how it compares with other programming languages. Elixir will need to be installed. See the instructions below:

 Windows
The link to the installer is here. 

Linux
You can install Elixir by passing elixir to your package manager. 

#arch
sudo pacman -S elixir

#ubuntu
sudo apt install elixir

Mac
sudo port install elixir  
 --------OR------------
brew install elixir

You can run the Elixir REPL(iex) to begin writing in Elixir. Like many REPLs, you can perform some basic math here and try out your code. Scripts can also be written and compiled by passing the script name to elixirc. There are a couple of file extensions to understand .ex and .exs. The two are different in the sense that .exs files are meant for scripting, and .ex files are meant for compilation. A programming language tutorial is never complete without the famous Hello World program.

Here’s how to write Hello World in Elixir.

Save the following to a file with a .exs extension, then run the file from the command line with elixirc

 IO.puts "Hello World"               #save in hello.exs

elixirc hello.exs
> Hello World

Elixir Types

Elixir, like most other languages, has basic types: string, list, tuple, boolean, integer, float, and atom. The less obvious type, the atom, is simply a constant that has its name as value.

 #string
a = "Hello" 				#Note: delimited by double quotes

#list
my_list = ["first", "second"]

#atom
reaction = :wow				

#tuple
t = {"third", :wow}



#boolean 
choice = false

#integer
num = 10

#float 
fee = 119.3

Arithmetic + Operators

Arithmetic in Elixir produces results similar to those in other languages with a few exceptions. Normal division returns a float. For floor division, you have to use div, and <> is used for concatenation. There is support for scientific notation in floats, as well as support for arithmetic in other bases like hexadecimal. Many operators like == and || behave as expected.

 iex> 1 + 1
2

iex> 10 / 3
3.3333333333333335

iex> div 10, 3
3

iex> "Hello" <> " World"
Hello World

iex> false || true
true

Modules + Functions

Modules in Elixir are a collection of functions, similar to modules in Python and other languages. A module is defined with the defmodule macro. An example of a module is the IO module used in the Hello World program. Others include File, System, and Path. We’ll define a module with a function to see exactly how it’s done.

 defmodule Magic do
    def multiply(first, second) do
        first * second 
    end
end

IO.puts Magic.multiply(100, 10)

When compiled successfully, you should see an output of 1000.

Knowledge of basic types, operators, functions, and modules can be combined to create useful programs. And Elixir has several powerful features that allow one to create complex and powerful programs. Processes are an example. Several processes can run concurrently, each performing a specific task while communicating with others. Processes in Elixir consume less memory and CPU resources than operating system processes, making it possible to create functional, distributed systems.

The official site provides more details on language concepts and will help you get started with creating complete applications in Elixir.

Elixir is still a fairly new language with great potential. Some companies have adopted it in production. It may be helpful to add it to your programming arsenal.


Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government. Bruno is a regular contributor at Fixate IO.


Discussion

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu
Skip to toolbar