Skip to content

Instantly share code, notes, and snippets.

José Valim josevalim

Block or report user

Report or block josevalim

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View rocket_date_time.ex
defmodule Rocket.DateTime do
def type, do: :datetime
# Provide our own casting rules.
def cast(string) when is_binary(string) do
# Try to parse different formats here
end
# Datetime itself should also be valid
def cast(%Ecto.DateTime{} = datetime) do
View split_stream.exs
"foo bar baz"
|> Stream.unfold(fn
"" -> nil;
acc ->
case String.split(acc, " ", parts: 2) do
[left, right] -> {left, right}
[left] -> {left, ""}
end
end)
|> Enum.take(2)
View exq_web_router.ex
defmodule Exq.RouterPlug do
import Plug.Conn
use Plug.Router
plug :match
plug :dispatch
get "/queues" do
IO.puts "YOLO"
conn |> halt()
View epmd
+ EPMD
- Distributed systems are cool
- Distributed systems are also complex
- First step: How to find other machines (and nodes) in the same network
- Erlang Port Mapper Daemon
+ EPMD in C
- Ships with Erlang
- is a TCP server
- usually 4369
View parsec.exs
defmodule Parsec do
# Handles parsec for ..., do: ...
defmacro parsec({:for, _, args}) do
{exprs, [[do: return]]} = Enum.split(args, -1)
do_parsec(exprs, return)
end
# Handles parsec for ... do ... end
defmacro parsec({:for, _, args}, do: return) do
do_parsec(args, return)
View shutdown.exs
iex(2)> import Supervisor.Spec
nil
iex(3)> Supervisor.start_link([worker(Task, [fn -> Process.register(self, :hello); :timer.sleep(:infinity) end])], strategy: :one_for_one)
{:ok, #PID<0.61.0>}
iex(4)> Process.whereis(:hello)
#PID<0.62.0>
iex(5)> Process.exit(self(), :shutdown)
** (EXIT from #PID<0.57.0>) shutdown
Interactive Elixir (0.15.2-dev) - press Ctrl+C to exit (type h() ENTER for help)
View gist:3e1817e2e9b5b282fdbf
iex(1)> defmodule Foo do
...(1)> use GenEvent
...(1)> end
{:module, Foo,
<<70, 79, 82, 49, 0, 0, 8, 144, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 2, 7, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 2, 104, 2, 100, 0, 4, ...>>,
[true, true, true, true, true, true]}
iex(2)> {:ok, pid} = GenEvent.start_link
{:ok, #PID<0.50.0>}
iex(3)> GenEvent.add_handler(pid, Foo, [])
:ok
View exercise.exs
@proto_version "1.0"
def process_options(opts) do
ret = []
if opts[:in], do: ret = ["-in"|ret]
case opts[:err] do
:out -> ret = ["-err", "out"|ret]
:err -> ret = ["-err", "err"|ret]
_ -> nil
View gist:f0d9025bb3f5a580aee6
=ERROR REPORT==== 12-Jun-2014::15:23:03 ===
** Task <0.38.0> terminating
** Reason for termination ==
** {badarg,[{erlang,hd,[[]],[]},
{'Elixir.Agent.Server',handle_call,3,
[{file,"lib/agent/server.ex"},{line,11}]},
{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,580}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]}
View supervisor.ex
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
You can’t perform that action at this time.