Skip to content

Instantly share code, notes, and snippets.

@MichaelXavier
MichaelXavier / CFBFeatures.js
Created June 28, 2012 15:47 — forked from dplummer/CFBFeatures.js
Ajax from PHP
var count = 2;
var loader = $('.infinite-loader');
$(window).scroll(function() {
if($(window).scrollTop() == $(document).height() - $(window).height()) {
if(count > total) {
return false;
} else {
@MichaelXavier
MichaelXavier / deferred_touch.rb
Created May 24, 2012 23:41
Deferred ActiveRecord touching
module DeferredTouch
def self.no_touching!(*klasses, &block)
to_touch = Set.new
old_methods = klasses.inject({}) {|acc, klass| acc[klass] = klass.instance_method(:touch); acc }
begin
klasses.each do |klass|
klass.send(:define_method, :touch) do
to_touch << self
end
@MichaelXavier
MichaelXavier / gist:1355459
Created November 10, 2011 17:16
Alternative to included black magic
## Having:
module Foozle
module ClassMethods
# great, we can document the init stuff too now, isn't that nice?
def initialize_foozle
# do some init-stuff here
end
private :initialize_foozle
end
module InstanceMethods
@MichaelXavier
MichaelXavier / deepvalue.hs
Created October 15, 2011 07:36
Recursive operator for parsing values from deeply nested Objects in Aeson
(.:/) :: (FromJSON a) => Object
-> [Text]
-> Parser a
obj .:/ (k:ks) = maybe (fail msg) parseJSON parsed
where parsed = deepValue ks =<< M.lookup k obj
msg = "Failed to find " ++ (intercalate "/" $ map unpack (k:ks))
obj .:/ [] = parseJSON $ Object obj
deepValue :: [Text]
-> Value
@MichaelXavier
MichaelXavier / ArrToRanges.hs
Created October 11, 2011 19:45
Summarizes a list into a list of ranges
import Data.List (groupBy, sort)
toRanges :: (Ord a, Enum a) => [a] -> [(a,a)]
toRanges = map range . groupBy isSucc . sort
where range xs = (minimum xs, maximum xs)
isSucc a b = succ a == b
@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
@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 / 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 / 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 / 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"