Skip to content

Instantly share code, notes, and snippets.

View renato-zannon's full-sized avatar

Renato Zannon renato-zannon

View GitHub Profile
import Control.Monad
data Tree a = EmptyTree | Node a (Tree a) (Tree a)
(<<) EmptyTree value = Node value EmptyTree EmptyTree
(<<) (Node value left right) newValue = if newValue < value
then Node value (left << newValue) right
else Node value left (right << newValue)
toArray EmptyTree = []
@renato-zannon
renato-zannon / gist:1290010
Created October 15, 2011 19:14
Inject alternate implementation
module Inject
class Injector
def self.inject(*args, &block)
options = args.last.kind_of?(Hash) ? args.pop : {}
target = options[:receiver] || block.binding.eval("self")
modules = args
modules.each { |mod| mod.before_inject(target) if mod.respond_to?(:before_inject)}
return_value = new(modules).inject_into(target, &block)
@renato-zannon
renato-zannon / named_dynamic_methods.rb
Created September 20, 2011 13:53
A simple practice to help your fellow workers navigate your dynamic ruby code
# It's common, while creating a bunch of very similar methods, to create
# a loop to define them all at once. (Very simple) example follows:
week_days = %w[sunday monday tuesday wednesday thursday friday saturday]
week_days.each do |day|
define_method("events_on_#{day}") do
events.select { |event| event.day == day }
end
end