PATCH: Make name accept String

0 views
Skip to first unread message

Drew Raines

unread,
Nov 11, 2008, 2:13:04 PM11/11/08
to clo...@googlegroups.com
I may be missing some philosophical significance of the name
function, but is there any reason why it can't work on Strings as
well as Named-s?

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

clj-multi-name.patch

Graham Fawcett

unread,
Nov 11, 2008, 3:11:27 PM11/11/08
to clo...@googlegroups.com
On Tue, Nov 11, 2008 at 2:13 PM, Drew Raines <aara...@gmail.com> wrote:
> I may be missing some philosophical significance of the name
> function, but is there any reason why it can't work on Strings as
> well as Named-s?

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

Reply all
Reply to author
Forward
0 new messages