Skip to content

Instantly share code, notes, and snippets.

View bryanwoods's full-sized avatar
💭
Replacing Security Deposits

Bryan Woods bryanwoods

💭
Replacing Security Deposits
View GitHub Profile
@bryanwoods
bryanwoods / scratch.rb
Created October 17, 2013 14:24
Seems unfortunate
def foo
puts "Foo!"
def bar
puts "Bar!"
end
end
begin
bar
def have_fun_refactoring_me
success = true
if giant_chunk_of_code_fails_somewhere?
success = false
end
rescue Exception => e
success
end
-- If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. --
-- The sum of these multiples is 23. --
-- Find the sum of all the multiples of 3 or 5 below 1000. --
allNums :: Int -> [Int]
allNums max = [1..max]
divisible :: Int -> Int -> Bool
divisible x y = y `mod` x == 0
puts (1..999).select { |i| (i % 3).zero? || (i % 5).zero? }.reduce(:+)
class LCVariable < Struct.new(:name)
def replace(name, replacement)
if self.name == name
replacement
else
self
end
end
def callable?
require 'benchmark'
def no_rescue
1 + 1
end
def rescued
1 + 1
rescue Exception
end
class LCVariable < Struct.new(:name)
def to_s
name.to_s
end
def inspect
to_s
end
end
@bryanwoods
bryanwoods / compact_flat_map.rb
Last active December 18, 2015 18:29
Enumerable#compact_flat_map
module Enumerable
def compact_flat_map(&block)
return to_enum(:compact_flat_map) unless block_given?
compact.flat_map(&block)
end
alias_method :compact_collect_concat, :compact_flat_map
end
p [1, 2, nil, 4, 5, nil].compact_flat_map { |i| [i + 1, i - 1] }
@bryanwoods
bryanwoods / automata.rb
Created June 17, 2013 17:44
Deterministic Finite Automata
class FARule < Struct.new(:state, :character, :next_state)
def applies_to?(state, character)
self.state == state && self.character == character
end
def follow
next_state
end
def inspect
@bryanwoods
bryanwoods / run.rb
Last active December 18, 2015 07:39
require 'treetop'
Treetop.load('simple')
parse_tree = SimpleParser.new.parse('while (x < 5) { x = x * 3 }')
statement = parse_tree.to_ast
puts "THE RESULT: #{statement.evaluate({ x: Number.new(1) })}"
puts
puts "TRANSLATED TO RUBY: #{statement.to_ruby}"