Skip to content

Instantly share code, notes, and snippets.

@Chouser
Chouser / demo-cps.js
Created August 4, 2011 20:53
Demonstrate CPS for array processing
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name cps-demo.js
// @formatting pretty_print
// ==/ClosureCompiler==
function forEach(coll, f) {
for(var i = 0; i < coll.length; ++i) {
f(coll[i]);
}
@Chouser
Chouser / repeat_string.clj
Created May 13, 2011 19:27
repeat a string n times, in a couple languages
(defn repeat-string [string num]
(apply str (repeat num string)))
@Chouser
Chouser / mapreduce.js
Created May 5, 2011 21:50
Making CouchDB map/reduce a little less nutty...
// This is the map function to give to CouchDB:
function(doc) {
function xemit(key, val) {
emit(key, {key: key, val: val});
}
// Your code goes here, working with the doc provided.
// Use xemit(key, value) instead of CouchDB's emit
// ...
@Chouser
Chouser / proxy-star.clj
Created March 15, 2011 21:22
Like clojure.core/proxy, but accepts a syntax like reify
(let [arrays '{objects "Ljava.lang.Object;",
ints I, longs J, floats F, doubles D, chars C,
shorts S, bytes B, booleans Z}]
(defn qualify-tag [tag]
(when tag
(let [cls (if-let [array (arrays tag)]
(clojure.lang.RT/classForName (str "[" array))
(resolve tag))]
(assert (class? cls))
(symbol (pr-str cls))))))
(defn stamped [vctr f & args]
(conj vctr (apply f (assoc (peek vctr) :stamp (java.util.Date.)) args)))
user=> (def v (atom []))
#<Var@3af7345b: #<Atom@61128f5a: []>>
-----
user=> (swap! v stamped assoc :name "Joe")
[{:name "Joe", :stamp #<Date Mon Feb 14 12:19:23 EST 2011>}]
-----
user=> (swap! v stamped assoc :name "Bob")
@Chouser
Chouser / seq_tree.clj
Created February 12, 2011 18:23
Very raw version of a function to turn a seq into a lazy tree
(defn seq-tree
"Takes a sequential collection of events that logically represents
a tree by each event being one of: enter-sub-tree event,
exit-sub-tree event, or node event. Returns a lazy sequence whose
first element is a sequence of sub-trees and whose remaining
elements are events that are not siblings or descendants of the
initial event. The given exit? function must return true for any
exit-sub-tree event. parent must be a function of two arguments:
the first is an event, the second a sequence of nodes or subtrees
that are children of the event. parent must return nil or false if
; The New Year in Snowflakes
; A Clojure doodle by Chouser
(import '(java.awt Color Graphics Frame RenderingHints))
(defonce draw-agent (agent nil))
(defonce halt (atom false))
(defmacro ui-thread [& body]
`(javax.swing.SwingUtilities/invokeAndWait (fn [] ~@body)))
@Chouser
Chouser / how-I-ran-maven.txt
Created December 10, 2010 15:09
intermittent clojure-maven-plugin compile failure
chouser@dev:~/myproj$ mvn -o -X compile > /tmp/chouser-dump.txt 2>&1
@Chouser
Chouser / gist:656138
Created October 31, 2010 04:41
new lazy quicksort
(defn sort-parts [work]
"Lazy, tail-recursive, incremental quicksort. Works against
and creates partitions based on the pivot, defined as 'work'."
(lazy-seq
(loop [[part & parts :as work] work] ;; #: Pull apart work
(when work
(if (coll? part) ;; #: Check partition?
(if (empty? part)
(recur parts)
(let [[pivot & xs] part ;; #: Grab pivot
@Chouser
Chouser / gist:654062
Created October 29, 2010 18:22
LinkedBlockingQueue
(import '(java.util.concurrent LinkedBlockingQueue TimeUnit))
(def q (LinkedBlockingQueue. 5))
(future (while true
(println (str "took " (.take q)))
(Thread/sleep 500))) ; simulate slow IO
(doseq [x (range 10)]
(println (str "offering " x))