This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def async_query(pid, statement, params) do | |
message = {:query, statement, params} | |
process = GenServer.whereis(pid) | |
monitor = Process.monitor(process) | |
from = {self(), monitor} | |
:ok = Connection.cast(pid, {message, from}) | |
%Task{ref: monitor} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source is a named process on different node to the sink | |
* sink sends ask to {:source, :source_node} | |
* source adds sink to its list of sinks | |
* source exits | |
* source supervisor receives :EXIT from source(1) and restarts the source, source(2) | |
* sink sends ask to {:source, :source_node} | |
* source(2) adds sink_pid to its list of sinks | |
* sink receives :DOWN from source(1) and assumes :eos | |
* source(2) sends events to sink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule EventWatcher do | |
use Application | |
def start(_type, _args) do | |
import Supervisor.Spec, warn: false | |
children = [ | |
worker(GenEvent, [[name: EventWatcher.GenEvent]], [id: :event_manager]), | |
supervisor(EventWatcher.Watcher.Supervisor, [], [id: :watcher_supervisor]) | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SimpleSup do | |
@moduledoc """ | |
This file shows methods for starting a configurable number of children under | |
a `:simple_one_for_one` supervisor. | |
When the supervision tree is first started all methods behave the same, `size` | |
children are started and the `:starter` returns `:ignore`. However if the | |
restart limit for those children is reached the `:simple_one_for_one` | |
supervisor will be restarted and then the `:starter`. It is possible that the | |
`:simple_one_for_one` is restarted successfully but the `:starter` fails to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule KVServer do | |
use Application | |
def start(_type, _args) do | |
import Supervisor.Spec | |
children = [ | |
supervisor(Task.Supervisor, [[name: KVServer.TaskSupervisor]]), | |
worker(Task, [KVServer, :accept, [4040]]) | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BasicBench do | |
use Benchfella | |
@num :random.uniform(999_999_999_999) | |
bench "interpolation" do | |
@num | |
|> NumToWordsString.say | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def concurrent_factorial(range, worker_pool_by_nodenames \\ worker_pool_by_nodenames) | |
def concurrent_factorial(%Range{first: start, last: finish}, worker_pool_by_nodenames) do | |
pmap(split_range_of_numbers(start..finish), fn(range) -> range |> Enum.reduce(&(&1*&2)) end, worker_pool_by_nodenames) |> | |
Enum.reduce(&(&1*&2)) | |
end | |
def concurrent_factorial(n, worker_pool_by_nodenames) when is_integer(n) and n > 0 do | |
concurrent_factorial(1..n, worker_pool_by_nodenames) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try do | |
is_atom(atom) or :erlang.error(:badarg) | |
else | |
true -> | |
:atom | |
rescue | |
x in [ArgumentError] -> | |
:not_atom | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule EnterLoop do | |
use GenServer | |
require Logger | |
def start_link(opts) do | |
spawn_opts = Keyword.get(opts, :spawn_opt, []) | |
# :infinity start timeout | |
:proc_lib.start_link(__MODULE__, :init_it, [opts], :infinity, spawn_opts) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File.read("match.exs") |> (fn({:ok, bin}) -> bin end).() |> String.length | |
#=> 156 | |
s = """ | |
id;name;value | |
1;foo;hi | |
2;bar;bye | |
""" | |
s |
NewerOlder