This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==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]); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn repeat-string [string num] | |
| (apply str (repeat num string))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| // ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; 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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| chouser@dev:~/myproj$ mvn -o -X compile > /tmp/chouser-dump.txt 2>&1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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)) |