elixir for rubysts
DESCRIPTION
A basic quick intro to elixir for ruby programmersTRANSCRIPT
![Page 1: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/1.jpg)
elixir for Rubyists
![Page 2: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/2.jpg)
The lovechild of ruby & erlang
+ =
![Page 3: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/3.jpg)
What’s similar
![Page 4: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/4.jpg)
Syntaxdefmodule Underscore.Enum do!! def pull(list, other) do!! ! Enum.reject(list, fn(item)-> item in other end)!! end!end!
![Page 5: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/5.jpg)
Meta-Programmingdefmodule MyMacro do! defmacro unless(clause, options) do! quote do: if(!unquote(clause), unquote(options))! end!end!
![Page 6: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/6.jpg)
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
![Page 7: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/7.jpg)
Whats different
![Page 8: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/8.jpg)
Functional• Functions are first level citizens
!
!
• List Comprehensions
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]!
iex> lc x inlist [1, 2], y inlist [3, 4], do: x * y![3, 4, 6, 8]!
![Page 9: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/9.jpg)
Immutableiex> list = [:a, :b, :c]![:a, :b, :c]!iex> List.delete(list, :b)![:a, :c]!iex> list![:a, :b, :c]!iex> list = List.delete(list, :b)![:a, :c]!iex> list![:a, :c]!
![Page 10: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/10.jpg)
Pattern matchingiex> {[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!"!
![Page 11: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/11.jpg)
Pattern matching functions are amazing
defmodule MyEnum do! def sum([]), do: 0! def sum([head | tail]), do: head + sum(tail)!end!
![Page 12: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/12.jpg)
Whats AMAZING
![Page 13: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/13.jpg)
The pipe operatorWe all wrote code like this from time to time:
!
!
Or worse, this:
people = DB.find_customers!orders = Orders.for_customers(people)!tax = sales_tax(orders, 2013)!filing = prepare_filing(tax)!
filing = prepare_filing(sales_tax(Orders.for_customers(DB.find_customers)))!
![Page 14: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/14.jpg)
Ta daa
filing = DB.find_customers!! |> Orders.for_customers!! |> sales_tax(2013)!! |> prepare_filing!
![Page 15: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/15.jpg)
Function captureEnum.map [1,2,3], fn(x) -> x * x end!!!!Enum.map [1,2,3], &(&1 * &1)!
![Page 16: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/16.jpg)
Guard clausesdefmodule Factorial do! def of(0), do: 1! def of(n) when n > 0 do! n * of(n-1)! end!end!
![Page 17: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/17.jpg)
Optional(!) type checking@spec add(integer, integer), do: integer!def add(a, b), do: a + b!
![Page 18: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/18.jpg)
Concurrency
![Page 19: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/19.jpg)
Actor baseddefmodule 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
![Page 20: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/20.jpg)
!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!!
![Page 21: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/21.jpg)
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
![Page 22: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/22.jpg)
Native support for multi machine distribution
• Actors can be on a local or remote VM, and it’s transparent to you!
on machine1!~> iex --name [email protected] --cookie a_cookie_string!on machine2!~> iex --name [email protected] --cookie a_cookie_string!!iex([email protected])1> Node.connect :"[email protected]"!true!!iex([email protected])2> print_node_name = fn -> IO.puts Node.self end!#Function<erl_eval.20.80484245>!!iex([email protected])3> Node.spawn(:"[email protected]", print_node_name)[email protected]!#PID<7789.49.0> !
![Page 23: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/23.jpg)
The OTPOpen Telecom Platform - but nobody cares
![Page 24: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/24.jpg)
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
![Page 25: Elixir for rubysts](https://reader031.vdocuments.site/reader031/viewer/2022020921/5566139cd8b42a7d608b4807/html5/thumbnails/25.jpg)
Summery• Functional and fun
• FAST
• Low/high level language
• Mature VM/young language
• Scalable