Skip to content

Instantly share code, notes, and snippets.

@tokland
tokland / gist:406204
Created May 19, 2010 11:29
Equivalent implementation in Python of a Haskell code:
#!/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
@tokland
tokland / external-command.rb
Created August 18, 2010 18:01 — forked from careo/external-command.rb
Capture stderr data on EventMachine.popen
require 'rubygems'
require 'eventmachine'
$stdout.sync = true
$stderr.sync = true
EM.run {
EM.add_periodic_timer(0.1) {
$stdout.write "stdout\n"
@tokland
tokland / codejam_2010_1b_file_fix_it.rb
Created March 15, 2011 20:34
Solution for codejam 2010_1b A. File Fix-it (Ruby)
#!/usr/bin/ruby
# http://code.google.com/codejam/contest/dashboard?c=635101#s=p0
#
# Author: tokland@gmail.com
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)
@tokland
tokland / adt.rb
Last active December 24, 2021 03:35
Simple Algebraic Data types for Ruby
#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:
#
@tokland
tokland / challenge8.rb
Created May 8, 2011 08:57
Solution to challenge 8 from "El País"
# 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
@tokland
tokland / problem11
Created May 27, 2011 11:16
Problem 11 El Pais
#!/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(:+)
# Usage:
# $loopc 3 echo 'hello world'
# hello world
# hello world
# hello world
#
function loopc {
LIMIT=$1
shift 1
@tokland
tokland / underscore_extensions.js
Created September 16, 2011 16:28
My extensions for underscore.js
_.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) {
@tokland
tokland / action_dispatch_extensions.rb
Created September 20, 2011 13:52
How to add locale scope to i18n_routing
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]
@tokland
tokland / ar_arel_wrapper.rb
Last active March 5, 2024 06:22
Simple wrapper over arel
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):