Skip to content

Instantly share code, notes, and snippets.

@heartpunk
heartpunk / gist:932934
Created April 20, 2011 21:37
my beautiful, beautiful, precious join.
select distinct t1.foo, t3.bar
from t1
join t1t2 on t1.id = t1t2.id1
left outer join t2 on t1t2.id2 = t2.id and (t2.expires > curdate() or t2.baz = 'qux')
join t2t3 on t2.id = t2t3.id2
left outer join t3 on t2t3.id3 = t3.id and t3.quux = 'garply' and t3.waldo = 'fred'
where
t1.plugh = 'xyzzy'
limit 100;
@heartpunk
heartpunk / gist:965288
Created May 10, 2011 20:16
javascript to find the element with respect to which the current element is positioned.
elementYouCareAbout.ancestors().detect(function(element) {pos = window.getComputedStyle(element).position;return ((pos == 'relative') || (pos == 'absolute'))})
@heartpunk
heartpunk / primes.rb
Created May 20, 2011 01:15
simple prime number generator in ruby
def primes i
(3..i).to_a.each do |number_to_test|
primes << number_to_test if primes.take_while do |prime|
prime < Math.sqrt(n)}.none? {|p| n % prime == 0}}
end
end
end
@heartpunk
heartpunk / simple_trace.rb
Created June 2, 2011 18:52
simple tracing mechanism for ruby.
def trace
def warn_or_log msg
if defined? Rails
logger.debug msg
else
warn msg
end
end
def debug
@heartpunk
heartpunk / newline_separated_to_set.rb
Created June 4, 2011 22:26
make a set from a newline terminated file
require 'set'
def newline_separated_file_to_set name
things = Set.new
File.open name do |f|
f.lines.each do |things|
things.add things.strip
end
end
things
end
@heartpunk
heartpunk / field_printer.hs
Created January 3, 2012 04:18
Why is this so slow?
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Enumerator as E
import qualified Data.Enumerator.Internal as EI
import qualified Data.Enumerator.Binary as ET
import qualified Data.Enumerator.List as EL
import qualified Data.ByteString.Char8 as T
import Data.Char
import Data.Functor
import Control.Monad
import qualified Data.Text.Lazy as TL
{-# LANGUAGE OverloadedStrings #-}
import System.IO
import Control.Pipe
import Control.Monad
import Control.Monad.Trans.Class
import qualified Data.Text as S
import qualified Data.Text.IO as I
readFile' :: Handle -> Producer S.Text IO ()
readFile' h = do
@heartpunk
heartpunk / benchmark.js
Created October 12, 2012 21:45
benchmark.js
var http = require('http');
var fs = require('fs');
var requests = 1;
var concurrency = process.argv[4];
var max_retries = 2;
var host = process.argv[2],
port = process.argv[3];
@heartpunk
heartpunk / gist:4009399
Created November 4, 2012 00:06
example of no fee credit in balanced
1.9.2p180 :202 > Balanced::Marketplace.my_marketplace.credits[1].attributes
+--------------------+---------------------------------------------------------------------------------+
| 0 | 1 |
+--------------------+---------------------------------------------------------------------------------+
| account | #<Balanced::Account:0x007f8aaf049390> |
| fee | 0 |
| description | None |
| amount | 100 |
| created_at | 2012-11-03T23:45:25.948590Z |
| destination | #<Balanced::BankAccount:0x007f8aaf03e968> |
@heartpunk
heartpunk / source_maps.rb
Created December 10, 2012 07:27 — forked from alexspeller/source_maps.rb
Coffeescript Source Maps in Rails
# config/initializers/source_maps.rb
if Rails.env.development?
require 'open3'
module CoffeeScript
class SourceMapError < StandardError; end;
class << self
def map_dir