Skip to content

Instantly share code, notes, and snippets.

View mccraigmccraig's full-sized avatar

mccraigmccraig of the clan mccraig mccraigmccraig

View GitHub Profile
@mccraigmccraig
mccraigmccraig / gist:128577
Created June 12, 2009 11:00
clojure/java : make java private data public
(def state-field (first (filter #(= "state" (.getName %)) (seq (.getDeclaredFields (java.lang.Class/forName "clojure.lang.Agent"))))))
(.setAccessible state-field true)
(def foo (agent 1))
(.set state-field foo 5)
user=> @foo
5
@mccraigmccraig
mccraigmccraig / gist:138776
Created July 1, 2009 13:27
ruby : array structure equality
class Array
class Val
attr_reader :i
def initialize(i)
@i = i
end
def eql?(other)
other.is_a?(Val) && @i==other.i
@mccraigmccraig
mccraigmccraig / gist:139453
Created July 2, 2009 13:01
ruby : dynamically bound values
# evil Ruby dynamic variables
class DynamicValue
attr_read :default_value
def initialize(val)
@default_value = val
end
def method_missing(name, *args)
@mccraigmccraig
mccraigmccraig / common_followers
Created July 3, 2009 12:23 — forked from DRMacIver/common_followers
ruby : find twitter common followers
#!/usr/bin/env ruby
# Trivial little twitter script
# Pass it a list of names as command line arguments
# and it will return a list of all people who follow everyone in the
# list.
# A person is implicitly assumed to follow themselves, so if foo follows
# bar and bar follows foo then common_followers foo bar would include
# both foo and bar in the output, but if bar did not follow foo it
# would include neither.
@mccraigmccraig
mccraigmccraig / gist:212493
Created October 18, 2009 00:16
clojure : early version of alex's puzzle solver
(defstruct board :size :values :value-index :all-transitions)
(defn create-board
"create a board of a given size"
[size]
(assert (> size 0))
(let [values (vec (range 1 (inc size)))
value-index (reduce (fn [h i] (assoc h (values i) i)) {} (range 0 (count values)))
all-transitions (reduce (fn [t n] (assoc t n (set values))) {} values)]
(struct-map board
@mccraigmccraig
mccraigmccraig / puzzle.clj
Created October 18, 2009 13:10
clojure : alex's puzzle solver
(ns puzzle)
(defstruct board :size :values :value-index :constraints)
(defn create-board
"create a board with given size and constraints"
[size constraints]
(assert (> size 0))
(let [values (vec (range 1 (inc size)))
value-index (reduce (fn [h i] (assoc h (values i) i)) {} (range 0 (count values)))
(defn multi-split-with
"Returns a lazy collection of lists which elements ..."
[pred coll]
(let [[head tail] (split-with pred coll)
tail-without-seps (drop-while (complement pred) tail)]
(lazy-seq
(if (seq tail-without-seps)
(cons head (multi-split-with pred tail-without-seps))
(list head)))))
@mccraigmccraig
mccraigmccraig / mail_decode_fail
Created October 21, 2010 15:24
mail fails to decode encoded addresses
>> puts m.to_s
Date: Thu, 21 Oct 2010 16:28:20 +0100
From: craig mcmillan <[email protected]>
To: =?UTF-8?B?ImNyYWlnIOaXpeacrOWbvSI=?= <[email protected]>
Message-ID: <[email protected]>
Subject: =?UTF-8?Q?big_in_=E6=97=A5=E6=9C=AC=E5=9B=BD?=
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8;
format=flowed
@mccraigmccraig
mccraigmccraig / gist:3093582
Created July 11, 2012 21:14
mark an emacs window as unavailable for other content
;; pin windows
(defun toggle-window-dedicated ()
"Toggle whether the current active window is dedicated or not"
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
(set-window-dedicated-p window
(not (window-dedicated-p window))))
"Window '%s' is dedicated"
"Window '%s' is normal")
{"status":200,
"data":
{"longitude":0.1537217,
"name":"AUTONOMY SYSTEMS LIMITED",
"postcode":"CB4 0WZ",
"last_revenue_year":2010,
"last_tweet":null,
"colocated":["03857576","03879840","04255000","07117553","03266657","04031841","06063523","05212691","04217293","06917673","03008226","03781249","04858580","03891934","02926171"],
"directors":[
{"name":"Mr Roberto Adriano Putland",