41
Learning Elixir

Learning Elixir as a Rubyist

Embed Size (px)

Citation preview

Learning Elixir

What is it good for?

■ Fault tolerance - maximum uptime

■ Large number of connections / IO

■ High level of concurrency

■ Distributed across machines

1.Built on Erlang

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

Erlang downsides?

■ Learning Curve

■ Syntax based on Prolog, not as approachable (to some)

2.Enter Elixir

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

Look familiar?

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)

Immutability

Immutable data structures

Potential source of errors in Ruby:

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

Immutable data structures

Pattern Matching

Pattern Matching

‘=’ operator in action:

“You Keep Using That Word, I Do Not Think It

Means What You Think It Means

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

Pipe Operator

■ Functional Programming: Series of data transformations.

■ Pipe operator makes this cleaner:

|>

Pipe Operator

Metaprogramming

Metaprogramming

VS.

Macros

■ Allow extension of the language in a clean way

■ Can control execution of passed in code

■ Gives you access to passed in AST

Processes

Lightweight processes / Message passing

■ Native support for lightweight processes

■ Can start tens of thousands of processes

■ Processes are isolated

■ Communicate via messages

Lightweight processes / Message passing

OTP

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

Fault tolerance / supervision

Distribution

■ Distribution is free!

■ Can send and receive messages from other nodes

■ Can supervise other nodes

Tooling

Growing Ecosystem

Number of tools is growing

Rails Phoenix

ActiveRecord Ecto

Rack Plug

irb iex

rake / bundler mix

Benchmarks

Phoenix

Working on Exq library - Sidekiq for Elixir

Feel free to help out!

Try it out!

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