I'm not even going to pretend to be objective, but I will try to avoid hyperbole
Elixir is a dynamic, functional language designed 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.
Haven't we invented all the languages?
Elixir is a dynamic, functional language designed 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.
dialyzer
iex> x = 1
1
iex> 1 = x
1
iex> [a, b, c] = [1, 2, 3]
[1, 2, 3]
iex>; a
1
iex> [head|tail] = [1,2,3]
[1, 2, 3]
iex> head
1
iex> tail
[2, 3]
iex> 1 # integer
iex> 0x1F # integer
iex> 1.0 # float
iex> true # boolean
iex> :atom # atom / symbol
iex> "elixir" # string
iex> [1, 2, 3] # list
iex> {1, 2, 3} # tuple
iex> %{key: "value"} # map
iex> x = 1
1
iex> 1 = x
1
iex> [a, b, c] = [1, 2, 3]
[1, 2, 3]
iex> a
1
iex> [head|tail] = [1,2,3]
[1, 2, 3]
iex> head
1
iex> tail
[2, 3]
iex> scores = %{ "users" => [{:alice, 500},
{:bob, 250},
{:carol, 100}] }
...
iex> %{"users" => [{first, _score}|_]} = scores
...
iex> first
:alice
iex> add = fn (a, b) -> a + b end
#Function<12.52032458/2 in :erl_eval.expr/5>
iex> add.(3, 5)
8
iex> add = &(&1+&2)
#&:erlang.+/2
iex> add.(3, 5)
8
iex> abs = fn (a) when a < 0 -> -a
(a) -> a
end
iex> abs.(-3)
3
iex> abs.(3)
3
iex> cond do
...> 2 + 2 == 5 ->
...> "This will not be true"
...> 2 * 2 == 3 ->
...> "Nor this"
...> 1 + 1 == 2 ->
...> "But this will"
...> end
"But this will"
iex> if nil do
...> "This won't be seen"
...> else
...> "This will"
...> end
"This will"
case File.write("/path/to/file", content) do
:ok -> :ok
{:error, :enospc} ->
IO.puts("Out of space.")
{:error, :enospc}
{:error, reason} ->
IO.puts("""
Didn't write to file because: #{inspect reason}.
""")
{:error, reason}
end
Hard Mode
iex> defmodule Test do
def test(0) do
IO.puts "Blastoff"
end
def test(n) do
IO.puts "#{n}"
end
end
...
iex> Test.test(3)
3
2
1
Blastoff!
:ok
iex> defmodule Factorial do
def fac(1) do
1
end
def fac(n) when n > 1 do
n * fac(n - 1)
end
end
...
iex> Factorial.fac(5)
120
iex> Enum.each([1,2,3], fn i -> IO.puts "Test #{i}" end)
Test 1
Test 2
Test 3
:ok
iex> Enum.filter([1,2,3], &Integer.is_odd?/2)
[1, 3]
iex> Enum.map([1,2,3], fn i ->
IO.puts "Test #{i*2}"
i * 2
end)
Test 2
Test 4
Test 6
[2, 4, 6]
Boring thing you're always doing
iex> "Hello, world!" |> IO.puts
Hello, world!
:ok
iex> [1,2,3]
|> Enum.map(&(&1*3))
|> Enum.filter(&Integer.is_odd?/1)
# Expands to:
# Enum.filter(
# Enum.map([1,2,3], &(&1*3)),
# &Integer.is_odd?/1
# )
[3, 9]
Amazing unix-like thing
[1,2,3,4,5]
|> Enum.filter(&Integer.is_odd?(&1))
|> case do
[1,3,5] -> :correct
_ -> :incorrect
end
|> IO.inspect
# Will print: :correct
Amazing unix-like thing
iex
is a full Elixir shell, you can remote into nodes and restart the shell without restarting the VMmix
is a task runner and hosts the compiler and toolsethex
is a package repository for everything http://hex.pm/elixir
will run a plain elixir filedialyzer
will type-check your codeobserver
will let you look at a running system livecredo
will give you opinions on your code and let you come up with style documents to enforce code styleex_unit
unit testing framework, extremely parallel