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 / 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 / flags-regexp.go
Created October 30, 2012 15:53
Flags for Go RegExp
package main
import "fmt"
import "regexp"
func main() {
str := "a\nb"
fmt.Println(str)
@stevedomin
stevedomin / mix.exs
Last active September 25, 2022 19:55
simple_dynamo_deploy mix file
defmodule SimpleDynamoDeploy.Mixfile do
use Mix.Project
def project do
[ app: :simple_dynamo_deploy,
version: "0.0.1",
build_per_environment: true,
dynamos: [SimpleDynamoDeploy.Dynamo],
compilers: [:elixir, :dynamo, :app],
deps: deps ]

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

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 / 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 / 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 / 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