Skip to content

Instantly share code, notes, and snippets.

View elucid's full-sized avatar

Justin Giancola elucid

View GitHub Profile
View = {}
(function(){
var self = View;
View.init = function() {
$('[href=#evt-name]').click(this.revealEventName);
$('[href=#evt-when]').click(this.revealEventWhen);
},
View.revealEventName = function() {
(defun btrees-of-size (n)
(cond ((= n 1) (list 'leaf))
((= n 2) (list (cons 'leaf 'leaf)))
(t (loop for i from 1 to (- n 1)
append (loop for j in (btrees-of-size i)
append (loop for k in (btrees-of-size (- n i))
collect (cons j k)))))))
(defun gen-combos (items n callback &optional accum)
(cond ((= n 0) (funcall callback accum))
# original implementation by jcl; see http://pastie.org/777044
import time
start_time = time.time()
_eq_map_cache = {}
#
def get_eq_map(vals):
# Given a list of floating point values, returns a map from result values
# to an equation string returning that result.
global _eq_map_cache
# global cache removed
import time
start_time = time.time()
#
def get_eq_map(vals):
# Given a list of floating point values, returns a map from result values
# to an equation string returning that result.
if len(vals) == 1:
return {vals[0]: str(vals[0])}
def get_value_map(vals):
if len(vals) == 1:
return {vals[0]: str(vals[0])}
else:
eq_map = {}
for i in range(1, len(vals)):
for h, heq in get_value_map(vals[:i]).iteritems():
for t, teq in get_value_map(vals[i:]).iteritems():
eq_map[h + t] = "(" + heq + "+" + teq + ")"
eq_map[h - t] = "(" + heq + "-" + teq + ")"
@elucid
elucid / crack_parse.rb
Created February 2, 2011 17:19
Crack::XML attribute parsing
require 'rubygems'
require 'crack'
xml = <<EOX
<credits>
<credit currency="GBP">100</credit>
<credit currency="USD">150</credit>
</credits>
EOX
@elucid
elucid / gist:836628
Created February 21, 2011 03:35
port Merb's :identify routing option to Rails 3 router
# see actionpack/lib/action_dispatch/routing/route_set.rb
module ActionDispatch
module Routing
class RouteSet
class Generator
# modified to allow :identify option ala Merb routing
def opts # :nodoc:
parameterize = lambda do |name, value|
to_param = lambda do |val|
if options[:identify] and name == :id and not val.is_a?(String)
@elucid
elucid / migrate.rake
Created May 13, 2011 22:04
wrap transactions in a DDL transaction (if your database supports it)
class << ActiveRecord::Base
def ddl_transaction(&block)
if connection.supports_ddl_transactions?
transaction { block.call }
else
block.call
end
end
end
@elucid
elucid / async_upload.rb
Created November 21, 2011 21:35
Running multiple Goliath apps
require 'goliath'
require 'yajl'
# adapted from goliath/examples/async_upload.rb
class AsyncUpload < Goliath::API
use Goliath::Rack::Params # parse & merge query and body parameters
use Goliath::Rack::DefaultMimeType # cleanup accepted media types
use Goliath::Rack::Render, 'json' # auto-negotiate response format
def on_headers(env, headers)
#!/usr/bin/env ruby
# usage:
# $ tail -5000 sidekiq.log | ./parse_sidekiq_logs.rb
class Averager
def initialize
@count = 0
@total = 0.0
end