Skip to content

Instantly share code, notes, and snippets.

View josevalim's full-sized avatar

José Valim josevalim

View GitHub Profile
@josevalim
josevalim / supervisor.ex
Created June 8, 2014 14:21
Public tables
defmodule Todo.Supervisor do
use Supervisor
def start_link do
ets = :ets.new(@manager,
[:set, :public, :named_table, {:read_concurrency, true}])
Supervisor.start_link(__MODULE__, ets, name: Todo.Supervisor)
end
@manager Todo.Manager
@josevalim
josevalim / server.ex
Created June 8, 2014 14:05
Table inheritance with supervisors
defmodule Todo.Manager do
use GenServer
## Client API
@doc """
Starts the `Todo.Manager`.
"""
def start_link(table, sup, event, opts \\ []) do
{:ok, pid} = GenServer.start_link(__MODULE__, {table, sup, event}, opts)
defmodule Rotor do
@doc """
Starts a new rotor as part of a supervision tree.
## Options
* `:name` - the registered name of the rotor
"""
def start_link(paths, opts) do
# config/config.exs
use Mix.Config
config :lager,
handlers: [
lager_console_backend: [:info, {:lager_default_formatter, [:time, ' [', :severity, '] ', :message, '\n']}]
],
crash_log: :undefined,
error_logger_hwm: 150
defmodule FibAgent do
def start_link do
cache = Enum.into([{0, 0}, {1, 1}], HashDict.new)
Agent.start_link(fn -> cache end)
end
def fib(pid, n) when n >= 0 do
Agent.get_and_update(pid, &do_fib(&1, n))
end
defmodule Foo do
@menu [foo: 1, bar: 3, baz: 2]
@filtered_menu @menu |> Enum.filter(...)
def filtered_menu do
@filtered_menu
end
end
diff --git a/lib/kernel/src/user_drv.erl b/lib/kernel/src/user_drv.erl
index 7b4ffb0..745fe46 100644
--- a/lib/kernel/src/user_drv.erl
+++ b/lib/kernel/src/user_drv.erl
@@ -172,17 +172,17 @@ server_loop(Iport, Oport, Curr, User, Gr) ->
{Iport,eof} ->
Curr ! {self(),eof},
server_loop(Iport, Oport, Curr, User, Gr);
- {User,Req} -> % never block from user!
- io_request(Req, Iport, Oport),
defmodule Poolboy.Mixfile do
use Mix.Project
@version File.read!("VERSION") |> String.strip
def project do
[app: :poolboy,
version: @version,
description: "A hunky Erlang worker pool factory",
package: package]

Before

A map pattern will match any map that has the given keys and values. For example, %{"hello" => world} will match any map that has the key "hello". An empty map therefore matches all maps.

After

A map pattern will match any map that has all the keys specified in the pattern. The values for the matching keys must also match. For example, %{"hello" => world} will match any map that has the key "hello" and assign the value to world, while %{"hello" => "world"} will match any map that has the key "hello"with value equals to"world". An empty map pattern (%{}`) will match all maps.

diff --git a/lib/task/sup.ex b/lib/task/sup.ex
index 3070aa2..4cc4968 100644
--- a/lib/task/sup.ex
+++ b/lib/task/sup.ex
@@ -66,10 +66,14 @@ defmodule Task.Sup do
"""
@spec async(Supervisor.supervisor, module, atom, [term]) :: Task.t
def async(supervisor, module, fun, args) do
- { :ok, pid } = Supervisor.start_child(supervisor, [self(), { module, fun, args }])
+ lock = make_ref()