Unlock the Power of IEx: Exploring Elixir's Interactive Shell

abreujp

João Paulo Abreu

Posted on June 29, 2024

Unlock the Power of IEx: Exploring Elixir's Interactive Shell

In this article, we'll explore Interactive Elixir (IEx), a powerful tool for experimenting with code and understanding Elixir's basic syntax. IEx is a REPL (Read-Eval-Print Loop) that enables real-time interaction with Elixir. With it, you can test commands, functions, and learn the language fundamentals in a practical and dynamic way. IEx is ideal for developers who want to explore and experiment with their ideas quickly.

For more information, check out the official IEx documentation.

This article is aimed at Elixir beginners, experienced developers in other languages who want to learn Elixir, or those looking to deepen their knowledge of the IEx tool.

Installing Elixir and IEx

To install Elixir and IEx, read my previous articles where I show the necessary commands to install Elixir on Linux distributions Ubuntu 24.04 or Fedora 40.

Starting IEx

To start IEx, open the terminal and type:

iex
Enter fullscreen mode Exit fullscreen mode

You'll see a prompt where you can start typing Elixir commands.

Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]
Interactive Elixir (1.17.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Enter fullscreen mode Exit fullscreen mode

To exit IEx, you can use the shortcut Ctrl + C twice.

Basic Commands in IEx

IEx allows the execution of various basic commands that help in understanding the language. Here are some examples:

Exploring Elixir Syntax

  • Basic Math
iex(1)> 2 + 3
5
Enter fullscreen mode Exit fullscreen mode
  • String Manipulation
iex(2)> "Hello, " <> "world!"
"Hello, world!"
Enter fullscreen mode Exit fullscreen mode
  • Using Variables
iex(3)> name = "John"
"John"
Enter fullscreen mode Exit fullscreen mode

Math and Operators

You can use basic mathematical operators in IEx to perform calculations:

  • Division: /
  • Integer division: div
  • Division remainder: rem
iex(4)> 10 / 2
5.0
iex(5)> 10 div 2
5
iex(6)> 10 rem 3
1
Enter fullscreen mode Exit fullscreen mode

String Manipulation

Elixir has useful functions for string manipulation:

  • Length: String.length()
  • Convert to uppercase: String.upcase()
iex(7)> String.length("Elixir")
6
iex(8)> String.upcase("elixir")
"ELIXIR"
Enter fullscreen mode Exit fullscreen mode

Variables and Immutability

In Elixir, variables are immutable. This means that once assigned, their value cannot be changed. When you assign a new value to a variable, a new variable is created, keeping the name but with the updated value:

iex(9)> x = 10
10
iex(10)> x = 20
20
iex(11)> x
20
Enter fullscreen mode Exit fullscreen mode

In this example, x initially has the value 10. When we assign 20, x keeps the new value, but the previous value is not changed because variables are immutable.

Data Structures

Elixir offers various data structures, such as lists and tuples:

  • Lists: []
  • Tuples: {}
iex(12)> list = [1, 2, 3]
[1, 2, 3]
iex(13)> tuple = {:ok, "success"}
{:ok, "success"}
Enter fullscreen mode Exit fullscreen mode

Functions and Pattern Matching

Anonymous Functions

You can create anonymous functions directly in IEx:

iex(14)> greeting = fn name -> "Hello, #{name}!" end
iex(15)> greeting.("World")
"Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Pattern Matching

Pattern matching is one of Elixir's most powerful features:

iex(16)> {a, b} = {1, 2}
iex(17)> a
1
iex(18)> b
2
Enter fullscreen mode Exit fullscreen mode

IEx Features

h/1 Command for Documentation

The h/1 command provides documentation for modules and functions:

iex(19)> h Enum.map
Enter fullscreen mode Exit fullscreen mode

i/1 Command for Variable Information

iex(20)> name = "John"
iex(21)> i name
Enter fullscreen mode Exit fullscreen mode

IEx Tips and Tricks

  • Use tab to autocomplete function and module names.
  • Use h() for help with IEx shortcuts.

Practical Examples

Let's see a practical example of using IEx to calculate the sum of a list:

iex(22)> Enum.sum([1, 2, 3, 4])
10
Enter fullscreen mode Exit fullscreen mode

Conclusion

IEx is a powerful tool for any Elixir developer. It allows for quick experimentation, continuous learning, and exploration of the language's capabilities. Use IEx to enhance your skills and develop projects efficiently.

Next Steps in Elixir

In the next article, we'll begin to detail data types in greater depth.

💖 💪 🙅 🚩
abreujp
João Paulo Abreu

Posted on June 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related