Skip to content

Instantly share code, notes, and snippets.

View remcopeereboom's full-sized avatar

Remco Peereboom remcopeereboom

View GitHub Profile
@remcopeereboom
remcopeereboom / flag_sort.rb
Created December 11, 2017 14:54
A solution to the Dutch flag problem.
def color(a, i)
a[i]
end
def swap(a, i, j)
a[i], a[j] = a[j], a[i]
end
def flag_sort(a)
# Indices (not colors):
@remcopeereboom
remcopeereboom / promise.rb
Created December 6, 2016 23:09
Roundabout way of implementing promises in ruby.
class Promise < BasicObject
def initialize(&computation)
@computation = computation
end
def __result__
if @computation
@result = @computation.call
@computation = nil # So we don't redo the calculation next time.
end
@remcopeereboom
remcopeereboom / streams.rb
Last active August 29, 2015 14:15
A naive ruby port of my lisp code for implementing streams.
class EvenStream
attr_reader :value
def initialize(n = 0)
@value = n
@next = lambda { EvenStream.new(n + 2) }
end
def next
@next.call
@remcopeereboom
remcopeereboom / factorial.rb
Created February 20, 2015 14:40
Enabling tail recursion in ruby
require_relative 'tail_call_optimization' # All files loaded AFTER this file with have tail-recursion enabled.
require_relative 'tail_factorial'