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
import Swoosh.Email
upload_invoice = %Plug.Upload{
path: "/tmp/plug/DEFfF014AaA01F",
content_type: "",
filename: "invoice-peter-may.pdf"
}
new()
|> to("peter@example.com")
@stevedomin
stevedomin / simple_attachment.ex
Last active May 6, 2017 09:46
simple_attachment
import Swoosh.Email
new()
|> to("peter@example.com")
|> from({"Jarvis", "jarvis@example.com"})
|> subject("Invoice May")
|> text_body("Here is the invoice for your superhero services in May.")
|> attachment("/Users/jarvis/invoice-peter-may.pdf")
@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
@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 / 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 / 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 / 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 / create_post.exs
Last active July 12, 2023 01:32
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 / 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

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