On 9 January 2011 16:23, bOR_ <[email protected]> wrote:
> (type (sorted-map-by > 3 1 2 4 5 3))
> clojure.lang.PersistentTreeMap
> user> (type (select-keys (sorted-map-by > 3 1 2 4 5 3) (list 5 2 3)))
> clojure.lang.PersistentArrayMap
>
> Not sure if this is related in any way but the topic of PersistentTreeMaps,
> but I noticed this morning that select-keys doesn't return the same type of
> map as that it was fed. That happens more often in clojure, so it might be
> the default behaviour, and not something that needs to be fixed.
>
> For now I just looked at source of select-keys, and made a copy of the
> function that builds a sorted-map-by, rather than just a {}. Is that a
> sensible way to do it.
How about using (empty map):
(defn select-keys2
"Returns a map containing only those entries in map whose key is in keys"
{:added "1.0"}
[map keyseq]
(loop [ret (empty map) keys (seq keyseq)]
(if keys
(let [entry (. clojure.lang.RT (find map (first keys)))]
(recur
(if entry
(conj ret entry)
ret)
(next keys)))
ret)))
user=> (type (select-keys2 (sorted-map-by > 3 1 2 4 5 3) (list 5 2 3)))
clojure.lang.PersistentTreeMap
--
Michael Wood <[email protected]>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en