SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
elixir
for Rubyists
The lovechild of ruby &
erlang

+

=
What’s similar
Syntax
defmodule Underscore.Enum do!
! def pull(list, other) do!
! ! Enum.reject(list, fn(item)-> item in other end)!
! end!
end!
Meta-Programming
defmodule MyMacro do!
defmacro unless(clause, options) do!
quote do: if(!unquote(clause), unquote(options))!
end!
end!
And other fun stuff
•

Huge, expressive standard lib

•

Heredocs, Multiline Strings, String Interpolation

•

Sigils(i.e %w/%c etc)

•

Great documentation

•

ITS FUN(C) TO WORK WITH
Whats different
Functional
•

Functions are first level citizens

iex> square = fn x -> x * x end!
!
#Function<6.17052888 in :erl_eval.expr/5>!
iex> Enum.map(1..10, square)!
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]!
!

•

List Comprehensions

iex> lc x inlist [1, 2], y inlist [3, 4], do: x * y!
[3, 4, 6, 8]!
Immutable
iex>
[:a,
iex>
[:a,
iex>
[:a,
iex>
[:a,
iex>
[:a,

list = [:a, :b, :c]!
:b, :c]!
List.delete(list, :b)!
:c]!
list!
:b, :c]!
list = List.delete(list, :b)!
:c]!
list!
:c]!
Pattern matching
iex> {[head | tail], {:atom, msg}} = !
! ! ! ! {[1, 2, 3], {:atom, "PATTERN MATCHING FTW!"}} !
{[1, 2, 3], {:atom, "PATTERN MATCHING FTW!"}}!
iex> head!
1!
iex> tail!
[2, 3]!
iex> msg!
"PATTERN MATCHING FTW!"!
Pattern matching functions are
amazing
defmodule MyEnum do!
def sum([]), do: 0!
def sum([head | tail]), do: head + sum(tail)!
end!
Whats AMAZING
The pipe operator
We all wrote code like this from time to time:
people
!
orders
tax
filing
!

=
=
=
=

DB.find_customers!
Orders.for_customers(people)!
sales_tax(orders, 2013)!
prepare_filing(tax)!

Or worse, this:
filing =
prepare_filing(sales_tax(Orders.for
_customers(DB.find_customers)))!
Ta daa
filing = DB.find_customers!
! |> Orders.for_customers!
! |> sales_tax(2013)!
! |> prepare_filing!
Function capture
Enum.map [1,2,3], fn(x) -> x * x end!
!
!
!

Enum.map [1,2,3], &(&1 * &1)!
Guard clauses
defmodule Factorial do!
def of(0), do: 1!
def of(n) when n > 0 do!
n * of(n-1)!
end!
end!
Optional(!) type checking
@spec add(integer, integer), do: integer!
def add(a, b), do: a + b!
Concurrency
Actor based
defmodule Greeter do!
def greet do !
receive do !
{:english, name} -> !
IO.puts "Hello, #{name}."!
greet!
{:chinese, name} -> !
IO.puts "你½ 好¥½, #{name}."!
greet!
{:spanish, name} -> !
IO.puts "¡Hola!, #{name}."!
greet!
:exit -> !
IO.puts "Bye bye!"!
_ -> !
IO.puts "I don't understand ... but Hello anyway!"!
greet!
end!
end!
end
!
iex> greeter = spawn(Greeter, :greet, [])!
#PID<0.52.0>!
!
iex> greeter <- {:english, 'Amy'}!
Hello, Amy.!
{:english, ‘Amy'}!
!
iex> greeter <- {:chinese, 'Ben'}!
{:chinese, 'Ben'}!
你½ 好¥½, Ben.!
!
iex> greeter <- {:spanish, 'Charlie'}!
{:spanish, 'Charlie'}!
¡Hola!, Charlie.!
!
iex(31)> greeter <- {:klingon, 'David'}!
I don't understand ... but Hello anyway!!
Few notes about actors
•

They’re fast & lightweight - you can have 10,000 of
them on a tiny machine.

•

The queue is managed by the VM

•

Support hot-swapping
Native support for multi
machine distribution
•

on
~>
on
~>

!

Actors can be on a local or remote VM, and it’s
transparent to you!
machine1!
iex --name node1@machine1.com --cookie a_cookie_string!
machine2!
iex --name node2@machine2.com --cookie a_cookie_string!

iex(node1@machine1.com)1> Node.connect :"node2@machine2.com"!
true!

!

iex(node1@machine1.com)2> print_node_name = fn -> IO.puts Node.self end!
#Function<erl_eval.20.80484245>!

!

iex(node1@machine1.com)3> Node.spawn(:"node2@machine2.com", print_node_name)!
node2@machine2.com!
#PID<7789.49.0> !
The OTP
Open Telecom Platform - but nobody cares
Reliability
•

Linked processes/Supervisor tree

•

Failover nodes

•

Hot code reload

•

20 years of battle-tested code, it’s VERY hard to break.

•

used by a very huge scale applications:
•

Facebook Messages/ WhatsApp

•

Riak/ CouchDB/ RabbitMQ
Summery
•

Functional and fun

•

FAST

•

Low/high level language

•

Mature VM/young language

•

Scalable

Weitere ähnliche Inhalte

Was ist angesagt?

"Lego Programming" with Lorzy
"Lego Programming" with Lorzy"Lego Programming" with Lorzy
"Lego Programming" with Lorzy
clkao
 

Was ist angesagt? (13)

Threequals - Case Equality in Ruby
Threequals - Case Equality in RubyThreequals - Case Equality in Ruby
Threequals - Case Equality in Ruby
 
"Lego Programming" with Lorzy
"Lego Programming" with Lorzy"Lego Programming" with Lorzy
"Lego Programming" with Lorzy
 
ppt9
ppt9ppt9
ppt9
 
name name2 n
name name2 nname name2 n
name name2 n
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmers
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 
ppt18
ppt18ppt18
ppt18
 
A (very brief) into to Functional Programming
A (very brief) into to Functional ProgrammingA (very brief) into to Functional Programming
A (very brief) into to Functional Programming
 
Erlang/OTP for Rubyists
Erlang/OTP for RubyistsErlang/OTP for Rubyists
Erlang/OTP for Rubyists
 
Kotlin - Better Java
Kotlin - Better JavaKotlin - Better Java
Kotlin - Better Java
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
Elm kyivfprog 2015
Elm kyivfprog 2015Elm kyivfprog 2015
Elm kyivfprog 2015
 

Ähnlich wie Elixir for rubysts

Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
Wen-Tien Chang
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
Wen-Tien Chang
 
Chicago Elixir - Elixir Intro
Chicago Elixir - Elixir IntroChicago Elixir - Elixir Intro
Chicago Elixir - Elixir Intro
drewolson
 

Ähnlich wie Elixir for rubysts (20)

Go(lang) for the Rubyist
Go(lang) for the RubyistGo(lang) for the Rubyist
Go(lang) for the Rubyist
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Rails by example
Rails by exampleRails by example
Rails by example
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 
Long Live the Rubyist
Long Live the RubyistLong Live the Rubyist
Long Live the Rubyist
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!
 
Introduction to Ruby, Rails, and Ruby on Rails
Introduction to Ruby, Rails, and Ruby on RailsIntroduction to Ruby, Rails, and Ruby on Rails
Introduction to Ruby, Rails, and Ruby on Rails
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
Chicago Elixir - Elixir Intro
Chicago Elixir - Elixir IntroChicago Elixir - Elixir Intro
Chicago Elixir - Elixir Intro
 
Ruby
RubyRuby
Ruby
 
python.pdf
python.pdfpython.pdf
python.pdf
 
Meet ruby
Meet rubyMeet ruby
Meet ruby
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Elixir for rubysts

  • 2. The lovechild of ruby & erlang + =
  • 4. Syntax defmodule Underscore.Enum do! ! def pull(list, other) do! ! ! Enum.reject(list, fn(item)-> item in other end)! ! end! end!
  • 5. Meta-Programming defmodule MyMacro do! defmacro unless(clause, options) do! quote do: if(!unquote(clause), unquote(options))! end! end!
  • 6. And other fun stuff • Huge, expressive standard lib • Heredocs, Multiline Strings, String Interpolation • Sigils(i.e %w/%c etc) • Great documentation • ITS FUN(C) TO WORK WITH
  • 8. Functional • Functions are first level citizens iex> square = fn x -> x * x end! ! #Function<6.17052888 in :erl_eval.expr/5>! iex> Enum.map(1..10, square)! [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]! ! • List Comprehensions iex> lc x inlist [1, 2], y inlist [3, 4], do: x * y! [3, 4, 6, 8]!
  • 9. Immutable iex> [:a, iex> [:a, iex> [:a, iex> [:a, iex> [:a, list = [:a, :b, :c]! :b, :c]! List.delete(list, :b)! :c]! list! :b, :c]! list = List.delete(list, :b)! :c]! list! :c]!
  • 10. Pattern matching iex> {[head | tail], {:atom, msg}} = ! ! ! ! ! {[1, 2, 3], {:atom, "PATTERN MATCHING FTW!"}} ! {[1, 2, 3], {:atom, "PATTERN MATCHING FTW!"}}! iex> head! 1! iex> tail! [2, 3]! iex> msg! "PATTERN MATCHING FTW!"!
  • 11. Pattern matching functions are amazing defmodule MyEnum do! def sum([]), do: 0! def sum([head | tail]), do: head + sum(tail)! end!
  • 13. The pipe operator We all wrote code like this from time to time: people ! orders tax filing ! = = = = DB.find_customers! Orders.for_customers(people)! sales_tax(orders, 2013)! prepare_filing(tax)! Or worse, this: filing = prepare_filing(sales_tax(Orders.for _customers(DB.find_customers)))!
  • 14. Ta daa filing = DB.find_customers! ! |> Orders.for_customers! ! |> sales_tax(2013)! ! |> prepare_filing!
  • 15. Function capture Enum.map [1,2,3], fn(x) -> x * x end! ! ! ! Enum.map [1,2,3], &(&1 * &1)!
  • 16. Guard clauses defmodule Factorial do! def of(0), do: 1! def of(n) when n > 0 do! n * of(n-1)! end! end!
  • 17. Optional(!) type checking @spec add(integer, integer), do: integer! def add(a, b), do: a + b!
  • 19. Actor based defmodule Greeter do! def greet do ! receive do ! {:english, name} -> ! IO.puts "Hello, #{name}."! greet! {:chinese, name} -> ! IO.puts "你½ 好¥½, #{name}."! greet! {:spanish, name} -> ! IO.puts "¡Hola!, #{name}."! greet! :exit -> ! IO.puts "Bye bye!"! _ -> ! IO.puts "I don't understand ... but Hello anyway!"! greet! end! end! end
  • 20. ! iex> greeter = spawn(Greeter, :greet, [])! #PID<0.52.0>! ! iex> greeter <- {:english, 'Amy'}! Hello, Amy.! {:english, ‘Amy'}! ! iex> greeter <- {:chinese, 'Ben'}! {:chinese, 'Ben'}! 你½ 好¥½, Ben.! ! iex> greeter <- {:spanish, 'Charlie'}! {:spanish, 'Charlie'}! ¡Hola!, Charlie.! ! iex(31)> greeter <- {:klingon, 'David'}! I don't understand ... but Hello anyway!!
  • 21. Few notes about actors • They’re fast & lightweight - you can have 10,000 of them on a tiny machine. • The queue is managed by the VM • Support hot-swapping
  • 22. Native support for multi machine distribution • on ~> on ~> ! Actors can be on a local or remote VM, and it’s transparent to you! machine1! iex --name node1@machine1.com --cookie a_cookie_string! machine2! iex --name node2@machine2.com --cookie a_cookie_string! iex(node1@machine1.com)1> Node.connect :"node2@machine2.com"! true! ! iex(node1@machine1.com)2> print_node_name = fn -> IO.puts Node.self end! #Function<erl_eval.20.80484245>! ! iex(node1@machine1.com)3> Node.spawn(:"node2@machine2.com", print_node_name)! node2@machine2.com! #PID<7789.49.0> !
  • 23. The OTP Open Telecom Platform - but nobody cares
  • 24. Reliability • Linked processes/Supervisor tree • Failover nodes • Hot code reload • 20 years of battle-tested code, it’s VERY hard to break. • used by a very huge scale applications: • Facebook Messages/ WhatsApp • Riak/ CouchDB/ RabbitMQ
  • 25. Summery • Functional and fun • FAST • Low/high level language • Mature VM/young language • Scalable