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.exports = (options = {})-> | |
cache = {} | |
cacheSize = 0 | |
maxKeys = options.maxKeys || 50 | |
seq = 1 | |
get: (key)-> | |
if c = cache[key] | |
c.used = seq++ | |
return c.value |
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 'eventmachine' | |
require 'em-http' | |
EM::run do | |
rq = EM::HttpRequest.new("http://www.slow.com").get | |
def rq.timed_out? | |
@timed_out | |
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
module Enumerable | |
def longest_common_prefix | |
common = first | |
self[1..-1].each do |item| | |
length_to_test = [item.length, common.length].min | |
while length_to_test > 0 | |
if item[0..(length_to_test-1)] === common[0..(length_to_test-1)] | |
common = common[0..(length_to_test-1)] | |
break | |
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
#! /bin/bash | |
SIGNAL="SIGINT" | |
while killall -${SIGNAL} "$@"; do | |
if [ $SIGNAL = SIGINT ]; then | |
SIGNAL="SIGQUIT" | |
else | |
SIGNAL="SIGKILL" | |
fi |
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 Numeric | |
def round_to_nearest(delta = 0.25) | |
return self if delta.to_f == 0.0 | |
((to_f / delta.to_f).round * delta.to_f) | |
end | |
def floor_to_nearest(delta = 0.25) | |
return self if delta.to_f == 0.0 | |
((to_f / delta.to_f).floor * delta.to_f) | |
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
module Enumerable | |
def split_where(&block) | |
inject([[]]) do |result, item| | |
if block.call(item) | |
result.push [] | |
else | |
result.last.push item | |
end | |
result | |
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
# Efficient way of splitting digits in positive integers | |
class Integer | |
def split_digits(base = 10) | |
digits = [] | |
head = self | |
while head >= base | |
digits.push head % base | |
head /= base | |
end | |
digits.push(head) |