Skip to content

Instantly share code, notes, and snippets.

@schmalz
schmalz / orc-battle.lisp
Created January 10, 2018 09:34
Land of Lisp: Orc Battle
(defparameter *player-health* NIL)
(defparameter *player-agility* NIL)
(defparameter *player-strength* NIL)
(defparameter *monsters* NIL)
(defparameter *monster-builders* NIL)
(defparameter *monster-count* 12)
(defun randval (n)
"A random number between one and `N`. The number returned is guaranteed to be greater than or equal to one."
@schmalz
schmalz / graph-util.lisp
Last active January 4, 2018 11:42
Land of Lisp: Grand Theft Wumpus
(defparameter *max-label-length* 30)
(defun dot-label (exp)
"Produce a DOT label from a Lisp `exp`."
(if exp
(let ((s (write-to-string exp :pretty nil)))
(if (> (length s) *max-label-length*)
(concatenate 'string (subseq s 0 (- *max-label-length* 3)) "...")
s))
""))
@schmalz
schmalz / cache.ex
Last active October 6, 2017 13:38
Elixir in Action - Chapter 7
defmodule Todo.Cache do
use GenServer
@moduledoc """
A todo-list cache; maps todo-list names to their corresponding server PIDs.
"""
# 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
@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 / 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 / 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 / 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 / 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 / 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])