summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2008-03-02 13:34:33 +0000
committerRich Hickey <richhickey@gmail.com>2008-03-02 13:34:33 +0000
commit66563c14ae32822b5f692486d30f18eebd533631 (patch)
tree375cd879c77ae806b0f63e1fdd49c8081ed3870c
parenteef883786dee479c4c2e30c950527e91221da309 (diff)
added slurp, subs, max-key, min-key
-rw-r--r--src/boot.clj35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/boot.clj b/src/boot.clj
index 582191be..d34f6e9b 100644
--- a/src/boot.clj
+++ b/src/boot.clj
@@ -2258,3 +2258,38 @@ var? [v]
#^{:doc "Returns the Class of x"}
class [#^Object x]
(. x (getClass)))
+
+(defn
+ #^{:doc "Reads the file named by f into a string and returns it."}
+slurp [f]
+ (let [r (new java.io.BufferedReader (new java.io.FileReader f))
+ sb (new StringBuilder)]
+ (loop [c (. r (read))]
+ (if (neg? c)
+ (str sb)
+ (do
+ (. sb (append (char c)))
+ (recur (. r (read))))))))
+
+(defn
+ #^{:doc "Returns the substring of s beginning at start inclusive,
+ and ending at end (defaults to length of string), exclusive."}
+subs
+ ([#^String s start] (. s (substring start)))
+ ([#^String s start end] (. s (substring start end))))
+
+(defn
+ #^{:doc "Returns the x for which (k x), a number, is greatest."}
+max-key
+ ([k x] x)
+ ([k x y] (if (> (k x) (k y)) x y))
+ ([k x y & more]
+ (reduce #(max-key k %1 %2) (max-key k x y) more)))
+
+(defn
+ #^{:doc "Returns the x for which (k x), a number, is least."}
+min-key
+ ([k x] x)
+ ([k x y] (if (< (k x) (k y)) x y))
+ ([k x y & more]
+ (reduce #(min-key k %1 %2) (min-key k x y) more)))