Am 30.05.2009 um 02:37 schrieb Brian Carper:
> Can anyone explain this?
>
> user> (def x {:foo :bar :foo :baz :foo :quux})
> #'user/x
> user> x
> {:foo :bar, :foo :baz, :foo :quux}
> user> (count (keys x))
> 3
> user> (map x (keys x))
> (:bar :bar :bar)
If I remember correctly, map literals smaller
than a certain size are based on array-map.
For speed reasons, this does not check for
duplicate keys. If you increase the size of
your literal beyond the threshold (was it
something around twenty entries?) the map
will be a hash-map and the effect should
go away...
My understanding is, that this falls under
"do broken stuff and you'll get broken stuff".
Map literals are most-likely small maps with
hard-wired keys. So the chance to have a
map like above is very small. And if there is
one I would strongly question the correctness.
So this shouldn't be a real problem in practice.
Sincerely
Meikel
> If I remember correctly, map literals smaller
> than a certain size are based on array-map.
> For speed reasons, this does not check for
> duplicate keys. If you increase the size of
> your literal beyond the threshold (was it
> something around twenty entries?) the map
> will be a hash-map and the effect should
> go away...
The threshold is currently 8, though that's an implementation detail,
not a promise.
user=> {:foo 1 :foo 2 :foo 3 :foo 4 :foo 5 :foo 6 :foo 7 :foo 8}
{:foo 1, :foo 2, :foo 3, :foo 4, :foo 5, :foo 6, :foo 7, :foo 8}
user=> {:foo 1 :foo 2 :foo 3 :foo 4 :foo 5 :foo 6 :foo 7 :foo 8 :foo 9}
{:foo 9}
> My understanding is, that this falls under
> "do broken stuff and you'll get broken stuff".
> Map literals are most-likely small maps with
> hard-wired keys. So the chance to have a
> map like above is very small. And if there is
> one I would strongly question the correctness.
>
> So this shouldn't be a real problem in practice.
+1
--Steve