for building scalable and maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain. Create by José Valim.
of computer programs, that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Calling a function f twice with the same value for an argument x will produce the same result f(x) each time.
code ➜ iex Interactive Elixir (1.2.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> 1 + 1 2 iex(2)> IO.puts "Hello world from Elixir" Hello world from Elixir :ok
run elixirc ➜ cat math.ex defmodule Math do def sum(a, b) do a + b end end ➜ elixirc math.ex ➜ ll -rw-r--r-- 1 angusmiller staff 1.3K Feb 23 14:34 Elixir.Math.beam -rw-r--r-- 1 angusmiller staff 57B Feb 23 14:31 math.ex -rw-r--r-- 1 angusmiller staff 34B Feb 23 14:20 sample.exs ➜ iex iex(1)> Math.sum(1,2) 3
values. Values can be of any type. Head and tail of a list are important and represent the first / remainder of a list. iex> [1,2,3] [1,2,3] iex> [1,2,3] ++ [4,5] [1,2,3,4,5] iex> [1,2,3,4,5] -- [4,5] [1,2,3] iex> hd [1,2,3] 1 iex> tl [1,2,3] [2,3]
tuples can hold any value. Tuples store elements contiguously in memory. Accessing a tuple element per index or getting the tuple size is a fast operation (indexes start from zero) iex> tuple = {'one', 'two'} {'one', 'two'} iex> elem(tuple, 0) 'one' iex> elem(tuple, 1) 'two'
lists, each element in a list holds its value and points to the following element until the end of the list is reached. This means accessing the length of a list is a linear operation Tuples are stored contiguously in memory. Getting the tuple size or accessing an element by index is fast. Updating or adding elements to tuples is expensive because it requires copying the whole tuple in memory.
b end def join(a, b, sep \\ “ “) do a + b end end Modules are a way to group functions They are defined using the defmodule macro Functions are defined with def Default arguments are defined with \\
sum_list(tail, head + accumulator) end def sum_list([] \\ [0], accumulator) do accumulator end end IO.puts Math.sum_list([1, 2, 3], 0) #=> 6 Due to immutability, loops in Elixir are written differently.
that provides tasks for creating, compiling, testing your application, managing its dependencies. Let’s build a sample… yaca (yet another chat app) ➜ mix new yaca --module Yaca * creating README.md * creating .gitignore * creating mix.exs * creating config * creating config/config.exs * creating lib * creating lib/yaca.ex * creating test * creating test/test_helper.exs * creating test/yaca_test.exs Your Mix project was created successfully. You can use "mix" to compile it, test it, and more: cd yaca mix test Run "mix help" for more commands.
YacaTest do use ExUnit.Case doctest Yaca test "the truth" do assert 1 + 1 == 2 end end ➜ yaca mix test Compiled lib/yaca.ex Generated yaca app Consolidated List.Chars Consolidated String.Chars Consolidated Collectable Consolidated IEx.Info Consolidated Enumerable Consolidated Inspect . Finished in 0.06 seconds (0.06s on load, 0.00s on tests) 1 test, 0 failures Randomized with seed 943520
compile) run by default :test - used by mix test :prod - the one you will use to run your project in production Default environment is :dev ➜ MIX_ENV=prod mix compile