Skip to content

Instantly share code, notes, and snippets.

View nathanl's full-sized avatar

Nathan Long nathanl

View GitHub Profile
@nathanl
nathanl / test_pathfinding.rb
Created October 23, 2014 19:55
Test pathfinding
# This can be run from the voltron directory while the NetSkycanner has a couple of `rake pathfinder` processes running
require 'bundler/setup'
Bundler.require :default, :pathfinder
require 'json'
require 'thump'
require 'mr_sulu_messages'
test = "searching"
@nathanl
nathanl / traveling_salesman_brute_force_time.rb
Created February 21, 2015 13:08
How long it takes to solve the Traveling Salesman problem by brute force
def factorial(n)
(1..n).reduce(:*)
end
SECONDS_PER_STEP = 0.001
def total_seconds(n)
factorial(n) * SECONDS_PER_STEP
end
@nathanl
nathanl / exponential_backoff.rb
Created February 25, 2015 14:41
Exponential backoff in Ruby
# Exponential backoff in Ruby
begin
make_request
rescue RequestError => e
if retries <= max_retries
retries += 1
sleep 2 ** retries
retry
else
raise "Timeout: #{e.message}"
@nathanl
nathanl / stupid_t.rb
Last active August 29, 2015 14:22
Quick and dirty way to spot non-localized strings in a Rails app page
# 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
@nathanl
nathanl / .vimrc
Created July 8, 2015 19:35
Flash current cursor position in Vim
" <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>
@nathanl
nathanl / activerecord_model_scopes.rb
Last active May 27, 2019 06:39
Rails code to use PostGIS in place of some of Geocoder's functionality - based on http://ngauthier.com/2013/08/postgis-and-rails-a-simple-approach.html
# 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
)
@nathanl
nathanl / experiment.md
Last active March 16, 2016 21:25
accessing data behind keys via function heads vs reading from a map

(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

@nathanl
nathanl / zalgo.ex
Created August 4, 2016 16:18
Zalgo text maker
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) ->
@nathanl
nathanl / postgresql_serializable_isolation.sql
Last active July 15, 2022 14:11
PostgreSQL Serializable Isolation - false positives
-- (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
);
@nathanl
nathanl / docker-bash
Created April 20, 2017 13:38
A few tiny, useful Docker scripts, to be placed in one's `$PATH` and made executable
#!/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