Skip to content

Instantly share code, notes, and snippets.

(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)))
(cond
(instance? clojure.lang.IPeristentCollection form) (outer (into (empty form) (map inner form))
(seq? form) (outer (doall (map inner form)))
:else (outer form))
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);
}
}
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)
(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))
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})
(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.
user=>|"This is test number ~(+ 2 2)"
"This is test number 4"
;;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
;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))