View example_output.txt
10 requests with no threads
response codes: ["200", "200", "200", "200", "200", "200", "200", "200", "200", "200"]
that took 1.787615 seconds
10 requests with threads
response codes: ["200", "200", "200", "200", "200", "200", "200", "200", "200", "200"]
that took 0.200502 seconds
View copy_production_heroku_data_to_local_db
#!/bin/sh
set -e
HEROKU_APP_NAME=someapp
DEV_DB_NAME=someapp_dev
LOCAL_BACKUP_FOLDER=tmp
LOCAL_BACKUP_LOCATION=$LOCAL_BACKUP_FOLDER/$HEROKU_APP_NAME-production-dump-$(date +"%Y-%m-%dT%H:%M")
echo
read -p "Make and download a fresh backup of production? [y/n]" -n 1 -r
View periodic_task.ex
defmodule MyApp.Periodically do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, %{})
end
def init(state) do
Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours
{:ok, state}
View docker-bash
#!/bin/bash
# Shell into the Docker container with the given name.
# eg: `docker-bash my_app`
# Note: fails if more than one id is returned.
ID=$(docker-id $1)
docker exec -it $ID bash
View postgresql_serializable_isolation.sql
-- (This code was run in PostgreSQL 9.6.1)
-- Demonstration of how serializable isolation for PostgreSQL, which detects possible
-- interference between concurrent transactions, can produce false positives
-- in psql, create the following table
CREATE TABLE users(
id SERIAL NOT NULL PRIMARY KEY,
username VARCHAR NOT NULL
);
View zalgo.ex
defmodule Zalgo do
def this(string) do
String.graphemes(string)
|> Enum.map(fn (char) -> char <> the_funk end)
|> Enum.join
end
def the_funk do
accent_codes
|> Enum.map(fn (hex) ->
View experiment.md

(Update - thanks to Chris for looking at this and not saying I'm crazy. :))

"Metaprogramming Elixir" (Chris McCord) talks about how String.Unicode reads a text file of unicode characters at compile time and defines a separate function head for each character we might want to upcase. It says this leans on the Erlang VM's pattern matching prowess and implies (I think) that it's more performant than it would be to create a lower -> upper map at compile time and consult it at runtime.

Similarly, McCord in advocates this approach for some example code that looks up I18n keys.

By generating function heads for each translation mapping, we again let the Virtual Machine take over for fast lookup.

Although defining multiple function heads is idiomatic Elixir, this seemed odd to me. I've heard that the Erlang VM is really fast at pattern matching and hence at finding the right function f

View activerecord_model_scopes.rb
# Scopes that make use of the index
def self.within_distance_of(lat:, lng:, meters:)
where(%{
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || #{table_name}.longitude || ' ' || #{table_name}.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
View .vimrc
" <cr> should not only clear highlighted search, but flash the current
" cursor location.
:nnoremap <CR> :nohlsearch<CR>:set cul cuc<cr>:sleep 50m<cr>:set nocul nocuc<cr>/<BS>
View stupid_t.rb
# config/initializers/stupid_t.rb
# Any text that doesn't appear as "localized" isn't localized
module ActionView::Helpers::TranslationHelper
def t(*args)
result = I18n.t!(*args)
if result.respond_to?(:map)
result.map { |r| "localized" }
else
"localized"
end