This file contains hidden or 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
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): |
This file contains hidden or 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 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 |
This file contains hidden or 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 EvenStream | |
attr_reader :value | |
def initialize(n = 0) | |
@value = n | |
@next = lambda { EvenStream.new(n + 2) } | |
end | |
def next | |
@next.call |
This file contains hidden or 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
require_relative 'tail_call_optimization' # All files loaded AFTER this file with have tail-recursion enabled. | |
require_relative 'tail_factorial' |