Clojure

Set func create sorted hash-set

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: Release 1.2
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    Ubuntu 10.04

Description

(def _sortedset (set (list 1,3,2)))
(println _sortedset)
(println (class _sortedset))

This code output is:

>> #{1 2 3}
>> clojure.lang.PersistentHashSet

As you can see set function create hash-set, but already sorted.

Activity

Hide
Sergey Krauchenia added a comment -

Thanks Michel for explanation.

Show
Sergey Krauchenia added a comment - Thanks Michel for explanation.
Hide
Fogus added a comment -

The hash set that you're creating only seems sorted. That is, while the ordering of a set is not guaranteed, it's a perfectly valid set if it is. The ordering in this case is due to implementation details guided by the fact that you happen to be inserting values of the same type whose hash values are ordered. If you make a change to one of the values then you could see a change:

(set (list 1 3.0 2))
;=> #{3.0 1 2)

Show
Fogus added a comment - The hash set that you're creating only seems sorted. That is, while the ordering of a set is not guaranteed, it's a perfectly valid set if it is. The ordering in this case is due to implementation details guided by the fact that you happen to be inserting values of the same type whose hash values are ordered. If you make a change to one of the values then you could see a change: (set (list 1 3.0 2)) ;=> #{3.0 1 2)

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: