SlideShare a Scribd company logo
Ruben Amortegui
@ramortegui
http://rubenamortegui.com
https://github.com/ramortegui
Elixir - Basics
Agenda
● About Elixir
● Tools ( iex and mix)
● Concepts (Pattern Matching, Immutability)
● Basics ( types, operators, functions, lists and
recursion, maps, collections, strings, control
flow)
About Elixir
● Functional, concurrent, general purpose
programming language.
● Build on top of Erlang
● Runs over Erlangs virtual Machine (BEAM)
About Elixir - Features
● Scalabiliy
– Creates light weight processes (code runs inside processes)
● Fault tolerance
– Supervisors (detects when a process dies and creates a new
one)
● Build on top of Erlang
– Supports erlang calls
● Runs over Erlang’s virtual Machine (BEAM)
– Can use erlang libraries: e.g. :math
Basic Tools
● iex (Interactive elixir shell)
– Commands: pwd() ls(), c, h, i
● mix (helper to build, run, compile, manage
dependencies).
– mix new project: To create a the default structure
– iex -s mix: To compile and load your project
– mix get.deps: To get dependencies
– mix hex: .... To publish your code
– mix test: To run the test suite
Elixir – Pattern Matching
● It’s a core part of elixir. The operator is =
iex> a = 3
3
iex> 3 = a
3
iex> 4 = a
** (MatchError) no match of right hand side value: 3
– Tuples and lists
{a,b,c} = {"1", "5", "6"}
[head|tail] = [1,2,3,4]
– Pin operator ^
● Match the value of the variable.
● It’s used in functions too.
Elixir – Immutability
● Immutable data is known data
● Sample
a = 10
something(a)
print a
● Benefits:
– You don’t need to worry that any code might mutate
your data.
Elixir – Basics 1
● Value Types
– Integer, Float, Atoms, Ranges, Regular Expresions
● System Types
– PIDs
● Collection Types
– Tuples {:name, "Ruben", :age, 36}
– Lists [1,2,3,4]
– Maps %{ "data" => 123 }
– Binaries <<91,93>>
Elixir – Basics 2
● Operators
– Comparison
● ===, !==, ==, !=, >, >=, <, <=
– Booelan
● or , and, not (Receive only booleans) true || false
● ||, &&, ! ( Any value != false or nill is true)
– Arithmetic
● +, -, *, /, div, rem
– Join
● binary <> binary
● list1 ++ list2, list1 -- list2
– In
● a in enum (check if a exists on enum array)
Elixir – Basics 3
● Pipe operator |>
– It’s used to pass the result of an expression as the first parameter of another
expression
e.g:
Count the number of words in a String
iex> Enum.count(String.split("This is a String"))
iex> "This is a String" |> String.split |> Enum.count
– On a file
"This is a String"
|> String.split
|>Enum.count
Elixir – Anonymous Functions
● What is anonymous funciton
Are basic types and are created using the fn keyword:
● fn
parameter-list -> body
parameter-list -> body
end
● How to define
sum = fn(x,y) -> x + y end
● How to use
sum.(1,3) # => 4
Elixir – Anonymous Functions
File: exist.txt
----------
Hello world
handle_open = fn
{:ok, file} -> "Read data: #{IO.read(file, :line)}"
{:error, error} -> "Error: #{:file.format_error(error)}"
end
handle_open.(File.open("exists.ex"))
"Read data: "hello world"n"
iex> handle_open.(File.open("not_exists.ex"))
"Error: no such file or directory"
Elixir – Modules and Named
Functions
● Organize code
defmodule Name do
def print_name(name) do
IO.puts "Hello #{name}"
end
end
● Iex(1)> Name.print_name("Ruben")
hello Ruben
:ok
Elixir – Modules and Named
Functions
●
guards
defmodule Name do
def greeting(name,age) when age < 2 do
IO.puts "Gaga #{name}"
end
def greeting(name,age) when age <5 do
IO.puts "Hi #{name}"
end
def greeting(name,age) do
IO.puts "Hello #{name}"
end
end
●
Iex(1)> Name.print_name("Ruben",1)
Gaga Ruben
:ok
Elixir – List and Recursion
● How to define a List
[], [1,2,3]
● Patter matching
[a,b,c] = [1,2,3]
● Sample
– Sum numbers of a list
● Tail Recursion
– Sum numbers of a list
Elixir – Maps, Keyword Lists, and
Structs
● What is:
– Keyword list (list of tuples – key needs to be an atom)
[name: "Ruben", age: 36]
kw = [{:name, "Ruben"},{ :age, 36}]
● Kw[:name]
– Map
● map = %{ "name" => "Ruben" }
● map["name"]
– Structs (Define map structure)
defmodule User do
defstruct [ name: "unknown"]
end
● %User{ username => "test" }
● %User{}
Elixir – Processing Collections
● Enum
– Module to process collections
– Iex>h Enum
● Comprehensions (a way to iterate over an
Enumerable)
– for var <- Enum, do: code
– for x <- [1,2,3], do: IO.puts(x)
Elixir – Strings
● "String"
– A String in Elixir is a UTF-8 encoded binary.
– Use String module to manipulate strings.
iex> String.capitalize("ruben")
– It’s represented as binary list
iex> i "Ruben"
– Contatenation with <>
iex> i ( "Ruben" <> " Dario" )
Elixir – Control Flow
●
If (cond) do body end
● If (cond) do body1 else body2 end
●
Case
x = 10
case x do
0 -> "This clause won't match"
_ -> "This clause would match any value (x = #{x})"
end
#=> "This clause would match any value (x = 10)"
●
Cond
cond do
1 + 1 == 1 -> "This will never match"
2 * 2 != 4 -> "Nor this"
true -> "This will"
end
#=> "This will"
References
●
NY: Manning Publications.
● Thomas, D. (2016). Programming Elixir 1.3:
functional, concurrent, pragmatic, fun. Releigh,
NC: Pragmatic Bookshelf.
Thanks!
Q & A?
@ramortegui
http://rubenamortegui.com
https://github.com/ramortegui

More Related Content

ODP
Elixir basics-2
ODP
From Perl To Elixir
PDF
Functional programming in scala
PPTX
Lua Study Share
PPTX
An Introduction to Functional Programming with Javascript
PDF
Clojure
ODP
10 Things I Hate About Scala
PPTX
Functional programming with Java 8
Elixir basics-2
From Perl To Elixir
Functional programming in scala
Lua Study Share
An Introduction to Functional Programming with Javascript
Clojure
10 Things I Hate About Scala
Functional programming with Java 8

What's hot (20)

PPTX
PHP: GraphQL consistency through code generation
PDF
05. haskell streaming io
PPTX
The joy of functional programming
PPTX
Dev Concepts: Functional Programming
ODP
Aura for PHP at Fossmeet 2014
PDF
使用.NET构建轻量级分布式框架
PDF
Intro to Java 8 Closures (Dainius Mezanskas)
PPTX
Functional programming and ruby in functional style
PPTX
Functional java 8
PDF
Functional Programming with JavaScript
PDF
JavaScript - Chapter 6 - Basic Functions
ODP
Knolx Session : Built-In Control Structures in Scala
PDF
(3) cpp procedural programming
ODP
Knolx Session: Introducing Extractors in Scala
PDF
Functional programming with Java 8
PPTX
Intro to Ruby on Rails
PDF
PDF
JavaScript operators
PDF
Java 8 Lambda Expressions
PDF
Clean Code JavaScript
PHP: GraphQL consistency through code generation
05. haskell streaming io
The joy of functional programming
Dev Concepts: Functional Programming
Aura for PHP at Fossmeet 2014
使用.NET构建轻量级分布式框架
Intro to Java 8 Closures (Dainius Mezanskas)
Functional programming and ruby in functional style
Functional java 8
Functional Programming with JavaScript
JavaScript - Chapter 6 - Basic Functions
Knolx Session : Built-In Control Structures in Scala
(3) cpp procedural programming
Knolx Session: Introducing Extractors in Scala
Functional programming with Java 8
Intro to Ruby on Rails
JavaScript operators
Java 8 Lambda Expressions
Clean Code JavaScript
Ad

Viewers also liked (20)

PDF
PDF
Bottleneck in Elixir Application - Alexey Osipenko
PDF
Learn Elixir at Manchester Lambda Lounge
PDF
Clojure Reducers / clj-syd Aug 2012
PDF
Monads in Clojure
PDF
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
PPTX
QCon - 一次 Clojure Web 编程实战
PDF
Java 与 CPU 高速缓存
PPTX
The mystique of erlang
PDF
Phoenix demysitify, with fun
PDF
Spark as a distributed Scala
PPTX
ELIXIR Webinar: Introducing TeSS
PDF
Big Data eBook
PDF
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
PDF
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
PPTX
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
PPTX
Control flow in_elixir
PDF
Spring IO for startups
PDF
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
PPTX
Phoenix: Inflame the Web - Alex Troush
Bottleneck in Elixir Application - Alexey Osipenko
Learn Elixir at Manchester Lambda Lounge
Clojure Reducers / clj-syd Aug 2012
Monads in Clojure
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
QCon - 一次 Clojure Web 编程实战
Java 与 CPU 高速缓存
The mystique of erlang
Phoenix demysitify, with fun
Spark as a distributed Scala
ELIXIR Webinar: Introducing TeSS
Big Data eBook
WEB MINING: PATTERN DISCOVERY ON THE WORLD WIDE WEB - 2011
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
나프다 웨비너 1604: Elixir와 함수형 프로그래밍을 이용한 웹 개발
Control flow in_elixir
Spring IO for startups
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Phoenix: Inflame the Web - Alex Troush
Ad

Similar to Elixir basics (20)

PDF
Elixir talk
PDF
Elixir cheatsheet
PPTX
PDF
What is the deal with Elixir?
PDF
Elixir in a nutshell - Fundamental Concepts
PDF
Elixir
PDF
Introduction to Elixir
PPTX
PDF
Elixir for aspiring Erlang developers
PPTX
Introduction to functional programming, with Elixir
PDF
Elixir for Rubyists
PDF
Programming Elixir 13 Functional Concurrent Pragmatic Fun Dave Thomas
PDF
Elixir and OTP Apps introduction
PDF
Functional Programming With Elixir
PDF
Introduction to Elixir
PDF
Introducing Elixir Getting Started In Functional Programming 2nd Edition Simo...
PDF
Elixir and Phoenix for Rubyists
PDF
Elixir tutorial
PPTX
Introducing Elixir
PDF
Learning Elixir as a Rubyist
Elixir talk
Elixir cheatsheet
What is the deal with Elixir?
Elixir in a nutshell - Fundamental Concepts
Elixir
Introduction to Elixir
Elixir for aspiring Erlang developers
Introduction to functional programming, with Elixir
Elixir for Rubyists
Programming Elixir 13 Functional Concurrent Pragmatic Fun Dave Thomas
Elixir and OTP Apps introduction
Functional Programming With Elixir
Introduction to Elixir
Introducing Elixir Getting Started In Functional Programming 2nd Edition Simo...
Elixir and Phoenix for Rubyists
Elixir tutorial
Introducing Elixir
Learning Elixir as a Rubyist

More from Ruben Amortegui (7)

PDF
Working with-phoenix
ODP
Elixir koans
ODP
Concurrent programming
ODP
Elixir otp-basics
ODP
Elixir absinthe-basics
ODP
Phoenix basics
Working with-phoenix
Elixir koans
Concurrent programming
Elixir otp-basics
Elixir absinthe-basics
Phoenix basics

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Introduction to Artificial Intelligence
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Transform Your Business with a Software ERP System
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
System and Network Administration Chapter 2
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
AI in Product Development-omnex systems
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
history of c programming in notes for students .pptx
medical staffing services at VALiNTRY
Odoo Companies in India – Driving Business Transformation.pdf
Introduction to Artificial Intelligence
Odoo POS Development Services by CandidRoot Solutions
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Wondershare Filmora 15 Crack With Activation Key [2025
CHAPTER 2 - PM Management and IT Context
Transform Your Business with a Software ERP System
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administration Chapter 2
Adobe Illustrator 28.6 Crack My Vision of Vector Design
AI in Product Development-omnex systems
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Softaken Excel to vCard Converter Software.pdf
Nekopoi APK 2025 free lastest update
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
history of c programming in notes for students .pptx

Elixir basics

  • 2. Agenda ● About Elixir ● Tools ( iex and mix) ● Concepts (Pattern Matching, Immutability) ● Basics ( types, operators, functions, lists and recursion, maps, collections, strings, control flow)
  • 3. About Elixir ● Functional, concurrent, general purpose programming language. ● Build on top of Erlang ● Runs over Erlangs virtual Machine (BEAM)
  • 4. About Elixir - Features ● Scalabiliy – Creates light weight processes (code runs inside processes) ● Fault tolerance – Supervisors (detects when a process dies and creates a new one) ● Build on top of Erlang – Supports erlang calls ● Runs over Erlang’s virtual Machine (BEAM) – Can use erlang libraries: e.g. :math
  • 5. Basic Tools ● iex (Interactive elixir shell) – Commands: pwd() ls(), c, h, i ● mix (helper to build, run, compile, manage dependencies). – mix new project: To create a the default structure – iex -s mix: To compile and load your project – mix get.deps: To get dependencies – mix hex: .... To publish your code – mix test: To run the test suite
  • 6. Elixir – Pattern Matching ● It’s a core part of elixir. The operator is = iex> a = 3 3 iex> 3 = a 3 iex> 4 = a ** (MatchError) no match of right hand side value: 3 – Tuples and lists {a,b,c} = {"1", "5", "6"} [head|tail] = [1,2,3,4] – Pin operator ^ ● Match the value of the variable. ● It’s used in functions too.
  • 7. Elixir – Immutability ● Immutable data is known data ● Sample a = 10 something(a) print a ● Benefits: – You don’t need to worry that any code might mutate your data.
  • 8. Elixir – Basics 1 ● Value Types – Integer, Float, Atoms, Ranges, Regular Expresions ● System Types – PIDs ● Collection Types – Tuples {:name, "Ruben", :age, 36} – Lists [1,2,3,4] – Maps %{ "data" => 123 } – Binaries <<91,93>>
  • 9. Elixir – Basics 2 ● Operators – Comparison ● ===, !==, ==, !=, >, >=, <, <= – Booelan ● or , and, not (Receive only booleans) true || false ● ||, &&, ! ( Any value != false or nill is true) – Arithmetic ● +, -, *, /, div, rem – Join ● binary <> binary ● list1 ++ list2, list1 -- list2 – In ● a in enum (check if a exists on enum array)
  • 10. Elixir – Basics 3 ● Pipe operator |> – It’s used to pass the result of an expression as the first parameter of another expression e.g: Count the number of words in a String iex> Enum.count(String.split("This is a String")) iex> "This is a String" |> String.split |> Enum.count – On a file "This is a String" |> String.split |>Enum.count
  • 11. Elixir – Anonymous Functions ● What is anonymous funciton Are basic types and are created using the fn keyword: ● fn parameter-list -> body parameter-list -> body end ● How to define sum = fn(x,y) -> x + y end ● How to use sum.(1,3) # => 4
  • 12. Elixir – Anonymous Functions File: exist.txt ---------- Hello world handle_open = fn {:ok, file} -> "Read data: #{IO.read(file, :line)}" {:error, error} -> "Error: #{:file.format_error(error)}" end handle_open.(File.open("exists.ex")) "Read data: "hello world"n" iex> handle_open.(File.open("not_exists.ex")) "Error: no such file or directory"
  • 13. Elixir – Modules and Named Functions ● Organize code defmodule Name do def print_name(name) do IO.puts "Hello #{name}" end end ● Iex(1)> Name.print_name("Ruben") hello Ruben :ok
  • 14. Elixir – Modules and Named Functions ● guards defmodule Name do def greeting(name,age) when age < 2 do IO.puts "Gaga #{name}" end def greeting(name,age) when age <5 do IO.puts "Hi #{name}" end def greeting(name,age) do IO.puts "Hello #{name}" end end ● Iex(1)> Name.print_name("Ruben",1) Gaga Ruben :ok
  • 15. Elixir – List and Recursion ● How to define a List [], [1,2,3] ● Patter matching [a,b,c] = [1,2,3] ● Sample – Sum numbers of a list ● Tail Recursion – Sum numbers of a list
  • 16. Elixir – Maps, Keyword Lists, and Structs ● What is: – Keyword list (list of tuples – key needs to be an atom) [name: "Ruben", age: 36] kw = [{:name, "Ruben"},{ :age, 36}] ● Kw[:name] – Map ● map = %{ "name" => "Ruben" } ● map["name"] – Structs (Define map structure) defmodule User do defstruct [ name: "unknown"] end ● %User{ username => "test" } ● %User{}
  • 17. Elixir – Processing Collections ● Enum – Module to process collections – Iex>h Enum ● Comprehensions (a way to iterate over an Enumerable) – for var <- Enum, do: code – for x <- [1,2,3], do: IO.puts(x)
  • 18. Elixir – Strings ● "String" – A String in Elixir is a UTF-8 encoded binary. – Use String module to manipulate strings. iex> String.capitalize("ruben") – It’s represented as binary list iex> i "Ruben" – Contatenation with <> iex> i ( "Ruben" <> " Dario" )
  • 19. Elixir – Control Flow ● If (cond) do body end ● If (cond) do body1 else body2 end ● Case x = 10 case x do 0 -> "This clause won't match" _ -> "This clause would match any value (x = #{x})" end #=> "This clause would match any value (x = 10)" ● Cond cond do 1 + 1 == 1 -> "This will never match" 2 * 2 != 4 -> "Nor this" true -> "This will" end #=> "This will"
  • 20. References ● NY: Manning Publications. ● Thomas, D. (2016). Programming Elixir 1.3: functional, concurrent, pragmatic, fun. Releigh, NC: Pragmatic Bookshelf.