Skip to content

Instantly share code, notes, and snippets.

def profile(label=nil, unit = :M)
# trying to see where our memory is going
population = Hash.new{|h,k| h[k] = [0,0]}
array_sizes = Hash.new{|h,k| h[k] = 0}
ObjectSpace.each_object do |object|
# rough estimates, see http://eigenclass.org/hiki.rb?ruby+space+overhead
size = case object
when Array
array_sizes[object.size / 10] += 1
case object.size
@MichaelXavier
MichaelXavier / shoes.js
Created February 1, 2011 04:44
with constructor
(function(global) {
function Shoes() {
this.version = '0.0.0';
this.release = 'Haste';
this.canvas = null;
this.context = null;
}
Shoes.prototype.app = function() {
@MichaelXavier
MichaelXavier / cmdargs_example.hs
Created February 7, 2011 02:11
Example program to help me remember the incredibly obtuse syntax of System.Console.CmdArgs for programs w/ subcommands
import Data.Typeable(Typeable)
import Data.Data (Data )
import qualified System.Console.CmdArgs as Arg
import System.Console.CmdArgs((+=),Annotate((:=)),(&=), def, opt, help)
data Mode
= English { name :: String }
| Spanish { name :: String }
deriving (Show, Typeable, Data)
@MichaelXavier
MichaelXavier / wtf.hs
Created February 12, 2011 05:45
This is why you don't parse JSON in Haskell
{-# LANGUAGE DeriveDataTypeable #-}
import Text.JSON
import Text.JSON.Generic
import Control.Monad (liftM2)
data Foo = Foo { name :: String } deriving (Eq, Show, Typeable, Data)
data FooSet = FooSet { foos :: [(String, Foo)]} deriving (Eq, Show)
-- Input data looks like
@MichaelXavier
MichaelXavier / Parse.hs
Created March 20, 2011 09:08
sample.json
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Control.Applicative
import Data.Aeson
import Data.Maybe
import Data.ByteString as BS (readFile, ByteString(..))
import Data.Attoparsec (parse, maybeResult, eitherResult)
main :: IO ()
main = do str <- BS.readFile "sample.json"
@MichaelXavier
MichaelXavier / trolld.rb
Created March 28, 2011 07:16
Messing with ATI color settings on a set interval
require 'rubygems'
require 'nokogiri'
XML_FILE = 'profiles.xml'
INTERVAL = 60
def randomize(hash)
%w[Contrast_Green Contrast_Red Contrast_Blue].each do |k|
hash[k] = rand(255)
end
@MichaelXavier
MichaelXavier / csv2json.hs
Created August 29, 2011 03:46
Converts CSV file with headers from STDIN into JSON STDOUT
module Main (main) where
{-# Language Overloaded Strings #-}
import Control.Exception.Base (SomeException)
import Data.Aeson (Object)
import Data.Aeson.Encode (encode)
import Data.Aeson.Types (Value(String))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS (putStr)
import Data.CSV.Enumerator
@MichaelXavier
MichaelXavier / hash_of_arrays_permutations.rb
Created September 13, 2011 17:10 — forked from dplummer/hash_of_arrays_permutations.rb
Permutations of a hash of arrays
# Input:
# choices = {
# 'color' => %w(blue red green),
# 'sizes' => %w(small medium large extra-large),
# 'style' => %w(tshirt longsleeve)
# }
#
# Output:
# [{"sizes"=>"small", "color"=>"blue", "style"=>"tshirt"},
# {"sizes"=>"small", "color"=>"blue", "style"=>"longsleeve"},
@MichaelXavier
MichaelXavier / buffer.rb
Created October 5, 2011 22:55
Simplest buffer I can think of
class Buffer
attr_reader :size, :contents
def initialize(size, &block)
@size = size
@contents = []
@callback = block
end
def <<(item)
@contents << item