diff options
| author | Scott Ostler <sbsotler@gmail.com> | 2010-11-14 00:22:56 -0800 |
|---|---|---|
| committer | Scott Ostler <sbsotler@gmail.com> | 2010-11-14 00:22:56 -0800 |
| commit | 7cd7b47e2e24c800e210d38e4f888bcb93cd438b (patch) | |
| tree | a8a2151eab0853f3187438e241867b2e333f3f24 | |
| parent | 90537b1498485f7571cedd47ed5700cd49f82afe (diff) | |
cleaned up utility functions, added index-of func
| -rwxr-xr-x | src/utils.clj | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/src/utils.clj b/src/utils.clj index 08b06ab..8ed801e 100755 --- a/src/utils.clj +++ b/src/utils.clj @@ -18,6 +18,7 @@ clojure.contrib.sql clojure.contrib.def clojure.contrib.duck-streams + clojure.contrib.seq-utils clojure.contrib.str-utils compojure config)) @@ -82,11 +83,12 @@ (declare stringify-and-escape) (defn escape-html-deep [o] - (if (map? o) - (stringify-and-escape o) - (cond (seq? o) (map escape-html-deep o) - (or (true? o) (false? o)) o - :else (escape-html o)))) + (cond (map? o) (stringify-and-escape o) + (vector? o) (map escape-html-deep o) + (seq? o) (map escape-html-deep o) + (true? o) o + (false? o) o + :else (escape-html o))) (defn stringify-and-escape [m] (zipmap (map str* (keys m)) (map escape-html-deep (vals m)))) @@ -343,18 +345,21 @@ ;; Parsing -(= (type 0) java.lang.Integer) - (defn maybe-parse-int ([s] (maybe-parse-int s 0)) - ([s default] - (if (= (type s) java.lang.Integer) - s - (try (Integer/parseInt s) - (catch NumberFormatException _ default))))) + ([s a] + (if (number? s) + (int s) + (try (Integer/parseInt s) + (catch NumberFormatException _ a))))) -(defn maybe-parse-long [s f] - (if s (Long/parseLong s) f)) +(defn maybe-parse-long + ([s] (maybe-parse-long s 0)) + ([s a] + (if (number? s) + (long s) + (try (Long/parseLong s) + (catch NumberFormatException _ a))))) (defn parse-yyyy-mm-dd-date [s] (try (.parse yyyy-mm-dd-formatter s) @@ -463,3 +468,11 @@ (swap! cached-results assoc arguments { :result result :time (System/currentTimeMillis)}) result))))) + +;; Taken from Programming Clojure by Stuart Halloway + +(defn index-filter [pred coll] + (for [[idx elt] (indexed coll) :when (pred elt)] idx)) + +(defn index-of [pred coll] + (first (index-filter pred coll)))
\ No newline at end of file |
