Skip to content

Instantly share code, notes, and snippets.

(reduce (fn [mentioned-files file]
(if (some #{file} mentioned-files)
mentioned-files
(let [duplicates (duplicates-of-file file files (:precision options))]
(when (seq duplicates)
(printf "%s\n" file)
(doseq [d duplicates]
(printf "%s\n" d))
(printf "\n"))
(concat mentioned-files duplicates))))
def categorize(fn, coll):
"""Like group_by, but fn returns multiple keys"""
acc = {}
for e in coll:
for key in fn(e):
acc.setdefault(key, []).append(e)
return acc
def group_by(fn, l):
@amalloy
amalloy / parens.clj
Last active August 29, 2015 14:13 — forked from djtrack16/parens.clj
(fn [o]
(set
((fn q [s o c]
(apply concat
(let [a (when (pos? o)
(q (concat s "(") (dec o) (inc c)))
b (when (pos? c)
(q (concat s ")") o (dec c)))]
(if (and (zero? o) (zero? c))
[[(clojure.string/join s)]]
@amalloy
amalloy / gist:848084
Created February 28, 2011 21:36 — forked from ray1729/gist:848069
;; not sure whether this is an improvement but I don't really like letfn
(defn keep-only
[n bucket-fn s]
((fn my-filter
[s seen]
(lazy-seq
(when (seq s)
(let [e (first s)
b (bucket-fn e)
c (inc (get seen b 0))]
(use 'name.choi.joshua.fnparse)
(def tokenize (partial re-seq #"[\[\](){}]|[^\[\](){}]+"))
(defn initial-state
[input] {:remainder (tokenize input)
:brackets (list)})
@amalloy
amalloy / core.clj
Created March 23, 2011 18:21 — forked from Raynes/core.clj
(import java.awt.event.ActionListener)
(defn add-action [cmp fn]
(doto cmp
(.addActionListener (doto (proxy [ActionListener] [])
(update-proxy {"actionPerformed" fn})))))
(defn add-action [cmp fn]
(doto cmp
(.addActionListener (reify ActionListener
(defmacro when-lets [bindings & body]
(reduce (fn [acc tstform]
`(when-let ~tstform ~acc))
(cons 'do body)
(reverse (map vec (partition 2 bindings)))))
(when-lets [n (first sizes)
s (seq coll)]
(cons (take n coll)
(partitions (next sizes) (drop n coll)))))
(defmacro phase-fn
([argvec] identity)
([argvec subphase & left]
`(fn [session# ~@argvec]
(--> session#
~subphase
~@(when left
[`((phase-fn ~argvec ~@left))])))))
(defmacro defratios
"Defines two converter functions with num-expr being how many of a make up b.
For example, there are 100 centimeters in a meter. To tell the program to make
(centimeter->meter) and (meter->centimeter), use (defratios meter centimeters 100)"
[a-symb b-symb num-expr]
(and (every? #{clojure.lang.Symbol}
(map type [a-symb b-symb]))
(let [ab-symb (symbol (arr a-symb b-symb))
ba-symb (symbol (arr b-symb a-symb))
num (gensym "num-")]
(defn routes
"Calculates all the routes from [x y] to [0 0].
Routes move only down and left."
[x y]
((fn routes* [queue]
(lazy-seq
(loop [queue queue]
(when (pos? (count queue))
(let [[current-level [x y]] (peek queue)
queue-left (pop queue)