Skip to content

Instantly share code, notes, and snippets.

(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 alternates
"Split coll into 'threads' subsequences (defaults to 2), feeding
each alternately from the input sequence. Effectively the inverse of
interleave:
(alternates 3 (range 9))
;=> ((0 3 6) (1 4 7) (2 5 8))"
([coll] (alternates 2 coll))
([threads coll]
(for [offset (range threads)]
(ns use-opts.core
(:use (clojopts core)))
(clojopts "clojopts-test"
["-t" "myfile"]
(no-arg time t "STOP TIME. USE WITH CAUTION"))
(let [plugins #{#_"dictionary" "lmgtfy" "google" "translate" "eball" "utils" "leet" "clojure" "login" "log"
"weather" "brainfuck" "whatis" "shorturl" "haskell"
"mail" "timer" "fortune" "rss" "title" "operator" "seen" "sed" "help"
"load" "embedded" "karma" "ping" "debug"}]
{:servers ["irc.freenode.net"] ; A list of servers.
:prepends #{":"} ; The character you want for a prepend. Currently set to @
:dont-sed #{"tomoj" "hiredman"}
:bitly-login "" ; Your bit.ly login.
:bitly-key "" ; API key and login above needed for URL shortening.
:wordnik-key "" ; API key needed for dictionary access.
@amalloy
amalloy / largest-size.clj
Created April 4, 2011 19:32
Gisted version of jsa's comment on What is Clojure
(defn largest-size [kfn l item]
(if (empty? l)
item
(if (nil? item)
(recur kfn (next l) (first l))
(let [f (first l)]
(recur kfn
(next l)
(if (> (kfn f) (kfn item)) f item))))))
(defn process-urls [callback chunk-size stream]
(let [mcurl (MCurl.)]
(letfn [(add-handle
[next-thunk]
(when-let [[url cbdata] (next-thunk)]
(.add mcurl (Curl. url inner-callback cbdata))))
(inner-callback
[info content data]
(add-handle #(or (apply callback args)
(stream))))])
(defn update
[m [& keys] f & args]
(reduce (fn [m k]
(update-in m [k] #(apply f % args)))
m keys))
(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)