59
Bootstrap EASY FUN FOR BUSY DEVELOPERS |> Elixir

Bootstrap |> Elixir - Easy fun for busy developers

Embed Size (px)

Citation preview

Page 1: Bootstrap |> Elixir - Easy fun for busy developers

Bootstrap EASY FUN FOR BUSY DEVELOPERS

|> Elixir

Page 2: Bootstrap |> Elixir - Easy fun for busy developers

YET ANOTHER PROGRAMMING LANGUANGE?

Y A P L

Page 3: Bootstrap |> Elixir - Easy fun for busy developers

DON’T RUN AWAY

Elixir is special!

Page 4: Bootstrap |> Elixir - Easy fun for busy developers

Elixir in a Nutshell

= +Elixir in a Nutshell

+

Page 5: Bootstrap |> Elixir - Easy fun for busy developers

PROGRAMMERS OFTEN FEEL JOY WHEN THEY CAN CONCENTRATE ON THE CREATIVE SIDE OF PROGRAMMING, SO RUBY IS DESIGNED TO MAKE PROGRAMMERS HAPPY. I CONSIDER A PROGRAMMING LANGUAGE AS A USER INTERFACE, SO IT SHOULD FOLLOW THE PRINCIPLES OF USER INTERFACE.

Yukihiro Matsumoto, Ruby Inventor

Why Ruby is awesome

Page 6: Bootstrap |> Elixir - Easy fun for busy developers

Principle of Conciseness Principle of Consistency Principle of Flexibility

Why Ruby is awesome

Page 7: Bootstrap |> Elixir - Easy fun for busy developers

THE AXD301 HAS ACHIEVED A NINE NINES RELIABILITY (YES, YOU READ THAT RIGHT, 99.9999999%). LET’S PUT THIS IN CONTEXT: 5 NINES IS RECKONED TO BE GOOD (5.2 MINUTES OF DOWNTIME/YEAR). 7 NINES ALMOST UNACHIEVABLE ... BUT WE DID 9.

Joe Armstrong, Erlang Designer

Why Erlang is awesome

Page 8: Bootstrap |> Elixir - Easy fun for busy developers

Battle-proven BEAM and OTP

Ever seen WhatsApp crash?

Reactive before Reactive-is-HipTm

Actors before Actors-are—HipTm

Why Erlang is awesome

Page 9: Bootstrap |> Elixir - Easy fun for busy developers

Why Clojure is awesome

Because Rich Hickey is always right

Page 10: Bootstrap |> Elixir - Easy fun for busy developers

SO…WHY NOT DO RUBY, ERLANG OR CLOJURE?

Page 11: Bootstrap |> Elixir - Easy fun for busy developers

WHY NOT USE RUBY? SPEED, CONCURRENCY, SCALABILITY

Page 12: Bootstrap |> Elixir - Easy fun for busy developers

WHY NOT USE ERLANG? SYNTAX, BAROQUE TOOLING

Page 13: Bootstrap |> Elixir - Easy fun for busy developers

WHY NOT USE CLOJURE? …NO REASON, REALLY. GO AHEAD USE IT!

Page 14: Bootstrap |> Elixir - Easy fun for busy developers

Scalability, lightweight Threads

Fault-tolerance, Supervisor

Functional, immutable DSLs using Meta-Programming

Mix, Hex, ExUnit,…

REPL for easy learning

Why Elixir is awesome

Page 15: Bootstrap |> Elixir - Easy fun for busy developers

Scalability, lightweight Threads

Fault-tolerance, Supervisor

Functional, immutable DSLs using Meta-Programming

Mix, Hex, ExUnit,…

REPL for easy learning

Why Elixir is awesome

Page 16: Bootstrap |> Elixir - Easy fun for busy developers

100000 Processes

1..100_000 |> Enum.map(&(Task.async( fn -> &1 * &1 end ))) |> Enum.map(&Task.await/1)

Page 17: Bootstrap |> Elixir - Easy fun for busy developers

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, ...]

100000 Processes

Page 18: Bootstrap |> Elixir - Easy fun for busy developers

1..100_000 |> Enum.map(&(Task.async( fn -> &1 * &1 end ))) |> Enum.map(&Task.await/1)?what?

100000 Processes

Page 19: Bootstrap |> Elixir - Easy fun for busy developers

Quick syntax

bootcamp

Page 20: Bootstrap |> Elixir - Easy fun for busy developers

Creating a project

$ mix new demo $ cd demo $ mix test

Page 21: Bootstrap |> Elixir - Easy fun for busy developers

Read Eval Print Loop

$ iex -S mix Erlang/OTP 18 …

Interactive Elixir (1.2.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> IO.puts “Hello Elixir” Hello Elixir :ok

Page 22: Bootstrap |> Elixir - Easy fun for busy developers

Transform - don’t mutate

10 |> fib |> IO.puts

IO.puts(fib(10))

transformation pipeline

Page 23: Bootstrap |> Elixir - Easy fun for busy developers

Declaring functions

def fib(0), do: 0

def fib(0) do 0 end (n-1) + fib(n-2) end

same thing

Page 24: Bootstrap |> Elixir - Easy fun for busy developers

Pattern matching

def fib(0), do: 0

def fib(1), do: 1

def fib(n) when n > 1 iex> fib(1)

pattern matching

Page 25: Bootstrap |> Elixir - Easy fun for busy developers

Guard expressions

def fib(n) when n > 1 do fib(n-1) + fib(n-2) end

guard expression

Page 26: Bootstrap |> Elixir - Easy fun for busy developers

iex> fib(-3) ** (FunctionClauseError) no function clause matching in fib/1 (fibnew) lib/fib2.ex:2: fib(-3)

Things go wrong

Page 27: Bootstrap |> Elixir - Easy fun for busy developers

Anonymous functions

fn n -> n + 2 end

&( &1 + 2 )

same thing

Page 28: Bootstrap |> Elixir - Easy fun for busy developers

Function shortcuts

IO.puts/2

def puts(device, item)

/2 denotes the number of args

Page 29: Bootstrap |> Elixir - Easy fun for busy developers

Compile-time structural decomposition

def sqrt([h | t]), do: …

iex> sqrt([1, 2, 3, 4 ])

Page 30: Bootstrap |> Elixir - Easy fun for busy developers

Show me the code Demo

Page 31: Bootstrap |> Elixir - Easy fun for busy developers

ELIXIR APPLICATION DESIGN APPLICATIONS, SUPERVISORS, PROCESSES

Page 32: Bootstrap |> Elixir - Easy fun for busy developers

Shared resources Shared state Shared stability

PROCESSSHARED STATE

Page 33: Bootstrap |> Elixir - Easy fun for busy developers

Shared nothing Message passing

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

Page 34: Bootstrap |> Elixir - Easy fun for busy developers

Abstractions for ResilienceAgent…abstractions around state GenServer…long-running, messaging Supervisor…let it crash Application…just think component

Page 35: Bootstrap |> Elixir - Easy fun for busy developers

Abstractions for ResilienceAgent…abstractions around state GenServer…long-running, messaging Supervisor…let it crash Application…just think component

Page 36: Bootstrap |> Elixir - Easy fun for busy developers

Message-based design using GenServer

BA

GenServer.start_link

{:ok, #PID<0.112.0>}

Page 37: Bootstrap |> Elixir - Easy fun for busy developers

Message-based design using GenServer

BA

GenServer.call(B, {:sum, 1..3})

Page 38: Bootstrap |> Elixir - Easy fun for busy developers

Message-based design using GenServer

BA

handle_call({:sum, 1..3}, from, state)

Page 39: Bootstrap |> Elixir - Easy fun for busy developers

Message-based design using GenServer

BA

{:reply, {:ok, 6}, new_state)

Page 40: Bootstrap |> Elixir - Easy fun for busy developers

GenServer Demo

Page 41: Bootstrap |> Elixir - Easy fun for busy developers
Page 42: Bootstrap |> Elixir - Easy fun for busy developers

Move risk to the bottom of the supervision tree

APPLICATION

SUPERVISOR WORKER C

WORKER A WORKER B Here be dragons…FILE IO DB ACCESS

Page 43: Bootstrap |> Elixir - Easy fun for busy developers

Supervisors watch their children

APPLICATION

SUPERVISOR WORKER C

WORKER A WORKER B

:one_for_one!

Page 44: Bootstrap |> Elixir - Easy fun for busy developers

:one_for_one replaces failed process

APPLICATION

SUPERVISOR WORKER C

WORKER A WORKER B WORKER B’ Auto-restart

Page 45: Bootstrap |> Elixir - Easy fun for busy developers

Supervisor Demo

Page 46: Bootstrap |> Elixir - Easy fun for busy developers

Processes are distributed across nodes

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

PROCESS

:”one@foo”

:”two@foo”

Page 47: Bootstrap |> Elixir - Easy fun for busy developers

Nodes and distribution Demo

Page 48: Bootstrap |> Elixir - Easy fun for busy developers

MORE COOL FEATURES TEASERS ONLY

Page 49: Bootstrap |> Elixir - Easy fun for busy developers

Testing documentation

@doc """ iex> sieve(10) [2, 3, 5, 7] """ def sieve(n) do ... end

defmodule Test do use ExUnit.Case

doctest … end

Page 50: Bootstrap |> Elixir - Easy fun for busy developers

Hygienic Macrosdefmacro time([do: body]) do quote do s = :os.system_time(…)

unquote(body)

f = :os.system_time(…)

info(“Took #{s-f} ms") end end

time do IO.puts "Something" :timer.sleep(100) end

[info] Took 101 ms

Page 51: Bootstrap |> Elixir - Easy fun for busy developers

There is so much more

Page 52: Bootstrap |> Elixir - Easy fun for busy developers

Protocols Sigils

Umbrella projects Ecto

Phoenix Hot code replacement

Page 53: Bootstrap |> Elixir - Easy fun for busy developers

SHOULD WE ALL START BUILDING EVERYTHING

WITH ELIXIR?

Page 54: Bootstrap |> Elixir - Easy fun for busy developers
Page 55: Bootstrap |> Elixir - Easy fun for busy developers

WELL….MAYBE NOT?!

Page 56: Bootstrap |> Elixir - Easy fun for busy developers

New insights and ideas Clean patterns

Architecture for IoT Vibrant Community

Page 57: Bootstrap |> Elixir - Easy fun for busy developers

学⼀一⻔门语⾔言,就是多⼀一个观察世界的窗户。To learn a language is to have one more window from which to look at the world

Page 58: Bootstrap |> Elixir - Easy fun for busy developers

Elixir Homepage, http://elixir-lang.org/

Dave Thomas, Programming Elixir

Fred Hebert, Stuff Goes Bad: Erlang in Anger

José Valim, Introduction to Elixir https://youtu.be/41PvAPSX0wg

Slides + Code, https://git.io/vKUGc

Do you want to know more?

Page 59: Bootstrap |> Elixir - Easy fun for busy developers

Thank you very much!<[email protected]> @koenighotze