View
268
Download
0
Category
Preview:
Citation preview
Bootstrap EASY FUN FOR BUSY DEVELOPERS
|> Elixir
YET ANOTHER PROGRAMMING LANGUANGE?
Y A P L
DON’T RUN AWAY
Elixir is special!
Elixir in a Nutshell
= +Elixir in a Nutshell
+
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
Principle of Conciseness Principle of Consistency Principle of Flexibility
Why Ruby is awesome
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
Battle-proven BEAM and OTP
Ever seen WhatsApp crash?
Reactive before Reactive-is-HipTm
Actors before Actors-are—HipTm
Why Erlang is awesome
Why Clojure is awesome
Because Rich Hickey is always right
SO…WHY NOT DO RUBY, ERLANG OR CLOJURE?
WHY NOT USE RUBY? SPEED, CONCURRENCY, SCALABILITY
WHY NOT USE ERLANG? SYNTAX, BAROQUE TOOLING
WHY NOT USE CLOJURE? …NO REASON, REALLY. GO AHEAD USE IT!
Scalability, lightweight Threads
Fault-tolerance, Supervisor
Functional, immutable DSLs using Meta-Programming
Mix, Hex, ExUnit,…
REPL for easy learning
Why Elixir is awesome
Scalability, lightweight Threads
Fault-tolerance, Supervisor
Functional, immutable DSLs using Meta-Programming
Mix, Hex, ExUnit,…
REPL for easy learning
Why Elixir is awesome
100000 Processes
1..100_000 |> Enum.map(&(Task.async( fn -> &1 * &1 end ))) |> Enum.map(&Task.await/1)
[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
1..100_000 |> Enum.map(&(Task.async( fn -> &1 * &1 end ))) |> Enum.map(&Task.await/1)?what?
100000 Processes
Quick syntax
bootcamp
Creating a project
$ mix new demo $ cd demo $ mix test
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
Transform - don’t mutate
10 |> fib |> IO.puts
IO.puts(fib(10))
transformation pipeline
Declaring functions
def fib(0), do: 0
def fib(0) do 0 end (n-1) + fib(n-2) end
same thing
Pattern matching
def fib(0), do: 0
def fib(1), do: 1
def fib(n) when n > 1 iex> fib(1)
pattern matching
Guard expressions
def fib(n) when n > 1 do fib(n-1) + fib(n-2) end
guard expression
iex> fib(-3) ** (FunctionClauseError) no function clause matching in fib/1 (fibnew) lib/fib2.ex:2: fib(-3)
Things go wrong
Anonymous functions
fn n -> n + 2 end
&( &1 + 2 )
same thing
Function shortcuts
IO.puts/2
def puts(device, item)
/2 denotes the number of args
Compile-time structural decomposition
def sqrt([h | t]), do: …
iex> sqrt([1, 2, 3, 4 ])
Show me the code Demo
ELIXIR APPLICATION DESIGN APPLICATIONS, SUPERVISORS, PROCESSES
Shared resources Shared state Shared stability
PROCESSSHARED STATE
Shared nothing Message passing
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
Abstractions for ResilienceAgent…abstractions around state GenServer…long-running, messaging Supervisor…let it crash Application…just think component
Abstractions for ResilienceAgent…abstractions around state GenServer…long-running, messaging Supervisor…let it crash Application…just think component
Message-based design using GenServer
BA
GenServer.start_link
{:ok, #PID<0.112.0>}
Message-based design using GenServer
BA
GenServer.call(B, {:sum, 1..3})
Message-based design using GenServer
BA
handle_call({:sum, 1..3}, from, state)
Message-based design using GenServer
BA
{:reply, {:ok, 6}, new_state)
GenServer Demo
Move risk to the bottom of the supervision tree
APPLICATION
SUPERVISOR WORKER C
WORKER A WORKER B Here be dragons…FILE IO DB ACCESS
Supervisors watch their children
APPLICATION
SUPERVISOR WORKER C
WORKER A WORKER B
:one_for_one!
:one_for_one replaces failed process
APPLICATION
SUPERVISOR WORKER C
WORKER A WORKER B WORKER B’ Auto-restart
Supervisor Demo
Processes are distributed across nodes
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
PROCESS
:”one@foo”
:”two@foo”
Nodes and distribution Demo
MORE COOL FEATURES TEASERS ONLY
Testing documentation
@doc """ iex> sieve(10) [2, 3, 5, 7] """ def sieve(n) do ... end
defmodule Test do use ExUnit.Case
doctest … end
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
There is so much more
Protocols Sigils
Umbrella projects Ecto
Phoenix Hot code replacement
SHOULD WE ALL START BUILDING EVERYTHING
WITH ELIXIR?
WELL….MAYBE NOT?!
New insights and ideas Clean patterns
Architecture for IoT Vibrant Community
学⼀一⻔门语⾔言,就是多⼀一个观察世界的窗户。To learn a language is to have one more window from which to look at the world
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?
Thank you very much!<david.schmitz@senacor.com> @koenighotze
Recommended