This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @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]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @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]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sidekiq::Middleware::Server::GC | |
def call(worker, msg, queue) | |
yield | |
ensure | |
GC.start | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source_key: > | |
reallylongbase64encodedstring | |
splitovermultiplelinesforthes | |
akeofmyhealthandforvim== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |