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)))))
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))