Skip to content

Instantly share code, notes, and snippets.

View d11wtq's full-sized avatar

Chris Corbyn d11wtq

  • Melbourne, Australia
View GitHub Profile
@d11wtq
d11wtq / queries.md
Last active December 16, 2015 09:29
SQL vs ORM vs QLCs

Comparing the power and expressiveness of various database query systems

Simple select all.

SELECT * FROM users;
User.all
@d11wtq
d11wtq / functional_ruby.rb
Created March 25, 2013 02:16
Lists, and the concept of "head" and "tail", coupled with recursion are at the core of functional programming. This demonstrates how you can do it in Ruby, without ever writing a loop.
# foldl() is fundamental. With a fold, you can do everything else.
def foldl(list, acc, &fn)
if list == [] # base case, return the accumulator
acc
else
head, *tail = list
foldl(tail, fn.call(acc, head), &fn) #recurse on the remainder
end
end
%% @doc This implements a kitchen fridge that you can put things into
%% and take them out.
%%
%% It is stateful, through the use of recursion.
%% I opted for the sets module only because I wanted to try it.
%% Using the sets module has the side-effect that you can only store
%% one of each food item in the fridge.
-module(kitchen).
-export([new/0, store/2, take/2]).
@d11wtq
d11wtq / calc.erl
Last active December 15, 2015 07:08
Reverse Polish Notation Calculator in Erlang
%% @doc Reverse Polish Notation Calculator.
%%
%% Parses expressions like "1 2 3 + -" = -4
%%
%% This is an exercise in Learn You some Erlang for Great Good,
%% however I didn't read the text and just implemented it.
%%
%% I guess understanding stack-based parsing helps here.
-module(calc).
-export([rpn/1]).
@d11wtq
d11wtq / binary_tree.erl
Created March 21, 2013 12:35
Binary tree implementation in Erlang.
%% @doc A binary tree implementation in Erlang.
%% A binary tree stores keys and values.
-module(binary_tree).
-export([init/0, init/1, insert/3, lookup/2]).
-define(EMPTY_NODE, {node, 'empty'}).
%% @doc Initialize an empty binary tree node.
%% This is how the root of the tree should be established.
%%
@d11wtq
d11wtq / time.rb
Created March 20, 2013 03:18
Like Timecop, but simpler.
class Time
class << self
attr_accessor :mock_time
def now_with_mock_time
@mock_time || now_without_mock_time
end
alias_method_chain :now, :mock_time
class Sidekiq::Middleware::Server::GC
def call(worker, msg, queue)
yield
ensure
GC.start
end
end
@d11wtq
d11wtq / ansi_color_test.rb
Created October 28, 2012 23:03
ANSI Color Test Script
class AnsiColorTest
FG = 38
BG = 48
class << self
def label(n, type)
"\033[01;#{type};5;#{n}m %3s \033[0m" % n
end
def dump256
@d11wtq
d11wtq / enum.sql
Created October 26, 2012 10:07
Renaming an ENUM label in PostgreSQL
/*
Assuming you have an enum type like this.
You want to rename 'pending' to 'lodged'
*/
CREATE TYPE dispute_status AS ENUM('pending', 'resolved', 'open', 'cancelled');
BEGIN;
ALTER TYPE dispute_status ADD VALUE 'lodged';
UPDATE dispute SET status = 'lodged' WHERE status = 'pending';
module DataMapper
class Transaction
def link_with_master_slave(*things)
things = things.collect do |t|
case t
when DataMapper::Adapters::MasterSlaveAdapter
t.master
else
t
end