Skip to content

Instantly share code, notes, and snippets.

View tdg5's full-sized avatar

Danny Guinther tdg5

View GitHub Profile
@tdg5
tdg5 / partially_implemented_interface.rb
Created April 15, 2015 19:00
PartiallyImplementedInterface mixin
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
@tdg5
tdg5 / itr_pbt_deep_dup_build.rb
Last active August 29, 2015 14:17
iterative pbt_deep_dup_build
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)
@tdg5
tdg5 / tco_ivar.rb
Created March 21, 2015 16:59
Instance variable reference from tail recursive call
require "tco_method"
class TCOInstance
extend TCOMethod::Mixin
attr_reader :tco_cycles
def initialize
@tco_cycles = 0
end
@tdg5
tdg5 / itoa.rb
Last active August 29, 2015 14:17
itoa method
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)
@tdg5
tdg5 / factorials.rb
Created March 20, 2015 18:39
4 kinds of factorial
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
@tdg5
tdg5 / stack_size_introspection.rb
Last active August 29, 2015 14:17
PoC of detecting tail call optimization by monitoring stack size via a lambda callback
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:"
@tdg5
tdg5 / eagerly_losing_logic.rb
Created March 1, 2015 03:07
Errors during eager evaluation cause result of logical expression to be lost
or_result = nil
begin
or_result = true | Seriously(this(is(valid(Ruby!))))
rescue NameError
puts "NameError :("
end
# Name Error :(
or_result
# => nil
@tdg5
tdg5 / prime_finder.rb
Last active August 29, 2015 14:16
Sieve generated prime finder
class PrimeFinder
attr_reader :limit, :primes
def initialize(limit)
@limit = limit
@primes = []
generate_primes!(limit)
end
def prime?(candidate)
@tdg5
tdg5 / tagged_console.rb
Created February 25, 2015 18:16
alternative means of adding tagging to rails console comand
# Add to require from script/rails before rails/commands is required.
if /^c(?:onsole)?$/ === ARGV[0] && index = ARGV.index { |arg| /-(t|-tag)/ === arg }
# Intercept the arguments vector before it is passed to Rails::Console.
# Drop the -t or --tag arg and following argument which should be the tag
# itself. That's all that should need to be done because the tag will show up
# in the command name given how the command was invoked.
ARGV.slice!(index, 2)
end
@tdg5
tdg5 / pe_08_with_simple.rb
Created February 18, 2015 11:19
Project Euler #08 with simpler alternative
# Project Euler #8 - Largest product in a series
# https://projecteuler.net/problem=8
#
# Find the thirteen adjacent digits in the 1000-digit number that have the
# greatest product. What is the value of this product?
def largest_product_in_series(series, adjacency_length = 13)
factors = []
largest_product = 0
current_product = 1