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
# split into groups of maximum size BATCH_SIZE | |
groups = []; grp = nil | |
items.each_with_index do |item, idx| | |
groups << (grp = []) if idx % BATCH_SIZE == 0 | |
grp << item | |
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/env ruby | |
require 'rubygems' | |
require 'twitterstream' | |
module MassStream | |
CREDS = [ | |
{:username => 'xxx', :password => 'xxx'}, | |
] |
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 ShortId | |
# We cut out vowels to avoid shortened strings from mistakenly | |
# forming words | |
Alphabet = 'bcdfghjklmnpqrstvwxyz0123456789BCDFGHJKLMNPQRSTVWXYZ' | |
AlphabetLength = Alphabet.length | |
# Encode a numeric ID | |
def self.encode(id) | |
alpha = '' |
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 'active_record' | |
# Look through each table and look for indexes that are subsets | |
# of each other. | |
ActiveRecord::Base.establish_connection(:database => 'elfcast_development', :adapter => 'mysql') | |
ActiveRecord::Base.connection.tables.each do |table| | |
# go through each index | |
indexes = ActiveRecord::Base.connection.indexes(table) |
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
s = IceCube::Schedule.new(Time.now, :duration => 3600 * 7) | |
s.add_recurrence_rule IceCube::Rule.daily.day(:monday, :tuesday, :wednesday, :thursday, :friday).hour_of_day(9) | |
s.occurring_at?(Time.new(2011, 5, 30, 10, 0, 0)) # true, monday at 10am | |
s.occurring_at?(Time.new(2011, 5, 29, 10, 0, 0)) # false, sunday at 10am | |
s.occurring_at?(Time.new(2011, 5, 30, 8, 0, 0)) # false, monday at 8am |
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 'yajl' | |
require 'rest-client' | |
require 'zlib' | |
require 'msgpack' | |
require 'benchmark' | |
require 'colorize' | |
def compressed(d) | |
output = StringIO.new | |
gz = Zlib::GzipWriter.new(output) |
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
var settings = Object.create({ | |
_defaults: {}, | |
_values: {}, | |
setDefault: function (key, value) { | |
this._values[key] = this[key] | |
this._defaults[key] = value | |
Object.defineProperty(this, key, { | |
get: function () { | |
var undef = typeof this._values[key] === 'undefined' | |
return undef ? this._defaults[key] : this._values[key] |
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
module AtOnce | |
extend self | |
def map(objects, &block) | |
results = Array.new(objects.count) | |
threads = objects.map.with_index do |object, idx| | |
Thread.new do | |
results[idx] = block.call(object) | |
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
<?php | |
// implementation | |
function array_partition($arr, $callable) { | |
$r = array(array(), array()); | |
foreach ($arr as $e) { | |
$r[$callable($e) ? 0 : 1][] = $e; | |
} | |
return $r; |
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
// ported from: | |
http://stackoverflow.com/questions/2140787/select-random-k-elements-from-a-list-whose-elements-have-weights | |
// each node in the heap has a value, weight, and totalWeight | |
// the totalWeight is the weight of the node plus any children | |
var Node = {}; | |
var newNode = function (value, weight, totalWeight) { | |
var node = Object.create(Node); | |
node.value = value; | |
node.weight = weight; |
OlderNewer