Skip to content

Instantly share code, notes, and snippets.

Avatar

Renato Zannon renato-zannon

View GitHub Profile
View gist:1311185
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 Oct 15, 2011
Inject alternate implementation
View gist:1290010
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 Sep 20, 2011
A simple practice to help your fellow workers navigate your dynamic ruby code
View named_dynamic_methods.rb
# 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