Skip to content

Instantly share code, notes, and snippets.

View stevedomin's full-sized avatar
🛫
Taking off

Steve Domin stevedomin

🛫
Taking off
View GitHub Profile
@stevedomin
stevedomin / output
Created June 20, 2014 22:20
Dynamo - Elixir 0.14.1 - Failing specs
..............................................................................................
1) test forwarding on root with dynamic path and params (Dynamo.Router.BaseTest)
test/dynamo/router/base_test.exs:167
** (ArgumentError) argument error
stacktrace:
(stdlib) :maps.merge(%{}, [glob: ["12", "hello"]])
(dynamo) lib/dynamo/connection/behaviour.ex:133: Dynamo.Connection.Test.route_params/2
test/dynamo/router/base_test.exs:69: Dynamo.Router.BaseTest.RootSample.dispatch/3
(dynamo) lib/dynamo/http/case.ex:250: Dynamo.HTTP.Case.do_process/2
@stevedomin
stevedomin / not_working_user_controller.ex
Created October 4, 2014 17:55
Avoid encoding/decoding in controller create
def create(conn, %{"user" => u}) do
user = struct(User, u)
IO.inspect user
# %App.User{created_at: nil, email: nil}
end

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns                     on recent CPU
L2 cache reference ........................... 7 ns                     14x L1 cache
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns                     20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs 4X memory

@stevedomin
stevedomin / porcelain_spawn_receive.exs
Last active August 29, 2015 14:16
Porcelain spawn/receive
alias Porcelain.Process
alias Porcelain.Result
defmodule Cmd do
def run() do
args = ["b"]
opts = [
in: "a\nb\nc\nb\nb\nd\nb\ne",
out: {:send, self()},
err: {:send, self()} # eksperimental -> This is the only thing that changed
@stevedomin
stevedomin / create_post.exs
Last active December 26, 2024 09:19
Using UUIDs as primary key with Ecto
defmodule MyBlog.Repo.Migrations.CreatePost do
use Ecto.Migration
def change do
create table(:posts, primary_key: false) do
add :id, :uuid, primary_key: true
add :body, :string
add :word_count, :integer
timestamps
@stevedomin
stevedomin / ex_playground.conf
Created August 12, 2015 23:32
Elixir Playground upstart conf
description "ex_playground"
setuid ex_playground
setgid ex_playground
start on runlevel [2345]
stop on shutdown
respawn
respawn limit 10 20
@stevedomin
stevedomin / form.html.eex
Last active March 17, 2016 10:54
Phoenix example form
<%= form_for @changeset, @action, fn f -> %>
<div>
<%= label f, :name %>
<%= text_input f, :name %>
</div>
<div>
<%= submit "Submit" %>
</div>
<% end %>
@stevedomin
stevedomin / iex
Created September 7, 2015 21:29
Process.info/2 weirdness
iex(1)> pid = spawn fn -> end
#PID<0.61.0>
iex(2)> Process.info(pid, :status)
nil
iex(3)> Process.info(pid, :status)
nil
iex(4)>
@stevedomin
stevedomin / mybank.ex
Created January 15, 2016 21:05
How to test an API client in Elixir?
defmodule MyBank.Client do
defstruct client_id: nil, access_token: nil
def request(client, method, path, params \\ %{}, body \\ nil, headers \\ []) do
# prepare url, headers, etc.
case HTTPoison.request(method, url, body, headers) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, body}
# handle other cases ...
end
end
@stevedomin
stevedomin / empty.ex
Last active February 21, 2016 19:59
empty changeset with Ecto 2.0
#
# pre ecto 2.0
#
# web/models/user.ex
defmodule User do
schema "users" do
field :name
field :email
end