SlideShare a Scribd company logo
Elixir
Abdulsattar - http://bimorphic.com
Features
Dynamic
Functional
Pattern Matching
Ruby inspired syntax
Extensible: macros
Concurrency, Distributed and Fault tolerant
Data Types/Structures
1 # integer
2.0 # float
true # boolean
:atom # atom / symbol
'elixir' # character list
"elixir" # string
[1, 2, 3] # list
{1,2} # tuple
[{:a, 1}, {:b, 2}] # keyword list
[a: 1, b: 2] # ^ same keyword list
%{a: 1, b: 2, c: 3} # hash
Pattern Matching
a = 1
{x, y} = {0, 0}
[head | tail] = [1, 2, 3]
result = case {1, 2, 3} do
{4, 5, 6} -> "Doesn't match"
{1, x, 3} when x > 3 -> "doesn't match because 2 < 3"
{x, 2, 3} -> "matches and x is #{x}"
_ -> "matches if nothing above matches"
end
def zero?(0), do: true
def zero?(x) when is_number(x), do: false
Modules
defmodule Math do
def sum(a, b) do
a + b
end
def sub(a, b) do
a - b
end
end
IO.puts (Math.sum(3,4))
Structs
defmodule Person do
defstruct name: "Sattar", age: 40
end
me = %Person{name: "AbdulSattar"}
IO.puts me.name # Abdulsattar
%Person{name: name, age: age} = me
IO.puts(age) # 40
youngMe = %{me | age: 10}
Protocols
defprotocol Blank do
@doc "Returns true if data is considered blank/empty"
def blank?(data)
end
defimpl Blank, for: Integer do
def blank?(0), do: true
def blank?(_), do: false
end
defimpl Blank, for: List do
def blank?([]), do: true
def blank?(_), do: false
end
Blank.blank?(3) # false
Blank.blank?([]) # true
Enumerables
iex(2)> Enum.map([1, 2, 3], fn x -> x * x end)
[1, 4, 9]
iex(3)> Enum.map([1, 2, 3], &(&1 * &1))
[1, 4, 9]
iex(4)> Enum.reduce(1..100, 0, &+/2)
5050
Pipe Operator
sumOfSquares = Enum.reduce(Enum.map(1..100, fn x -> x * x end), 0, &+/2)
sumOfSquares1 = 1..100
|> Enum.map(fn x -> x * x end)
|> Enum.reduce(0, &+/2)
square = fn x -> x * x end
sumOfSquares2 = 1..100
|> Enum.map(square)
|> Enum.sum
Macros
defmodule Unless do
defmacro macro_unless(clause, expression) do
quote do
if(!unquote(clause), do: unquote(expression))
end
end
end
Unless.macro_unless false, do: IO.puts "prints"
Unless.macro_unless true, do: IO.puts "doesn't print"
Processes
pid = spawn fn ->
receive do
msg -> IO.puts "Received: #{msg}"
end
end
send pid, "Hello!"
send pid, "3 times"
Supervisors
DEMO

More Related Content

PDF
Python Usage (5-minute-summary)
PDF
Simplifying code monster to elegant in n 5 steps
PDF
Extending Operators in Perl with Operator::Util
PPTX
Creating a Table from a Function
PPTX
Python data structures
PPTX
PPTX
Sql
PDF
Fog City Ruby - Triple Equals Black Magic
Python Usage (5-minute-summary)
Simplifying code monster to elegant in n 5 steps
Extending Operators in Perl with Operator::Util
Creating a Table from a Function
Python data structures
Sql
Fog City Ruby - Triple Equals Black Magic

What's hot (9)

PDF
My First Data Science Project (Data Science Thailand Meetup #1)
PPTX
The groovy puzzlers (as Presented at JavaOne 2014)
PPTX
Open course(programming languages) 20150121
PDF
Python an-intro youtube-livestream-day2
PDF
Writeable CTEs: The Next Big Thing
ZIP
全裸でワンライナー(仮)
KEY
Iterators, Hashes, and Arrays
PDF
Twittori - Twittwoch Berlin
PDF
Twittori - Tweets aus der Umgebung
My First Data Science Project (Data Science Thailand Meetup #1)
The groovy puzzlers (as Presented at JavaOne 2014)
Open course(programming languages) 20150121
Python an-intro youtube-livestream-day2
Writeable CTEs: The Next Big Thing
全裸でワンライナー(仮)
Iterators, Hashes, and Arrays
Twittori - Twittwoch Berlin
Twittori - Tweets aus der Umgebung
Ad

Viewers also liked (20)

PPT
終身保證收入
PPT
Commercial Architecture
PPT
Marilyn Gardner Milton: Advanced Law School Chat Pt. 2
PPT
班級經營100.03.30
PDF
IoT Eindhoven Iskander Smit - Civic City
PDF
World Office Forum Alianza del Pacífico 2015
PDF
7 reasons why media productivity plans don't work as expected
PDF
Мастердент
PDF
Getting Started as a PM
PDF
EEK! Halloween Activities for K to 5
PPTX
Improving Promotion Effectiveness - Mindtree webinar
PDF
Hashtaggery BLC16
DOCX
9 Frame Analysis - Biffy Clyro - Mountains
PDF
日本の名水
PDF
Stampions Cross Media Cafe
PDF
Finalaya daily wrap_01sep2014
PPTX
Atividade - Mapa Conceitual
PPTX
Atención
PPTX
Qualitem - Large List Support - SharePoint Saturday
DOC
Greatest Hits 80s
終身保證收入
Commercial Architecture
Marilyn Gardner Milton: Advanced Law School Chat Pt. 2
班級經營100.03.30
IoT Eindhoven Iskander Smit - Civic City
World Office Forum Alianza del Pacífico 2015
7 reasons why media productivity plans don't work as expected
Мастердент
Getting Started as a PM
EEK! Halloween Activities for K to 5
Improving Promotion Effectiveness - Mindtree webinar
Hashtaggery BLC16
9 Frame Analysis - Biffy Clyro - Mountains
日本の名水
Stampions Cross Media Cafe
Finalaya daily wrap_01sep2014
Atividade - Mapa Conceitual
Atención
Qualitem - Large List Support - SharePoint Saturday
Greatest Hits 80s
Ad

Similar to Introducing Elixir (20)

PDF
Elixir in a nutshell - Fundamental Concepts
PDF
Elixir formatter Internals
PDF
iRODS Rule Language Cheat Sheet
PDF
Elixir pattern matching and recursion
PDF
Elixir & Phoenix – fast, concurrent and explicit
PPT
SQL -PHP Tutorial
PDF
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
PDF
Pre-Bootcamp introduction to Elixir
PDF
R learning by examples
PDF
Slides chapter3part1 ruby-forjavaprogrammers
PDF
Elixir & Phoenix – fast, concurrent and explicit
PDF
Ruby Language - A quick tour
KEY
CoffeeScript - A Rubyist's Love Affair
PDF
Story for a Ruby on Rails Single Engineer
PDF
Begin with Python
PPTX
Django - sql alchemy - jquery
PPT
Python tutorial
PDF
Ecto DSL Introduction - Yurii Bodarev
PDF
Yurii Bodarev - Ecto DSL
PDF
An overview of Python 2.7
Elixir in a nutshell - Fundamental Concepts
Elixir formatter Internals
iRODS Rule Language Cheat Sheet
Elixir pattern matching and recursion
Elixir & Phoenix – fast, concurrent and explicit
SQL -PHP Tutorial
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
Pre-Bootcamp introduction to Elixir
R learning by examples
Slides chapter3part1 ruby-forjavaprogrammers
Elixir & Phoenix – fast, concurrent and explicit
Ruby Language - A quick tour
CoffeeScript - A Rubyist's Love Affair
Story for a Ruby on Rails Single Engineer
Begin with Python
Django - sql alchemy - jquery
Python tutorial
Ecto DSL Introduction - Yurii Bodarev
Yurii Bodarev - Ecto DSL
An overview of Python 2.7

Recently uploaded (20)

PPTX
Transform Your Business with a Software ERP System
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Digital Strategies for Manufacturing Companies
PPTX
Introduction to Artificial Intelligence
PDF
System and Network Administration Chapter 2
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
history of c programming in notes for students .pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Transform Your Business with a Software ERP System
Navsoft: AI-Powered Business Solutions & Custom Software Development
Operating system designcfffgfgggggggvggggggggg
Digital Strategies for Manufacturing Companies
Introduction to Artificial Intelligence
System and Network Administration Chapter 2
How to Migrate SBCGlobal Email to Yahoo Easily
Which alternative to Crystal Reports is best for small or large businesses.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Softaken Excel to vCard Converter Software.pdf
history of c programming in notes for students .pptx
CHAPTER 2 - PM Management and IT Context
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

Introducing Elixir

  • 2. Features Dynamic Functional Pattern Matching Ruby inspired syntax Extensible: macros Concurrency, Distributed and Fault tolerant
  • 3. Data Types/Structures 1 # integer 2.0 # float true # boolean :atom # atom / symbol 'elixir' # character list "elixir" # string [1, 2, 3] # list {1,2} # tuple [{:a, 1}, {:b, 2}] # keyword list [a: 1, b: 2] # ^ same keyword list %{a: 1, b: 2, c: 3} # hash
  • 4. Pattern Matching a = 1 {x, y} = {0, 0} [head | tail] = [1, 2, 3] result = case {1, 2, 3} do {4, 5, 6} -> "Doesn't match" {1, x, 3} when x > 3 -> "doesn't match because 2 < 3" {x, 2, 3} -> "matches and x is #{x}" _ -> "matches if nothing above matches" end def zero?(0), do: true def zero?(x) when is_number(x), do: false
  • 5. Modules defmodule Math do def sum(a, b) do a + b end def sub(a, b) do a - b end end IO.puts (Math.sum(3,4))
  • 6. Structs defmodule Person do defstruct name: "Sattar", age: 40 end me = %Person{name: "AbdulSattar"} IO.puts me.name # Abdulsattar %Person{name: name, age: age} = me IO.puts(age) # 40 youngMe = %{me | age: 10}
  • 7. Protocols defprotocol Blank do @doc "Returns true if data is considered blank/empty" def blank?(data) end defimpl Blank, for: Integer do def blank?(0), do: true def blank?(_), do: false end defimpl Blank, for: List do def blank?([]), do: true def blank?(_), do: false end Blank.blank?(3) # false Blank.blank?([]) # true
  • 8. Enumerables iex(2)> Enum.map([1, 2, 3], fn x -> x * x end) [1, 4, 9] iex(3)> Enum.map([1, 2, 3], &(&1 * &1)) [1, 4, 9] iex(4)> Enum.reduce(1..100, 0, &+/2) 5050
  • 9. Pipe Operator sumOfSquares = Enum.reduce(Enum.map(1..100, fn x -> x * x end), 0, &+/2) sumOfSquares1 = 1..100 |> Enum.map(fn x -> x * x end) |> Enum.reduce(0, &+/2) square = fn x -> x * x end sumOfSquares2 = 1..100 |> Enum.map(square) |> Enum.sum
  • 10. Macros defmodule Unless do defmacro macro_unless(clause, expression) do quote do if(!unquote(clause), do: unquote(expression)) end end end Unless.macro_unless false, do: IO.puts "prints" Unless.macro_unless true, do: IO.puts "doesn't print"
  • 11. Processes pid = spawn fn -> receive do msg -> IO.puts "Received: #{msg}" end end send pid, "Hello!" send pid, "3 times"