SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Lab Zero Innovations Inc.
77 Battery Street
San Francisco, CA 94111
415.839.6861
info@labzero.com
labzero.com
Introduction to Elixir
Brien Wankel
Sasha Voynow
What is Elixir?
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.
● First release: 2012
● 1.0 in September 2014
● Takes advantage of the Erlang VM
○ Compiled to Erlang (BEAM) bytecode
○ Runs on the Erlang (BEAM) VM
Wait, did you say Erlang?
● Created in 1986 by Ericsson for telco exchange software
● Open Source since 1988
● A VM (BEAM)
● A set of libraries and tools (OTP)
● Engineered for
○ Fault tolerance (uptime!)
○ Responsiveness (low latency!)
○ Distributed computing (clustering! concurrency!)
Open Telecom Platform (OTP)
Search YouTube for: “Erlang: The Movie”
OTP
● a set of tools and libraries for implementing fault tolerant server
applications
● Some OTP jargon
○ Process: An extremely lightweight unit of execution
○ Application: A tree/graph of processes that are managed
together.
○ Project: What is normally thought of as an application. A
number of applications started together in the same VM
OSS in Erlang
Erlang in Production
If Erlang is so great, why Elixir?
Erlang has some UX issues
○ Funky, unfamiliar syntax
○ Lots of boilerplate
% module_name.erl
-module(module_name). % you may use some other name
-compile(export_all).
hello() ->
io:format("~s~n", ["Hello world!"]).
# module_name.ex
defmodule ModuleName do
def hello do
IO.puts "Hello World"
end
end
Elixir in Production
Types
The Usual Suspects
● Value Types
○ Int
○ Float
○ Atom :yo
○ String / Binary
● Container Types
○ Tuple {1, :foo}
○ List [1,2,3]
○ Map %{foo: 10, bar: "baz" “foo” => “figgle”}
○ Struct
■ a named or tagged Map
■ %User{name: "Brien Wankel", company: "Lab Zero"}
● and some misc others
Pattern
M
atching
The Match Operator (=)
iex(1)> 3 = 3
3
iex(2)> x = 3
3
iex(3)> 3 = x
3
iex(4)> 2 = x
** (MatchError) no match of right hand side value: 3
The Pin Operator (^)
iex(1)> x = 3
3
iex(2)> y = x
3
iex(3)> x = 10
10
iex(4)> y
3
iex(5)> ^x = 3
** (MatchError) no match of right hand side value: 3
iex(5)> ^x = 10
10
Matching Lists
iex(1)> [a,b,c] = [1,2,3]
[1, 2, 3]
iex(2)> a
1
iex(3)> b
2
iex(4)> c
3
iex(1)> [h | t] = [1,2,3]
[1, 2, 3]
iex(2)> h
1
iex(3)> t
[2, 3]
Matching Maps
iex(1)> user = %{name: "Brien", status: :expired, state: "AZ"}
%{name: "Brien", state: "AZ", status: :expired}
iex(2)> %{name: theName} = user
%{name: "Brien", state: "AZ", status: :expired}
iex(3)> theName
"Brien"
iex(4)> %{name: theName, state: "CA"} = user
** (MatchError) no match of right hand side value: %{name:
"Brien", state: "AZ", status: :expired}
Matching in case statements
def registered_users do
case load_users_from_database() do
{:ok, users} -> users
{:err, error}
-> IO.puts("something went wrong: #{error}")
[]
end
end
def charge_user(user) do
case user do
%User{account_type: :premium} -> charge_card(user)
_ -> nil
end
end
Functions
# different functions
# &Lunchdown.string_to_int/1
def string_to_int(x) do
# ...
end
# &Lunchdown.string_to_int/2
def string_to_int(x, radix) do
# ...
end
# multiple heads of the same function
# &Lunchdown.find/1
def find([_h | _t] = ids) do
# ...
end
def find(id) do
# ...
end
Matching in Function Definitions
def handle_cast(:increment, state) do
{:noreply, state + 1}
end
def handle_cast(:decrement, state) do
{:noreply, state - 1}
end
def charge_credit_card(user = %User{account_type: :premium}) do
# charge the credit card
end
def charge_credit_card(_) do
# no-op
end
Matching in Function Definitions
# recursion
def sum([]), do: 0
def sum*([h | t]) do
h + sum(t)
end
Guard clauses
* You can only use a subset of built-in functions and operators
def charge_credit_card(cc_number, amount) when amount < 0, do: end
def charge_credit_card(cc_number, amount) do
# do some stuff
end
Pipelines and the |> operator
● A common elixir idiom for composing functions
● Building up complex transformations of data out of simpler ones in a way
that is readable and intention revealing
● APIs are commonly designed to work well with pipelines
Pipelines
def user_by_uid(uid) do
Authorization
|> where([a], a.uid == ^uid)
|> join(:inner, [a], u in User, u.id == a.user_id)
|> preload(:user)
|> Repo.one
end
def word_count(filename) do
increment = fn x -> x + 1 end
count_fun = fn w, m -> Map.update(m, w, 1, increment) end
filename
|> File.stream!
|> Stream.map(&String.trim/1)
|> Stream.flat_map(&String.split/1)
|> Stream.map(&String.downcase/1)
|> Enum.reduce(%{}, count_fun)
end
Modules
● The unit of code organization in Elixir
● Just a group of related functions.
defmodule Stack do
defstruct [:items]
# this defines a struct %Stack with the field items
def init do
%Stack{items: []}
end
def push(%Stack(items: items}, item), do: %Stack{items: items ++ [item]}
def pop(%Stack{items: [h | t]}), do: {:ok, h, %Stack{items: t}}
def pop(%Stack{items: []}), do: {:err, "no items"}
end
Elixir Ecosystem Highlights
● mix / hex
● Phoenix Framework
● ecto
● nerves
● distillery / edeliver
Further info
● elixir-lang.org
● hex.pm
● phoenix-framework.org
● Elixir Sips
● Google: “awesome elixir”
● elixirstatus.com
Thank you
info@labzero.com

Contenu connexe

Tendances

built in function
built in functionbuilt in function
built in functionkinzasaeed4
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJSunil OS
 
function in c
function in cfunction in c
function in csubam3
 
F# Presentation
F# PresentationF# Presentation
F# Presentationmrkurt
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Sumant Tambe
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScripttmont
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Mercury: A Functional Review
Mercury: A Functional ReviewMercury: A Functional Review
Mercury: A Functional ReviewMark Cheeseman
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operationsarchikabhatia
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
programming in C++ report
programming in C++ reportprogramming in C++ report
programming in C++ reportvikram mahendra
 
Advance python programming
Advance python programming Advance python programming
Advance python programming Jagdish Chavan
 
Class xi sample paper (Computer Science)
Class xi sample paper (Computer Science)Class xi sample paper (Computer Science)
Class xi sample paper (Computer Science)MountAbuRohini
 
Working with functions in matlab
Working with functions in matlabWorking with functions in matlab
Working with functions in matlabharman kaur
 
The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtendtakezoe
 
C sharp 8.0 new features
C sharp 8.0 new featuresC sharp 8.0 new features
C sharp 8.0 new featuresMiguel Bernard
 

Tendances (20)

built in function
built in functionbuilt in function
built in function
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
 
C++11
C++11C++11
C++11
 
function in c
function in cfunction in c
function in c
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Mercury: A Functional Review
Mercury: A Functional ReviewMercury: A Functional Review
Mercury: A Functional Review
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operations
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Functions
FunctionsFunctions
Functions
 
programming in C++ report
programming in C++ reportprogramming in C++ report
programming in C++ report
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
 
Class xi sample paper (Computer Science)
Class xi sample paper (Computer Science)Class xi sample paper (Computer Science)
Class xi sample paper (Computer Science)
 
Working with functions in matlab
Working with functions in matlabWorking with functions in matlab
Working with functions in matlab
 
The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtend
 
C sharp 8.0 new features
C sharp 8.0 new featuresC sharp 8.0 new features
C sharp 8.0 new features
 
C++
C++C++
C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similaire à Introduction to Elixir

An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and VerificationKapilRaghunandanTrip
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdfKhaledIbrahim10923
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to ElixirDiacode
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)Sami Said
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptKamil Toman
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...Sang Don Kim
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
 
20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptx20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptxDianeKesler1
 
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...InfluxData
 

Similaire à Introduction to Elixir (20)

C
CC
C
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and Verification
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptx20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptx
 
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
Michael Hall [InfluxData] | Become an InfluxDB Pro in 20 Minutes | InfluxDays...
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 

Dernier

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 

Dernier (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 

Introduction to Elixir

  • 1. Lab Zero Innovations Inc. 77 Battery Street San Francisco, CA 94111 415.839.6861 info@labzero.com labzero.com Introduction to Elixir Brien Wankel Sasha Voynow
  • 2.
  • 3. What is Elixir? 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. ● First release: 2012 ● 1.0 in September 2014 ● Takes advantage of the Erlang VM ○ Compiled to Erlang (BEAM) bytecode ○ Runs on the Erlang (BEAM) VM
  • 4. Wait, did you say Erlang? ● Created in 1986 by Ericsson for telco exchange software ● Open Source since 1988 ● A VM (BEAM) ● A set of libraries and tools (OTP) ● Engineered for ○ Fault tolerance (uptime!) ○ Responsiveness (low latency!) ○ Distributed computing (clustering! concurrency!)
  • 5. Open Telecom Platform (OTP) Search YouTube for: “Erlang: The Movie”
  • 6. OTP ● a set of tools and libraries for implementing fault tolerant server applications ● Some OTP jargon ○ Process: An extremely lightweight unit of execution ○ Application: A tree/graph of processes that are managed together. ○ Project: What is normally thought of as an application. A number of applications started together in the same VM
  • 7.
  • 8. OSS in Erlang Erlang in Production
  • 9. If Erlang is so great, why Elixir? Erlang has some UX issues ○ Funky, unfamiliar syntax ○ Lots of boilerplate % module_name.erl -module(module_name). % you may use some other name -compile(export_all). hello() -> io:format("~s~n", ["Hello world!"]). # module_name.ex defmodule ModuleName do def hello do IO.puts "Hello World" end end
  • 11. Types
  • 12. The Usual Suspects ● Value Types ○ Int ○ Float ○ Atom :yo ○ String / Binary
  • 13. ● Container Types ○ Tuple {1, :foo} ○ List [1,2,3] ○ Map %{foo: 10, bar: "baz" “foo” => “figgle”} ○ Struct ■ a named or tagged Map ■ %User{name: "Brien Wankel", company: "Lab Zero"} ● and some misc others
  • 15. The Match Operator (=) iex(1)> 3 = 3 3 iex(2)> x = 3 3 iex(3)> 3 = x 3 iex(4)> 2 = x ** (MatchError) no match of right hand side value: 3
  • 16. The Pin Operator (^) iex(1)> x = 3 3 iex(2)> y = x 3 iex(3)> x = 10 10 iex(4)> y 3 iex(5)> ^x = 3 ** (MatchError) no match of right hand side value: 3 iex(5)> ^x = 10 10
  • 17. Matching Lists iex(1)> [a,b,c] = [1,2,3] [1, 2, 3] iex(2)> a 1 iex(3)> b 2 iex(4)> c 3 iex(1)> [h | t] = [1,2,3] [1, 2, 3] iex(2)> h 1 iex(3)> t [2, 3]
  • 18. Matching Maps iex(1)> user = %{name: "Brien", status: :expired, state: "AZ"} %{name: "Brien", state: "AZ", status: :expired} iex(2)> %{name: theName} = user %{name: "Brien", state: "AZ", status: :expired} iex(3)> theName "Brien" iex(4)> %{name: theName, state: "CA"} = user ** (MatchError) no match of right hand side value: %{name: "Brien", state: "AZ", status: :expired}
  • 19. Matching in case statements def registered_users do case load_users_from_database() do {:ok, users} -> users {:err, error} -> IO.puts("something went wrong: #{error}") [] end end def charge_user(user) do case user do %User{account_type: :premium} -> charge_card(user) _ -> nil end end
  • 20. Functions # different functions # &Lunchdown.string_to_int/1 def string_to_int(x) do # ... end # &Lunchdown.string_to_int/2 def string_to_int(x, radix) do # ... end # multiple heads of the same function # &Lunchdown.find/1 def find([_h | _t] = ids) do # ... end def find(id) do # ... end
  • 21. Matching in Function Definitions def handle_cast(:increment, state) do {:noreply, state + 1} end def handle_cast(:decrement, state) do {:noreply, state - 1} end def charge_credit_card(user = %User{account_type: :premium}) do # charge the credit card end def charge_credit_card(_) do # no-op end
  • 22. Matching in Function Definitions # recursion def sum([]), do: 0 def sum*([h | t]) do h + sum(t) end
  • 23. Guard clauses * You can only use a subset of built-in functions and operators def charge_credit_card(cc_number, amount) when amount < 0, do: end def charge_credit_card(cc_number, amount) do # do some stuff end
  • 24. Pipelines and the |> operator ● A common elixir idiom for composing functions ● Building up complex transformations of data out of simpler ones in a way that is readable and intention revealing ● APIs are commonly designed to work well with pipelines
  • 25. Pipelines def user_by_uid(uid) do Authorization |> where([a], a.uid == ^uid) |> join(:inner, [a], u in User, u.id == a.user_id) |> preload(:user) |> Repo.one end def word_count(filename) do increment = fn x -> x + 1 end count_fun = fn w, m -> Map.update(m, w, 1, increment) end filename |> File.stream! |> Stream.map(&String.trim/1) |> Stream.flat_map(&String.split/1) |> Stream.map(&String.downcase/1) |> Enum.reduce(%{}, count_fun) end
  • 26. Modules ● The unit of code organization in Elixir ● Just a group of related functions. defmodule Stack do defstruct [:items] # this defines a struct %Stack with the field items def init do %Stack{items: []} end def push(%Stack(items: items}, item), do: %Stack{items: items ++ [item]} def pop(%Stack{items: [h | t]}), do: {:ok, h, %Stack{items: t}} def pop(%Stack{items: []}), do: {:err, "no items"} end
  • 27. Elixir Ecosystem Highlights ● mix / hex ● Phoenix Framework ● ecto ● nerves ● distillery / edeliver
  • 28. Further info ● elixir-lang.org ● hex.pm ● phoenix-framework.org ● Elixir Sips ● Google: “awesome elixir” ● elixirstatus.com