Elixir 101: A Quick and Easy Guide to Getting Started

Get Elixir Up and Running in No Time with This Quick Guide

ยท

3 min read

Elixir 101: A Quick and Easy Guide to Getting Started

Introduction

Elixir is a modern and dynamic language built on the Erlang virtual machine. It's simple, with a syntax similar to Ruby, and a strong focus on functional programming. Elixir is a popular choice for building distributed systems, real-time and web applications, thanks to its scalability, reliability, and fault-tolerance. It's an accessible language for developers new to programming, but it's also powerful enough to handle millions of users. With Elixir, you can build complex and reliable systems that are easy to reason about, test, and maintain.

Elixir is being used by some of the well-known tech giants like Whatsapp and Discord to power their mission-critical applications and services.

Step 1: Installing Elixir

On macOS:
Using Homebrew, a package manager for macOS, and run the following command in the Terminal.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install elixir

On Ubuntu/Debian:
Adding the Erlang Solutions repository to the package manager, updating the package list, and installing Elixir by running the following commands in the Terminal

wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && sudo dpkg -i erlang-solutions_2.0_all.deb
sudo apt-get update
sudo apt-get install elixir

On Windows:
Downloading the installer from the Elixir website and following the instructions.

Step 2: Verifying the Installation

Once you have Elixir installed, you can check its version by running elixir --version. In case any errors mean something went wrong while executing Step 1.

Step 3: Getting started with Elixir

Getting started with Elixir is straightforward. After installing Elixir, you can quickly start experimenting with Elixir code using the interactive shell, iex. The iex shell is a powerful tool for inspecting and debugging Elixir code, and it offers many features, such as autocompletion, documentation lookup, and module reloading. To start the iex shell just run the command in the terminal.

here are some examples of using the Elixir iex (interactive shell) :

Arithmetic Operations
You can perform arithmetic operations in iex, just like you would in a regular Elixir program. Here are some examples:

Note that the / operator performs floating-point division by default. If you want an integer division, use the div function instead.

String Manipulation
You can manipulate strings in iex using various built-in functions. Here are some examples:

Working with Lists
You can work with lists in iex using various built-in functions. Here are some examples:

In the above examples, we create a list of integers, and then use the Enum.map function to square each element in the list, and the Enum.filter function to return only the odd numbers in the list.

Defining and Calling Functions
You can define and call functions in iex just like you would in a regular Elixir program. Here's an example:

In the above example, we define a module called MyModule with a single function called print. We can then call this function in iex by passing one argument.

Another way to execute Elixir code is to execute directly from the file. Here is an example of how to execute Elixir code directly from an .ex file:

  • Create a new file called hello.ex and add the following code:

      def MyModule do
          def welcome do
              IO.puts("Welcome To The Party")
          end
      end
      MyModule.welcome()
    
  • Open a terminal or command prompt and navigate to the directory where you saved the hello.ex file

  • Type the following command to execute the file:

      elixir hello.ex
    

You should see the output Welcome To The Party printed to the console.


Adios. ๐Ÿ‘‹

Connect with me:
Linkedin

ย