Elixir Tutorial
Elixir Tutorial
BASIC TYPES
Type iex in the command line to enter the shell for Erlang /OTP environment.
FOUR TYPES
Atoms
Strings
Tuples
Lists
Maps
.exs: are intended for scripting and they stay in memory when run or executed.
.ex: are compiled to byte code and written to disk in a beam file.
ATOMS
Example:
C:\Users\essie>iex
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :firstname
:firstname
:ok
iex(4)>
{:error, reason}
:false
Example:
Term
false
Data type
Atom
Reference modules
Atom
iex(4)> i true
Term
true
Data type
Atom
Reference modules
Atom
STRINGS
Format “sting_name”
Example:
iex(1)> euro = "#"
"#"
iex(2)> byte_size(euro)
iex(3)> String.length(euro)
"Hello, "
"Nana"
"Hello, Nana"
<>: concatenation
Example:
iex(7)> handle = "nana_yaw"
"nana_yaw"
TUPLES
An ordered collection used for storing a peace of data, is use for two, three or four values.
Example:
iex(1)> book={"Programming Book", "Dave", 25.00}
iex(2)> elem(book, 2)
25.0
iex(4)> book
LISTS
Example:
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
[1, 2, 3]
iex(3)> hd(first_list)
1
iex(4)> tl(first_list)
[2, 3]
[1, 2, 3]
[1, 2, 3]
[:ok, 1, 2, 3]
[1, 2, 3]
iex(9)> a
iex(10)> b
iex(11)> c
[1, 2, 3]
iex(13)> head
iex(14)> tail
[2, 3]
iex(16)> options[:notify_user]
true
IMMUTABILITY
In elixir, data is immutable, meaning that if the data is created, it cannot change.
Example:
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
MAPS
Similarities of lists and maps are collections of items that have a key and a value.
Differences
MAPS: the key does not have to an atom; it can be any type. It only allows one instance of each key.
Example
iex(1)> my_map = %{1 => {"Nana", "Yaw"}, 2 => {"Mark", "Annafi"}}
iex(2)> other_map = %{:names => ["Nana Yaw", "Mark Annafi"], "gender"=> "male"}
iex(4)> other_map["gender"]
"male"
iex(5)> other_map.names
iex(7)> names_lists
MODULES
Example:
def say_hello do
end
end
Compile in terminal
C:\Users\essie\Desktop>iex "module_playgroup.exs"
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> ModulePlaygroup.say_hello
Hello World
:ok
iex(2)> r(ModulePlaygroup)
module_playgroup.exs:1
iex(3)> ModulePlaygroup.say_hello
:ok
MODULES DIRECTIVES
1. IMPORT: Include module functions. Includes/Excludes specific functions from specific modules
Example:
end
def inspect(param1) do
puts param1
end
end
Compile in terminal
iex(2)> r(ModulePlaygroup)
module_playgroup.exs:1
iex(3)> ModulePlaygroup.say_hello
Starting Output
Ending output
:ok
Example:
def say_hello do
end
def inspect(param1) do
puts "Starting Output"
puts param1
end
def print_sum do
MyMath.add(1,2)
end
end
defmodule ModulePlaygroup.Misc.Util.Math do
def add(a,b) do
a+b
end
end
Compile in Terminal
iex "module_playgroup.exs"
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> import_file("misc_util_maths.exs")
{:module, ModulePlaygroup.Misc.Util.Math,
<<70, 79, 82, 49, 0, 0, 3, 200, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 109,
0, 0, 0, 9, 37, 69, 108, 105, 120, 105, 114, 46, 77, 111, 100, 117, 108, 101,
80, 108, 97, 121, 103, 114, 111, 117, 112, ...>>, {:add, 2}}
iex(2)> ModulePlaygroup.print_sum
iex(3)> r(ModulePlaygroup)
module_playgroup.exs:1
iex(4)> ModulePlaygroup.print_sum
3
3. REQUIRE: Allows using macros in your module that will depend upon.
Example:
require Integer
def say_hello do
end
def inspect(param1) do
puts param1
end
def print_sum do
MyMath.add(1,2)
end
def is_even(num) do
end
end
defmodule ModulePlaygroup.Misc.Util.Math do
def add(a,b) do
a+b
end
end
Compile in Terminal
iex(5)> r(ModulePlaygroup)
module_playgroup.exs:1
iex(6)> ModulePlaygroup.is_even(1)
Is 1 even? false
:ok
iex(7)> ModulePlaygroup.is_even(2)
Is 2 even? true
:ok
iex(8)> ModulePlaygroup.is_even(4)
Is 4 even? true
:ok
iex(9)>