Introduction and Setup


Installing Elixir

Installation instructions for different operating systems can be found on the Elixir website.

Reflection

Congratulations! Take a moment to pat yourself on the back. You've already made a great decision by choosing to learn a functional language. The current and future state of web applications requires a robust development solution. Functional programming delivers a level performance and scalability that imperative languages are incapable of providing.

If your goal is to produce fast, fault-tolerant, and scalable applications, you're reading the right book. Elixir harnesses the power and reliability of the battle-tested Erlang Virtual Machine. We'll explore this further in the next chapter. For now, let's start by exploring Elixir.

Elixir is a modern language that first appeared in 2011. However, it's even newer than that as the current version more closely resembles the release of 1.0, which occurred in September of 2014. It enjoys what's known in business as the "second-mover" advantage. In a competitive market, the second-mover advantage is the distinct benefit an organization receives from studying and adapting the business strategies of organizations that arrived to a market first. Second movers observe how other organizations operate, then tailor their strategy accordingly. This allows them to mimic the strategies that were succesful while avoiding any pitfalls that may have plagued other companies. How does this strategy relate to José Valim and Elixir's core contributors?

While creating Elixir, José was able to take the best parts of other languages and include them in Elixir. He realized the profound power of the Erlang Virtual Machine and used this as the foundation on which to build a new language. He combined the benefits of functional programming with a modern Ruby-inspired syntax. This makes writing Elixir code a joy while providing the benefits of immutability, pattern matching, first-class functions, and much more. Elixir also includes protocols for polymorphism, full fledged lexical closures and complete Erlang interoperability. Furthermore, it's Lisp-style macros give Elixir the power of metaprogramming, making it extensible. With all of these features combined (which are by no means an exhaustive list) Elixir is a modern language powerhouse that makes writing highly concurrent and distributed applications easy and enjoyable.

Interactive Elixir

Elixir comes with a REPL (Read Evaluate Print Loop) shell that can be started by typing the letters iex into a terminal.

After installing Elixir, verify the installation by checking the version number with elixir -v. Your version number may differ from mine but you should see something like this:

Erlang/OTP 20 [erts-9.1.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Elixir 1.5.2

Once installed, begin an iex session by typing iex and pressing enter. Notice the prompt shows iex(1)> . Elixir will automatically provides line numbers, which store the return value of each line in the shells memory. We can then refer to these values using v(n) where 'n' represents the line number. See the example below:

#
iex(3)> 5 + 8
13
iex(4)> 13 + v(3)
26
iex(5)> 1 + v(4)
27
iex(6)> 1 + v(-1)
28

Line three contains the expression 5 + 8, which returns 13. In line four, we add 13 to the return value of line three, giving us a total of 26. On line five, we add 1 to the return value of line four. Then on line six we do something slightly different. Instead of referring to line five, we use a negative value (-1) to refer to the previous line. If we used v(-2) it would have referred to line four, v(-3) would refer to line two, and so on.

Being able to capture the output of previously executed code is very useful in Elixir. In the previous example, we are only using integers. However, the return value may be an atom, list, or other data type.

iex(10)> :hello
:hello
iex(11)> var = v(-1)
:hello
iex(12)> var
:hello

We'll return to this functionality later in the book when we discuss spawning new processes and capturing the Process Identifier (PID).

Configuration

Within iex, we can store variables, perform calculations, test code, and even debug our applications. Each iex session is independent. This means that variables stored in one session will not be available after the session in closed. Each time an iex shell is started we're given a blank slate. However, we can store variables and other options to be loaded each time a new iex session is started. The interactive Elixir configuration file is named .iex.exs, notice the file starts with a period ( . ). This means it's a hidden file. Elixir will look for this file in the current directory a shell is started in. If no configuration file is found, it will check the user's home directory. There are a number of configuration options available. See the documentation for IEx.

results matching ""

    No results matching ""