You are on page 1of 5

CHAPTER 1

Getting Comfortable

The easiest place to start learning Elixir is in Interactive Elixir, IEx. This command-
line interface is a cozy place to get started and a good place to start figuring out what
works and what doesn’t work in Elixir. Its features will spare you headaches later, so
settle in!

Installation
Because Elixir runs on top of Erlang, you’ll need to install Erlang on your system first,
and then install Elixir.

Installing Erlang
If you’re on Windows, installing Erlang is easy. Download the Windows binary file,
run the installer, and you’re set. If you are a brave beginner tackling your first pro‐
gramming language, this is easily your best bet.
On Linux or macOS, you may be able to download the source file and compile it. On
macOS you should be able to unzip and untar it and then, from the directory created
by the untarring, run ./configure, make, and sudo make install. However, that
simple sequence works only if you have the right files installed, and it can give you
mysterious errors if they aren’t. In particular, Apple’s shift to the LLVM compiler in
newer versions of XCode instead of GCC makes it less likely that GCC will be on
newer macOS systems, and Erlang needs GCC.
You can ignore the error about FOP, which Erlang uses to generate PDF documenta‐
tion you can download elsewhere. Also, on newer Macs, you’ll get an error at the end
that wxWidgets doesn’t work on 64-bit macOS. For now, ignore this too.

1
If the compilation approach doesn’t work or isn’t for you, Erlang Solutions offers a
number of installs. Also, many different package managers (Debian, Ubuntu, Mac‐
Ports, Homebrew, and so on) include Erlang. It may not be the very latest version, but
having Erlang running is much better than not having Erlang running. They do tend
to make it run on the latest version of various operating systems, so if you have instal‐
lation problems, look closely at their requirements.

Erlang is increasingly part of the default installation on many sys‐


tems, including Ubuntu, largely thanks to the spread of CouchDB.

Installing Elixir
Once you have Erlang installed, you should be able to download a precompiled ver‐
sion of Elixir or the GitHub source. Some package managers are starting to support
Elixir, including Homebrew. The code in this book should work with Elixir 1.3.0.
Then you need to set your path so that it can find elixir/bin.
Elixir’s instructions for setup are organized into a tutorial.

Firing It Up
Go to the command line (or shell, or terminal) and type mix new first_app. This
will invoke Elixir’s Mix tool, which “provides tasks for creating, compiling, and test‐
ing Elixir projects, managing its dependencies, and more.” In this case, the command
you type creates a new, empty project in a directory named first_app:
$ mix new first_app
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/first_app.ex
* creating test
* creating test/test_helper.exs
* creating test/first_app_test.exs

Your Mix project was created successfully.


You can use "mix" to compile it, test it, and more:

cd first_app
mix test

2 | Chapter 1: Getting Comfortable


Run "mix help" for more commands.

Make sure that the directory containing the Elixir executable is in


your $PATH variable so that Mix can find it.

Rather than compiling and testing the empty project, go into the first_app directory
and start the IEx shell with these commands:
$ cd first_app
$ iex -S mix
You’ll see something like the following code sample, likely with a cursor next to the
iex(1)> prompt. Note that where necessary in the book, some of the longer lines
have been reformatted to fit on the page:
$ cd first_app
[david@localhost first_app]$ iex -S mix
Erlang/OTP 19 [erts-8.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe]
[kernel-poll:false]

Compiling 1 file (.ex)


Generated first_app app
Interactive Elixir (1.3.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
You’re in Elixir! (The first line about Erlang reflects that Elixir runs within Erlang.
Don’t worry about that part!)

First Steps
Before moving on to the excitement of programming Elixir, it’s always worth noting
how to quit. The shell suggests Ctrl+C, which will bring you to a menu. If you press
“a” in that menu, IEx will stop, and you’ll see whatever prompt you had before start‐
ing IEx:
iex(1)>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
a
$

You can also ask iex (once you start it up again) for help, by entering h() or just h:

First Steps | 3
iex(1)> h()
# IEx.Helpers

IEx.Helpers

Welcome to Interactive Elixir. You are currently seeing the documentation for
the module IEx.Helpers which provides many helpers to make Elixir's shell more
joyful to work with.

This message was triggered by invoking the helper h(), usually referred to as
h/0 (since it expects 0 arguments).

You can use the h function to invoke the documentation for any Elixir module or
function:

┃ h Enum
┃ h Enum.map
┃ h Enum.reverse/1

You can also use the i function to introspect any value you have in the shell:

┃ i "hello"

There are many other helpers available:


...
:ok

So what have you done here? You’ve issued an iex command, calling a helper func‐
tion, h(), that provides you with some basic help information. It printed a lot of
information to the screen and then ended, returning :ok.

Moving Through Text and History


If you explore the shell, you’ll find that many things work the way they do in other
shells, or in Emacs. The left and right arrow keys move you backward and forward
through the line you’re editing. Some of the key bindings echo those of the Emacs
text editor. Ctrl+A will take you to the beginning of a line, while Ctrl+E will take you
back to the end of the line. If you get two characters in the wrong sequence, pressing
Ctrl+T will transpose them.
Also, as you type a closing parenthesis, square bracket, or curly brace, the cursor will
highlight the corresponding opening parenthesis, square bracket, or curly brace.
The up and down arrow keys run through the command history, making it easy to
reissue commands. You can reference a given result value with v(N), where N is the
line number.

4 | Chapter 1: Getting Comfortable


Moving Through Files
IEx does understand filesystems to some extent, because you may need to move
through them to reach the files that will become part of your program. The com‐
mands have the same names as Unix commands but are expressed as functions. IEx
starts wherever you opened the shell, and you can figure out where that is with pwd():
iex(1)> pwd()
/Users/elixir/code/first_app
:ok

To change directories, use the cd() function, but you’ll need to wrap the argument in
double quotes:
iex(2)> cd ".."
/Users/elixir/code
:ok
iex(3)> cd "first_app"
/Users/elixir/code/first_app
:ok

You can look around with the ls() command, which will list files in the current
directory if you give it no arguments, and list files in the specified directory if you
give it an argument.

Doing Something
One of the easiest ways to get started playing with Elixir is to use the shell as a calcu‐
lator. Unlike with your typical command line, you can enter mathematical expres‐
sions and get useful results:
iex(1)> 2 + 2
4
iex(2)> 27 - 14
13
iex(3)> 35 * 42023943
1470838005
iex(4)> 4 * (3 + 5)
32
iex(5)> 200 / 15
13.333333333333334

The first three operators are addition (+), subtraction (-), and multiplication (*),
which work the same way whether you’re working with integer values or floating
points. Parentheses let you modify the order in which operators are processed, as
shown on line 4. (The normal order of operations is listed in Appendix A.) The
fourth operator, /, supports division where you expect a floating-point result (a num‐
ber with a decimal part), as shown on line 5. You don’t have to put spaces around
operators, but it can make your code more readable.

Doing Something | 5

You might also like