Skip to content

Instantly share code, notes, and snippets.

@schmalz
schmalz / coffee.lfe
Last active September 6, 2017 09:47
Designing for Scalability with Erlang/OTP - Ch 6 - Coffee FSM - Pure LFE
(defmodule coffee
(export
(tea 0)
(espresso 0)
(americano 0)
(cappuccino 0)
(pay 1)
(cup-removed 0)
(cancel 0)
(start_link 0)
@schmalz
schmalz / coffee-fsm.lfe
Created September 6, 2017 15:05
Designing for Scalability with Erlang/OTP - Ch 6 - Coffee FSM - LFE + OTP
(defmodule coffee-fsm
(behaviour gen_fsm)
(export
(start-link 0)
(stop 0)
(handle_sync_event 4)
(init 1)
(terminate 3)
(selection 2)
(payment 2)
@schmalz
schmalz / my-supervisor.lfe
Created September 11, 2017 11:06
Designing for Scalability with Erlang/OTP - Ch 8 - Coffee Supervisor - Pure LFE
(defmodule my-supervisor
(export
(start 2)
(init 1)
(stop 1)))
(defun start (name child-spec-list)
"Start a supervisor with `name` and `child-spec-list`."
(let ((pid (spawn (MODULE) 'init (list child-spec-list))))
(register name pid)
@schmalz
schmalz / hlr.ex
Created September 13, 2017 11:58
Designing for Scalability with Erlang/OTP - Ch 2 - HLR Module - Elixir
defmodule HLR do
@moduledoc"""
Maintain the associations between Mobile Subscriber Integrated Services Digital Network (MSISDN) numbers and process
identifiers (PID).
"""
def new() do
:ets.new(:msisdn_pid, [:named_table])
:ets.new(:pid_msisdn, [:named_table])
@schmalz
schmalz / frequency.ex
Created September 13, 2017 15:04
Designing for Scalability with Erlang/OTP - Ch 3 - Frequency Module - Elixir
defmodule Frequency do
@moduledoc"""
"""
# Client API
def start() do
Process.register(Process.spawn(&init/0, []), :frequency)
end
@schmalz
schmalz / frequency.ex
Created September 14, 2017 07:56
Designing for Scalability with Erlang/OTP - Ch 4 - Frequency GenServer Module - Elixir/OTP
defmodule Frequency do
use GenServer
@moduledoc"""
Maintain a list of available radio frequencies and any associations between process identifiers and their
allocated frequencies.
"""
@initial_allocations []
@initial_frequencies [10, 11, 12, 13, 14, 15]
@schmalz
schmalz / coffee.ex
Created September 18, 2017 07:33
Designing for Scalability with Erlang/OTP - Ch 6 - Coffee FSM - Elixir/OTP
defmodule Coffee do
use GenStateMachine, callback_mode: :state_functions
# Client API
def start_link() do
GenStateMachine.start_link(__MODULE__, {}, [name: __MODULE__])
end
def stop() do
@schmalz
schmalz / phone_fsm.ex
Created September 21, 2017 11:09
Designing for Scalability with Erlang/OTP - Ch 6 - Phone FSM - Elixir/OTP
defmodule PhoneFSM do
@behaviour :gen_statem
@moduledoc """
A mobile 'phone controller.
"""
# Client API
def start_link(ms), do: :gen_statem.start_link(__MODULE__, ms, [])
@schmalz
schmalz / pushbutton.ex
Last active September 22, 2017 06:59
The Erlang `gen_statem` documentation example, in `:state_functions` callback mode.
defmodule Pushbutton do
@behaviour :gen_statem
@moduledoc """
A simple pushbutton FSM.
"""
# Client API
@doc """
@schmalz
schmalz / sup.ex
Created September 22, 2017 15:20
Designing for Scalability with Erlang/OTP - Ch 8 - Coffee Supervisor - Elixir
defmodule Sup do
def start(name, child_spec_list) do
Process.register(pid = spawn(__MODULE__, :init, [child_spec_list]), name)
{:ok, pid}
end
def stop(name), do: send(name, :stop)
def init(child_spec_list) do