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 / 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 / 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
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
source_key: >
reallylongbase64encodedstring
splitovermultiplelinesforthes
akeofmyhealthandforvim==
@d11wtq
d11wtq / caching.txt
Created August 14, 2012 10:16
An awesome caching proxy should...
Imagine something like:
var http = require('http')
, cache = require('cache')
;
var options = {
ignoreCookies: true,
cacheStatics: 300 // 5 minutes, unless an explicit cache-control header is given
};
(define (each-fib fn)
(letrec
((next (lambda (a b)
(fn a)
(next b (+ a b)))))
(next 0 1)))
(define (take-n-fibs n)
(call/cc
(lambda (return)