SlideShare una empresa de Scribd logo
1 de 21
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

Más contenido relacionado

La actualidad más candente

使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架
jeffz
 
Intro to Java 8 Closures (Dainius Mezanskas)
Intro to Java 8 Closures (Dainius Mezanskas)Intro to Java 8 Closures (Dainius Mezanskas)
Intro to Java 8 Closures (Dainius Mezanskas)
Kaunas Java User Group
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
WebF
 
Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in Scala
Ayush Mishra
 

La actualidad más candente (20)

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

Destacado

Bottleneck in Elixir Application - Alexey Osipenko
 Bottleneck in Elixir Application - Alexey Osipenko  Bottleneck in Elixir Application - Alexey Osipenko
Bottleneck in Elixir Application - Alexey Osipenko
Elixir Club
 
The mystique of erlang
The mystique of erlangThe mystique of erlang
The mystique of erlang
Carob Cherub
 
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Magic Clusters and Where to Find Them 2.0 - Eugene Pirogov
Elixir Club
 
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Flowex: Flow-Based Programming with Elixir GenStage - Anton MishchukFlowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Flowex: Flow-Based Programming with Elixir GenStage - Anton Mishchuk
Elixir Club
 

Destacado (20)

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

Similar a Elixir basics

Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
Chia-Chi Chang
 

Similar a Elixir basics (20)

Cypher.PL: an executable specification of Cypher semantics
Cypher.PL: an executable specification of Cypher semanticsCypher.PL: an executable specification of Cypher semantics
Cypher.PL: an executable specification of Cypher semantics
 
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISP
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
R workshop
R workshopR workshop
R workshop
 
Cooking Software101
Cooking Software101Cooking Software101
Cooking Software101
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
 
10-IDL.pptx
10-IDL.pptx10-IDL.pptx
10-IDL.pptx
 
Intro To Erlang
Intro To ErlangIntro To Erlang
Intro To Erlang
 
R programmingmilano
R programmingmilanoR programmingmilano
R programmingmilano
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
Elixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental ConceptsElixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental Concepts
 
Groovy unleashed
Groovy unleashed Groovy unleashed
Groovy unleashed
 
Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
 

Más de Ruben Amortegui (7)

Ecto
EctoEcto
Ecto
 
Working with-phoenix
Working with-phoenixWorking with-phoenix
Working with-phoenix
 
Elixir koans
Elixir koansElixir koans
Elixir koans
 
Concurrent programming
Concurrent programmingConcurrent programming
Concurrent programming
 
Elixir otp-basics
Elixir otp-basicsElixir otp-basics
Elixir otp-basics
 
Elixir absinthe-basics
Elixir absinthe-basicsElixir absinthe-basics
Elixir absinthe-basics
 
Phoenix basics
Phoenix basicsPhoenix basics
Phoenix basics
 

Último

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
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
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
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
 
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 🔝✔️✔️
 

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.