summaryrefslogtreecommitdiff
path: root/src/utils.clj
diff options
context:
space:
mode:
authordumpfmprod <dumpfmprod@ubuntu.(none)>2010-11-14 16:22:46 -0500
committerdumpfmprod <dumpfmprod@ubuntu.(none)>2010-11-14 16:22:46 -0500
commit9e15175efa7d056d27e94e15116b2a34dbe92346 (patch)
tree09c3442be57ade63412e4ca461f9b5ce70e455d6 /src/utils.clj
parent2041ed45c75ded3b0f7b6c5e6536dc262406b0d1 (diff)
parent789ed39751c17fa1aa73f3119a80403e34f0d984 (diff)
Merge branch 'master' of /pichat/repo
Diffstat (limited to 'src/utils.clj')
-rwxr-xr-xsrc/utils.clj41
1 files changed, 27 insertions, 14 deletions
diff --git a/src/utils.clj b/src/utils.clj
index 1a9e09e..9d7fd3a 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)
@@ -466,3 +471,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)))