Bug? sorted-map as fn behaves differently than hash-map as fn

3 views
Skip to first unread message

MikeM

unread,
Dec 17, 2008, 8:55:24 AM12/17/08
to Clojure
With SVN 1162,

((hash-map 'a 1 'b 2) "a") => nil
((sorted-map 'a 1 'b 2) "a") => ClassCastException

I would think that sorted-maps and hash-maps should both give nil for
this, but perhaps the sorted-map implementation requires a cast?

Meikel Brandmeyer

unread,
Dec 17, 2008, 9:14:20 AM12/17/08
to Clojure
Hi,

On 17 Dez., 14:55, MikeM <michael.messini...@invista.com> wrote:
> ((sorted-map 'a 1 'b 2) "a") => ClassCastException
>
> I would think that sorted-maps and hash-maps should both give nil for
> this, but perhaps the sorted-map implementation requires a cast?

Obviously a string is not a symbol. A sorted-map only
makes sense with comparable keys. How do you compare
a string and a symbol for sorting? And indeed:

((sorted-map 'a 1 'b 2) 'c) => nil

I think, that this is a bug - not in sorted-map, but in the user
code providing a wrong (type of) key.

Sincerely
Meikel

Randall R Schulz

unread,
Dec 17, 2008, 9:25:00 AM12/17/08
to clo...@googlegroups.com

What about when you use the three-argument form of (get ...) on a sorted
map with a type mismatch on the supplied lookup key? One _might_ argue
that in that case the exception should be caught and the default value
returned.


> Sincerely
> Meikel


Randall Schulz

Mark Volkmann

unread,
Dec 17, 2008, 9:26:18 AM12/17/08
to clo...@googlegroups.com

I expected that


((sorted-map 'a 1 'b 2) "a")

would return the same result as


((sorted-map 'a 1 'b 2) 'c)

In both cases the key isn't in the map, so I thought nil would be returned.

It appears that you can create a sorted-map where all the keys are symbols
and you can create a sorted-map where all the keys are strings,
but you can't create a sorted-map where some of the keys are symbols
and some are strings.
I take it this is because, as you say above, symbols and strings can't
be compared to each other.

--
R. Mark Volkmann
Object Computing, Inc.

Randall R Schulz

unread,
Dec 17, 2008, 9:34:27 AM12/17/08
to clo...@googlegroups.com
On Wednesday 17 December 2008 06:26, Mark Volkmann wrote:
> ...

>
> It appears that you can create a sorted-map where all the keys are
> symbols and you can create a sorted-map where all the keys are
> strings, but you can't create a sorted-map where some of the keys are
> symbols and some are strings.
> I take it this is because, as you say above, symbols and strings
> can't be compared to each other.

There's also (sorted-map-by ...):

user=> (doc sorted-map-by)
-------------------------
clojure.core/sorted-map-by
([comparator & keyvals])
keyval => key val
Returns a new sorted map with supplied mappings,
using the supplied comparator.


So you can write a less stringent comparator if you like and use that to
side-step this sort of exception.


Randall Schulz

Rich Hickey

unread,
Dec 17, 2008, 9:51:40 AM12/17/08
to Clojure


On Dec 17, 9:26 am, "Mark Volkmann" <r.mark.volkm...@gmail.com> wrote:
> On Wed, Dec 17, 2008 at 8:14 AM, Meikel Brandmeyer <m...@kotka.de> wrote:
>
> > Hi,
>
> > On 17 Dez., 14:55, MikeM <michael.messini...@invista.com> wrote:
> >> ((sorted-map 'a 1 'b 2) "a") => ClassCastException
>
> >> I would think that sorted-maps and hash-maps should both give nil for
> >> this, but perhaps the sorted-map implementation requires a cast?
>
> > Obviously a string is not a symbol. A sorted-map only
> > makes sense with comparable keys. How do you compare
> > a string and a symbol for sorting? And indeed:
>
> > ((sorted-map 'a 1 'b 2) 'c) => nil
>
> > I think, that this is a bug - not in sorted-map, but in the user
> > code providing a wrong (type of) key.
>
> I expected that
> ((sorted-map 'a 1 'b 2) "a")
> would return the same result as
> ((sorted-map 'a 1 'b 2) 'c)
>
> In both cases the key isn't in the map, so I thought nil would be returned.
>

One can't determine that a key isn't in a sorted map without comparing
it to other keys. Thus, unless you supply your own Comparator (using
sorted-map-by), it is subject to the contract of Comparable, which
specifies ClassCastException on type mismatch.

Rich

Reply all
Reply to author
Forward
0 new messages