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
require 'tco_method' | |
module StackBuster | |
def self.fact_yielder(n, acc = 1, &block) | |
block.call(acc) | |
n <= 1 ? acc : fact_yielder(n - 1, n * acc, &block) | |
end | |
end | |
puts "StackBuster:" |
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
require "benchmark/ips" | |
def recursive_factorial(n) | |
n < 2 ? 1 : n * recursive_factorial(n - 1) | |
end | |
def tail_recursive_factorial(n, accumulator = 1) | |
n < 2 ? accumulator : tail_recursive_factorial(n - 1, n * accumulator) | |
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
def itoa(int, base = 10) | |
str = "" | |
while int > 0 | |
str = ((int % base) + 48).chr + str | |
int /= base | |
end | |
str | |
end | |
def itoarr(int, base = 10) |
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
require "tco_method" | |
class TCOInstance | |
extend TCOMethod::Mixin | |
attr_reader :tco_cycles | |
def initialize | |
@tco_cycles = 0 | |
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
def self.pbt_deep_dup_build(clone, original) | |
mapped = Set.new | |
edges = [[clone, original]] | |
while edges.any? | |
builder, twin = edges.shift | |
pair_hash = "#{builder.class.name}:#{builder.hash}~#{twin.hash}".hash | |
next if mapped.include?(pair_hash) | |
mapped << pair_hash | |
pbt_dup_build(builder, twin) |
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 PartiallyImplementedInterface | |
def self.not_implemented_error(klass, method) | |
raise NotImplementedError, not_implemented_message(klass, method), caller(2) | |
end | |
def self.not_implemented_message(klass, method_name) | |
"#{method_name} must be implemented by subclasses of #{klass.name}!" | |
end | |
private_class_method :not_implemented_message |
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
%w[ | |
after_read | |
before_eval | |
after_eval | |
when_started | |
before_session | |
after_session | |
].each do |hook| | |
Pry.config.hooks.add_hook(hook.to_sym, "my_#{hook}_hook") do |_, _, _| | |
puts "hello from #{hook}" |
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
# Abstract UnionList base class | |
class AbstractUnionList | |
private :initialize | |
def length | |
how_many?(0) | |
end | |
def how_many?(counter) | |
raise NotImplementedError |
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
# Create some special classes to facilitate tracking allocated objects. | |
class TrackedArray < Array; end | |
class TrackedString < String; end | |
STRANG = "a" * 5000 | |
# Other than a few extra escapes, the code below can be understood more or less | |
# as is. Any weirdness is to facilitate interpretting with/without tail-call | |
# optimization in a DRY manner. | |
KLASS = <<-CODE | |
class GcFriendlyTco |
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
# Create some special classes to facilitate tracking allocated objects. | |
class TrackedArray < Array; end | |
class TrackedString < String; end | |
STRANG = "a" * 5000 | |
class ClingyObjects | |
def generate(should_cling = false) | |
strs = TrackedArray.new | |
30000.times { strs << TrackedString.new(STRANG) } |