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
#!/usr/bin/python | |
# | |
# See http://stackoverflow.com/questions/1016997/generate-from-generators | |
# | |
# Equivalent implementation in Python of this Haskell code: | |
# | |
# grandKids generation kidsFunc val = | |
# iterate (concatMap kidsFunc) [val] !! generation | |
from itertools import chain, islice, imap |
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 'rubygems' | |
require 'eventmachine' | |
$stdout.sync = true | |
$stderr.sync = true | |
EM.run { | |
EM.add_periodic_timer(0.1) { | |
$stdout.write "stdout\n" |
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
#!/usr/bin/ruby | |
# http://code.google.com/codejam/contest/dashboard?c=635101#s=p0 | |
# | |
# Author: [email protected] | |
class Problem | |
# Create a directory (as array of slugs) in an existing filesystem and | |
# return the final filesytem and the total creation cost. | |
def self.create_directory(filesystem, slugs) | |
if (head = slugs.first) |
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
#https://gist.github.com/tokland/871431 | |
module Adt | |
# Build a ADT parent class with its children constructors. | |
# | |
# class_names_with_constructors -- Hash of key=class_name, value=constructor_arguments | |
# class_body -- Body to add to the parent class | |
# | |
# Example: | |
# |
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
# http://www.elpais.com/videos/sociedad/cubo/suma/cero/elpepusoc/20110505elpepusoc_2/Ves/ | |
faces_vertices = [[0,1,2,3], [0,1,4,5], [4,5,6,7], [2,3,6,7], [0,3,4,7], [1,2,5,6]] | |
vertices_solution = ([+1, -1].repeated_permutation(8)).select do |vertices_values| | |
faces_values = faces_vertices.map do |vertices| | |
vertices_values.values_at(*vertices).inject(:*) | |
end | |
(vertices_values + faces_values).inject(:+) == 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
#!/usr/bin/ruby-1.9 | |
require 'set' | |
class Problem11 | |
def self.solve(boxes, nails_per_box, weights) | |
(1..nails_per_box).to_a.repeated_combination(boxes).select do |combination| | |
weights.repeated_permutation(boxes).map do |weights| | |
weights.zip(combination).map do |weight, value| | |
weight * value | |
end.inject(:+) |
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
# Usage: | |
# $loopc 3 echo 'hello world' | |
# hello world | |
# hello world | |
# hello world | |
# | |
function loopc { | |
LIMIT=$1 | |
shift 1 |
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
_.mixin({ | |
/* Like extend but get key/values object->key_names */ | |
extend_from: function(object, source_object, key_names) { | |
return _(object).extend(_(source_object).slice(key_names)); | |
}, | |
/* Return a new object with the merged properties of all objects in arguments */ | |
merge: function() { | |
var objects = arguments; | |
return _.reduce(_.rest(objects), function(obj, o) { |
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
class ActionDispatch::Routing::Mapper | |
def localize_and_scope_for(locales, options = {}, &block) | |
scoped_locales = locales - Array(options[:skip_scope]) | |
localized(locales) do | |
locale_regexp = Regexp.new(scoped_locales.join('|')) | |
scope("/:i18n_locale", :constraints => {:i18n_locale => locale_regexp}) do | |
yield | |
end | |
yield if options[:skip_scope] |
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 'active_record' | |
require 'arel' | |
# Ruby-like syntax in AR conditions using the underlying Arel layer (Rails >= 3.0). | |
# | |
# What you would usually write like this: | |
# | |
# User.where(["users.created_at > ? AND users.name LIKE ?", Date.yesterday, "Mary"]) | |
# | |
# can now be written like this (note those parentheses required by the operators precedences): |
OlderNewer