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 'set' | |
require 'rspec' | |
### An example of how to implement a variant pattern in Ruby | |
class Variant | |
InvalidLabel = Class.new(StandardError) | |
UnhandledMatchCase = Class.new(StandardError) | |
UnnecessaryDefaultCase = Class.new(StandardError) | |
ExtraMatchCase = Class.new(StandardError) |
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; |
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
var url = 'https://lh6.googleusercontent.com/-NQAMokukfdE/AAAAAAAAAAI/AAAAAAAAAAA/zSaLZajdgEI/s48-c-k-no/photo.jpg'; | |
var xoffset = 240; | |
var yoffset = 210; | |
var pixelSize = 7; | |
var viewportWidth = 800; | |
var viewportHeight = 670; | |
// | |
// And away we go | |
// |
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 | |
raise 'usage: globber \'views/**/*.php\' \'echo {}\'' unless ARGV.length == 2 | |
Dir.glob(ARGV[0]).each { |f| system(ARGV[1].gsub('{}', f)) } |
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
// A quick runner for doing operations (either synchronous or asynchronous), | |
// in series | |
var simpleSequence = function (sequenceCallback) { | |
// Collect the operations | |
var operations = []; | |
sequenceCallback(function (operation) { | |
operations.push(operation); | |
}); | |
// Do the operations | |
(function perform() { |
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
// Grab a timebomb copy of a function that will expire in a certain time | |
// period - raising errors when called outside of that window | |
var timebomb = function (toCall, expireIn) { | |
// Expire the function call in expireIn ms | |
setTimeout(function () { | |
toCall = undefined; | |
}, expireIn); | |
// Return a wrapper function that will clear the timeout, and |
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
(function($){ | |
/* | |
* This is a jQuery extension to make it so that when you drag | |
* a fixed-position div's handle, you can resize the div. | |
* | |
* options.handleSelector (default='.handle') - how to find the handle | |
* options.direction (default='up') - the expansion direction | |
* options.min (default=0) - the min height (or width) | |
* options.max (default=clientHeight) - the max height (or width) |
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
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 |
NewerOlder