Email or username:

Password:

Forgot your password?
Niki Tonsky

Useful Clojure functions that fit in toot

(defn zip [& xs]
(apply map vector xs))

(defn now []
(System/currentTimeMillis))

(defn between? [x from to]
(and
(<= from x)
(< x to)))

(defn index-of [x xs]
(loop [i 0 xs xs]
(cond
(nil? xs) nil
(= (first xs) x) i
:else (recur (inc i) (next xs)))))

(defn index-by [pred xs]
(loop [i 0 xs xs]
(cond
(nil? xs) nil
(pred (first xs)) i
:else (recur (inc i) (next xs)))))

3 comments
Niki Tonsky

Better cond:

(defmacro cond+ [& clauses]
(when-some [[test expr & rest] clauses]
(condp = test
:do `(do ~expr (cond+ ~@rest))
:let `(let ~expr (cond+ ~@rest))
:some `(or ~expr (cond+ ~@rest))
`(if ~test ~expr (cond+ ~@rest)))))

Use it like this:

(cond+
(= a 1) true
:let [b (- a)]
(= b 1) false
:do (println (+ a b))
:else nil))

Steven Deobald

@nikitonsky maybe somebody should do the classic clj kitchensink lib thing but only with fns that meet this constraint

Go Up