Hi,

another hint that you can rely on sorted maps returning sorted keys and seqis 
the behavior of Sorted Map in Java 7

HTH
-Gianluca

On Sunday, October 20, 2013 4:10:53 PM UTC+2, juan.facorro wrote:
>
> Hi Ru,
>
> I hope I understood your question correctly, because I did some digging 
> based on that, since I was curious about the answer.
>
> **
> The implementation for sorted maps (at least in 1.5.1 and as far back as 
> 1.2.0, 
> maybe farther, I didn't check) guarantees that the result from from *keys* 
> will 
> return an ascending sorted seq of keys; *vals* will return a seq 
> following the corresponding keys order. Having said that, the docstring for 
> *keys* doesn't mention anything about the order of the elements in the 
> sequence, so the order could considered as an implementation detail... on 
> the other hand it hasn't changed in quite a while so if it were me I would 
> consider it a feature, plus the documentation on sorted maps 
> here<http://clojure.org/data_structures>, 
> says:
>
> [...] 
> seq<http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/seq>returns
>  a sequence of map entries, which are key/value pairs. Sorted map 
>> also supports 
>> rseq<http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/rseq>,
>>  
>> which returns the entries in reverse order. [...]
>
>
> Which implicitly suggests (at least for me) that *seq* returns the 
> entries in order (since *rseq* returns the inverse, but *only* for sorted 
> maps).
>
> Hope it helps
>
> ---------
>
> Digging details:
>
> *vals* is implemented in terms of* 
> clojure.lang.RT.vals()<https://github.com/clojure/clojure/blob/clojure-1.5.1/src/jvm/clojure/lang/RT.java#L518>
> * (*keys* follows the same approach). There, the map is *seq*'ed and that 
> is what is used in 
> *APersistentMap.ValSeq*<https://github.com/clojure/clojure/blob/clojure-1.5.1/src/jvm/clojure/lang/APersistentMap.java#L164>
> 's (and 
> *APersistentMap.KeySeq*<https://github.com/clojure/clojure/blob/clojure-1.5.1/src/jvm/clojure/lang/APersistentMap.java#L133>
> 's) implementation of *first* and *next* to return the values. The 
> sequence of entries as a result from *(seq m)** *determines the order of 
> the keys *and* values in the seqs returned from the functions. This means 
> that if entries are sorted by their keys, then the corresponding values 
> will follow the same order with vals,* *that is, they won't be 
> necessarily ordered.
>
> The *seq* of a *sorted-map* is guaranteed to return entries ordered by *
> key*, based on the implementation of 
> *entrySet()*<https://github.com/clojure/clojure/blob/clojure-1.5.1/src/jvm/clojure/lang/APersistentMap.java#L214>
>  (from 
> *APersitentMap*) and the 
> *iterator()*<https://github.com/clojure/clojure/blob/clojure-1.5.1/src/jvm/clojure/lang/PersistentTreeMap.java#L204>
>  and 
> *NodeIterator*<https://github.com/clojure/clojure/blob/clojure-1.5.1/src/jvm/clojure/lang/PersistentTreeMap.java#L836>(from
>  
> *PersistentTreeMap*, class with which sorted maps are implemented).    
>
> Juan
>
> On Sunday, October 20, 2013 8:25:02 PM UTC+8, ru wrote:
>>
>> Dear clojure people!
>>
>> Do results of functions "keys" and "vals" ordered for sorted maps 
>> accordingly? Does it guaranteed?
>>
>> Thanks in advance.
>>
>> Sincerely,
>>   Ru
>>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to