This document provides an introduction to the Elixir programming language. It discusses what Elixir is, how to get started with installation and configuration of Elixir and Erlang, basic and compound data types in Elixir, functions and modules, and higher-order functions and comprehensions. Key topics covered include installing Elixir using ASDF, basic data types like integers, floats, atoms, and more, functions and anonymous functions, modules, and Enum functions like map, reduce, and comprehensions.
2. # 1. Introduction - what is Elixir, how to start?
# 2. Basic and compound data types
# 3. Functions and modules
# 4. Let’s talk about `=`
# 5. Let’s have some fun!
# 6. Let’s talk about `mix`
# 7. Let’s talk about dependencies
# 8. Suggestions where to find more information
# 9. QA
22. # Install erlang
$ asdf plugin-add erlang
$ asdf list-all erlang # will list all the available versions
$ asdf install erlang 20.3.7
$ asdf global erlang 20.3.7
23. # Install erlang
$ asdf plugin-add erlang
$ asdf list-all erlang # will list all the available versions
$ asdf install erlang 20.3.7
$ asdf global erlang 20.3.7
# Install Elixir
$ asdf plugin-add elixir
$ asdf list-all elixir
$ asdf install elixir 1.6.5
$ asdf global elixir 1.6.5
24. # Install erlang
$ asdf plugin-add erlang
$ asdf list-all erlang # will list all the available versions
$ asdf install erlang 20.3.7
$ asdf global erlang 20.3.7
# Install Elixir
$ asdf plugin-add elixir
$ asdf list-all elixir
$ asdf install elixir 1.6.5
$ asdf global elixir 1.6.5
# [Optional] Install Node
45. # Functions
inc = fn x -> x + 1 end
inc.(3) # => 4
# Capture operator
dec = &(&1 - 1)
dec.(3) # => 2
46. # Functions
inc = fn x -> x + 1 end
inc.(3) # => 4
# Capture operator
dec = &(&1 - 1)
dec.(3) # => 2
# Modules
defmodule MyModule do
end
47. # Functions
inc = fn x -> x + 1 end
inc.(3) # => 4
# Capture operator
dec = &(&1 - 1)
dec.(3) # => 2
# Modules
defmodule MyModule do
def square(x) do
x * 2
end
end
MyModule.square(4) # => 8
48. # Functions
inc = fn x -> x + 1 end
inc.(3) # => 4
# Capture operator
dec = &(&1 - 1)
dec.(3) # => 2
# Modules
defmodule MyModule do
def square(x) do
x * 2
end
def multi(x, y), do: x * y
end
MyModule.square(4) # => 8
MyModule.multi(3, 4) # => 12
49. # Comprehensions
for x <- (1..5),
y <- [:blue, :red],
rem(x, 2) == 0,
do: "#{x * x}-#{y}" # => ["4-blue", "4-red", "16-blue", "16-red"]
50. # Comprehensions
for x <- (1..5),
y <- [:blue, :red],
rem(x, 2) == 0,
do: "#{x * x}-#{y}" # => ["4-blue", "4-red", "16-blue", "16-red"]
# Higher-order functions
61. x = 1
1 = x
2 = x
** (MatchError) no match of right hand side value: 1
^x = 1 # ^ - pin operator
^x = 2
list = [20, 21, 22]
[a, b, c] = list
a # => 20
b # => 21
[a, ^x, c] = list
** (MatchError) no match of right hand side value: [20, 21, 22]
hd(list) # => 20
tl(list) # => [21, 22]
[head | tail] = list
head # => 20
tail # => [21, 22]
[a2, 21 | tail] = list
a2 # => 20
tail # => [22]
65. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
66. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
defmodule Employee do
end
67. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
defmodule Employee do
defstruct [:name, :lang]
end
68. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
defmodule Employee do
defstruct [:name, :lang]
end
p = %Employee{name: "Pawel", lang: "Elixir"}
69. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
defmodule Employee do
defstruct [:name, :lang]
end
p = %Employee{name: "Pawel", lang: "Elixir"}
%{name: name} = p
name # => "Pawel"
70. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
defmodule Employee do
defstruct [:name, :lang]
end
p = %Employee{name: "Pawel", lang: "Elixir"}
%{name: name} = p
name # => "Pawel"
%Employee{name: name} = p
name # => "Pawel"
71. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
defmodule Employee do
defstruct [:name, :lang]
end
p = %Employee{name: "Pawel", lang: "Elixir"}
%{name: name} = p
name # => "Pawel"
%Employee{name: name} = p
name # => "Pawel"
lang = "Elixir"
72. map = %{a: "Foo", b: "Bar"}
%{a: a} = map
a # => "Foo"
%{a: "Baz", b: b} = map
** (MatchError) no match of right hand side value: %{a: "Foo", b: "Bar"}
defmodule Employee do
defstruct [:name, :lang]
end
p = %Employee{name: "Pawel", lang: "Elixir"}
%{name: name} = p
name # => "Pawel"
%Employee{name: name} = p
name # => "Pawel"
lang = "Elixir"
%Employee{name: name, lang: ^lang} = p
name # => "Pawel"
75. File.read("./nice_file") # => {:ok, "Hello World! Nice to see you all!n"}
File.read("./non_existing_one") # => {:error, :enoent}
76. File.read("./nice_file") # => {:ok, "Hello World! Nice to see you all!n"}
File.read("./non_existing_one") # => {:error, :enoent}
File.read!("./non_existing_one")
** (File.Error) could not read file "./non_existing_one": no such file or directory
77. File.read("./nice_file") # => {:ok, "Hello World! Nice to see you all!n"}
File.read("./non_existing_one") # => {:error, :enoent}
File.read!("./non_existing_one")
** (File.Error) could not read file "./non_existing_one": no such file or directory
HTTPoison.get("https://google.com")
78. File.read("./nice_file") # => {:ok, "Hello World! Nice to see you all!n"}
File.read("./non_existing_one") # => {:error, :enoent}
File.read!("./non_existing_one")
** (File.Error) could not read file "./non_existing_one": no such file or directory
HTTPoison.get("https://google.com")
{:ok,
%HTTPoison.Response{
body: "...",
headers: [
{"Location", "https://www.google.com/"},
{"Content-Type", "text/html; charset=UTF-8"},
{"Cache-Control", "public, max-age=2592000"},
{"Server", "gws"},
{"Content-Length", "220"},
{"X-Frame-Options", "SAMEORIGIN"},
],
request_url: "https://google.com",
status_code: 301
}}