diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2009-08-21 14:31:58 -0400 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2009-08-21 14:31:58 -0400 |
commit | 5dbecc380cbb65afff33648325f1e3b44b535143 (patch) | |
tree | 2fbde73e938a6ad6ba5a2657631df513355900d5 | |
parent | 61d9b320c3b4da071d64229eaf1a7b061b1e87e2 (diff) |
java_utils.clj: made as-str variadic like str, fixes #18
This commit also includes tests for this function.
-rw-r--r-- | src/clojure/contrib/java_utils.clj | 36 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib.clj | 2 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/java_utils.clj | 10 |
3 files changed, 42 insertions, 6 deletions
diff --git a/src/clojure/contrib/java_utils.clj b/src/clojure/contrib/java_utils.clj index 75e8ca8b..5f34df16 100644 --- a/src/clojure/contrib/java_utils.clj +++ b/src/clojure/contrib/java_utils.clj @@ -90,11 +90,37 @@ (reduce file (file parent child) more))) (defn as-str - "Returns the name or string representation of x" - [x] - (if (instance? clojure.lang.Named x) - (name x) - (str x))) + "Returns the name or string representation of args, concatenated to + a single string. Like clojure.core/str, but uses the names of + keywords and symbols instead of their literal representation." + [& args] + (apply str (map (fn [x] (if (instance? clojure.lang.Named x) + (name x) x)) + args))) +(defn as-str + "Like clojure.core/str, but if an argument is a keyword or symbol, + its name will be used instead of its literal representation. + + Example: + (str :foo :bar) ;;=> \":foo:bar\" + (as-str :foo :bar) ;;=> \"foobar\" + + Note that this does not apply to keywords or symbols nested within + data structures; they will be rendered as with str. + + Example: + (str {:foo :bar}) ;;=> \"{:foo :bar}\" + (as-str {:foo :bar}) ;;=> \"{:foo :bar}\" " + ([] "") + ([x] (if (instance? clojure.lang.Named x) + (name x) + (str x))) + ([x & ys] + ((fn [#^StringBuilder sb more] + (if more + (recur (. sb (append (as-str (first more)))) (next more)) + (str sb))) + (new StringBuilder #^String (as-str x)) ys))) (defn get-system-property "Get a system property." diff --git a/src/clojure/contrib/test_contrib.clj b/src/clojure/contrib/test_contrib.clj index 17f3e0e9..d7b2597b 100644 --- a/src/clojure/contrib/test_contrib.clj +++ b/src/clojure/contrib/test_contrib.clj @@ -20,7 +20,7 @@ [:complex-numbers :fnmap :macro-utils :monads :pprint.pretty :pprint.cl-format :str-utils :shell-out :test-graph :test-dataflow :test-java-utils :test-lazy-seqs - :test-trace :test-jmx]) + :test-trace :test-jmx :java-utils]) (def test-namespaces (map #(symbol (str "clojure.contrib.test-contrib." (name %))) diff --git a/src/clojure/contrib/test_contrib/java_utils.clj b/src/clojure/contrib/test_contrib/java_utils.clj new file mode 100644 index 00000000..44901ad1 --- /dev/null +++ b/src/clojure/contrib/test_contrib/java_utils.clj @@ -0,0 +1,10 @@ +(ns clojure.contrib.test-contrib.java-utils + (:use clojure.test clojure.contrib.java-utils)) + +(deftest t-as-str + (is (= "foo" (as-str "foo"))) + (is (= "foo" (as-str 'foo))) + (is (= "foo" (as-str :foo))) + (is (= "[1 2 3]" (as-str [1 2 3]))) + (is (= "Hello, World!" (as-str "Hello, " :World \!))) + (is (= (str {:foo :bar}) (as-str {:foo :bar})))) |