This file contains 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
(ns user) | |
(defn assoc-in | |
"Associates a value in a nested associative structure, where ks is a | |
sequence of keys and v is the new value and returns a new nested structure. | |
If any levels do not exist, hash-maps will be created." | |
[m [k & ks] v] | |
(if ks | |
(assoc m k (assoc-in (get m k) ks v)) | |
(assoc m k v))) |
This file contains 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
(cond | |
(instance? clojure.lang.IPeristentCollection form) (outer (into (empty form) (map inner form)) | |
(seq? form) (outer (doall (map inner form))) | |
:else (outer form)) |
This file contains 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
public static class PartialReader extends AFn{ | |
public Object invoke(Object reader, Object leftbracket) throws Exception{ | |
PushbackReader r = (PushbackReader) reader; | |
IPersistentList tempList = PersistentList.create(readDelimitedList(']', r, true)); | |
return tempList.cons(PARTIAL); | |
} | |
} |
This file contains 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
user=> (use 'clojure.template) | |
user=>(macroexpand-1 | |
'(do-template | |
[fn-name unique-form] | |
(defn fn-name [x y] | |
(do | |
unique-form | |
(+ x y))) | |
fn-x (println x) |
This file contains 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 take-last | |
"Mirrors drop-last. Works like tail, the classic *nix command." | |
[n coll] | |
(drop (- (count coll) n) coll)) | |
(defn take-until | |
"Returns a lazy sequence of successive items from coll while | |
(pred item) returns false. pred must be free of side-effects." | |
[pred coll] | |
(take-while (complement pred) coll)) |
This file contains 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
user=>(same take 2 "abcdef") | |
"ab" | |
user=>(same drop 2 "abcdef") | |
"cdef" | |
user=>(same map name [:a :b :c]) | |
["a" "b" "c"] | |
user=>(same filter (set "ab") {\a \b \c \d}) |
This file contains 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- same-dispatch [& args] | |
(class (last args))) | |
(defmulti | |
#^{:doc | |
"same is a mutlimethod that is designed to \"undo\" seq. | |
It expects a seq-fn that returns a normal seq, and the | |
appropraite args. It converts the resulting seq into the same | |
type as the last argument. If it is a sorted seq, the | |
comparator is preserved. |
This file contains 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
user=>|"This is test number ~(+ 2 2)" | |
"This is test number 4" |
This file contains 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
;;Code courtesy of Chas Emerick | |
(let [slurp* (fn [f] | |
(with-open [#^java.io.BufferedReader r (java.io.BufferedReader. f)] | |
(let [sb (StringBuilder.)] | |
(loop [c (.read r)] | |
(if (neg? c) | |
(str sb) | |
(do (.append sb (char c)) | |
(recur (.read r))))))))] | |
(defn silent-read |
This file contains 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
;Defined in terms of pennies to avoid float error | |
(def xkcd | |
(sorted-map-by | |
(comp - compare) | |
215 :fruit | |
275 :fries | |
335 :salad | |
355 :wings | |
420 :cheeze | |
580 :sampler)) |