Skip to content

Instantly share code, notes, and snippets.

@joshuawscott
joshuawscott / sqrt.exs
Last active December 20, 2015 14:59
Find a square root with elixir (or just call :math.sqrt/1)
defmodule Math do
def sqrt(x) do
refine x, x / 2.0, 1.0
end
def refine(target, _, attempt) when attempt * attempt == target do
attempt
end
@joshuawscott
joshuawscott / spec_helper.rb
Created August 1, 2013 14:08
checking for rspec typo
module RSpec
module Mocks
module AnyInstance
class Recorder
def should_recieve(*args)
raise NoMethodError.new "should_recieve: Did you mean 'should_receive' ?"
end
def should_not_recieve(*args)
raise NoMethodError.new "should_not_receive: Did you mean 'should_not_receive' ?"
end
@joshuawscott
joshuawscott / gist:5811659
Last active December 18, 2015 16:29
# npm publish (never having published)
Joshuas-MacBook-Pro:postgresql-node joshua$ npm publish
npm ERR! need auth auth and email required for publishing
npm ERR! need auth You need to authorize this machine using `npm adduser`
npm ERR! System Darwin 12.4.0
npm ERR! command "node" "/usr/local/bin/npm" "publish"
npm ERR! cwd /Users/joshua/dev/postgresql-node
npm ERR! node -v v0.10.9
npm ERR! npm -v 1.2.24
npm ERR! code ENEEDAUTH
@joshuawscott
joshuawscott / pg_all_types.sql
Created June 19, 2013 04:18
CREATE TABLE statement for PostgreSQL that has one of each column type. I use this for testing various interfaces into PostgreSQL; check that each of the data types are mapped correctly, etc.
CREATE TABLE test (
a_smallint SMALLINT,
a_integer INTEGER,
a_bigint BIGINT,
a_decimal DECIMAL(10,3),
a_numeric NUMERIC(10,3),
a_real REAL,
a_double_precision DOUBLE PRECISION,
a_serial SERIAL,
a_bigserial BIGSERIAL,
@joshuawscott
joshuawscott / bsearch_index.rb
Last active December 18, 2015 09:29
Optimized Binary Search for a sorted array - returns the index of the element. 6x faster than Array#bsearch for arrays of strings.
class Array
# Returns the index number of a matching element.
# The returned element is not guaranteed.
# target must implement #<=>
# example:
# arr = [2,3,5,7,11,13,17,19,23,29]
# arr.bsearch_index(2) => 0
# arr.bsearch_index(23) => 8
# arr.bsearch_index(10) => nil
# nil always is nil: