learning elixir as a rubyist
TRANSCRIPT
What is it good for?
■ Fault tolerance - maximum uptime
■ Large number of connections / IO
■ High level of concurrency
■ Distributed across machines
Erlang - 1986It was originally designed by Ericsson to support distributed, fault-tolerant, soft real-time, highly available, non-stop applications. It supports hot swapping, thus code can be changed without stopping a system.
WhatsAppMessaging app acquired by Facebook for $19 billion. Used Erlang for handling 2 million connections a server. 10 team members work on Erlang and they handle both development and ops.
RabbitMQRabbitMQ is an open source message broker software that implements the Advanced Message Queuing Protocol (AMQP). It is fault tolerant and distributed.
Riak is a distributed NoSQL key-value data store that offers high availability, fault tolerance, operational simplicity, and scalability.
Erlang in the Wild
About Elixir
■ Invented by José Valim, Rails committer
■ Syntax inspired by○ Ruby○ Clojure
■ Nicely bring together strengths of these languages while gaining benefits of Erlang VM
Beyond Ruby...
Pattern Matching
[h | _] = [1, 2, 3]
Immutabilitysocket
|> read_line()
|> write_line(socket)
Pipe Operatorsocket
|> read_line()
|> write_line(socket)
Protocolsdefprotocol Blank do
@doc "Returns true if empty"
def blank?(data)
end
LW Processessend self(), {:hello, "world"}
receive do
{:hello, msg} -> msg
{:world, msg} -> "won't match"
end
Metaprogramming
quote do: sum(1, 2 + 3, 4)
Immutable data structures
What’s wrong with this?
■ You don’t know what to expect of your data when you call a method
■ Especially painful for threading○ Concurrent modification errors○ Race conditions
Immutable data structures
In Elixir all data structures are immutable!!
■ You can safely share with other processes
■ Don’t have to worry about data changing under the covers
Pattern Matching
The ‘=’ operator handles assignment and comparison at the same time.
Can “destructure” complex types and assign variables automatically.
Pattern matching
Fail when error code is not as expected:
Pluck out variables from complex structures
Pattern matching in methods
Multiple method definitions, match params:
No need for guards inside methods
Pipe Operator
■ Functional Programming: Series of data transformations.
■ Pipe operator makes this cleaner:
|>
Macros
■ Allow extension of the language in a clean way
■ Can control execution of passed in code
■ Gives you access to passed in AST
Lightweight processes / Message passing
■ Native support for lightweight processes
■ Can start tens of thousands of processes
■ Processes are isolated
■ Communicate via messages
OTP / Supervision hierarchy
■ OTP= “Rails” of event driven architecture
■ Supervision hierarchies help you define fault tolerance
■ “Let it crash” - reboot with known state
■ Define supervisors for processes and the process / dependencies for restarting
Distribution
■ Distribution is free!
■ Can send and receive messages from other nodes
■ Can supervise other nodes
Growing Ecosystem
Number of tools is growing
Rails Phoenix
ActiveRecord Ecto
Rack Plug
irb iex
rake / bundler mix
Resources
■ http://elixir-lang.org/crash-course.html■ http://learnyousomeerlang.com/■ https://pragprog.com/book/elixir/programming-elixir■ https://www.manning.com/books/elixir-in-action■ http://www.infoq.com/presentations/Value-Identity-State-Rich-Hickey■ https://github.com/mroth/phoenix-showdown