user=> (name :foo)
"foo"
user=> (name "foo")
"foo"
This would clean up conditionals I have scattered about where I
normalize heterogeneous collections of keywords and strings.
(reduce #(let [m %1
[k v] %2]
(conj m [(if (keyword? k)
(name k)
k) v]))
{} {:foo :bar "baz" :quux})
(reduce #(let [m %1
[k v] %2]
(conj m [(name k) v]))
{} {:foo :bar "baz" :quux})
=> {"foo" :bar, "baz" :quux}
The attached patch does this with a couple of multimethods.
-Drew
Hi,
I think the philosophical point is that a String doesn't have a name,
and shouldn't pretend that it does. :-)
Why not just define your own utility function, e.g.
(defn name-string-friendly [s]
(if (instance? clojure.lang.Named s)
(name s)
s))
and use that wherever you've been using (name)?
If (name) were to be changed, I'd vote to have it return nil for any
input value other than a Named instance. Then you could write
(or (name ks) ks)
which is concise and very Lispy, IMHO.
Graham