Converting sequence from sort into a list

89 views
Skip to first unread message

Dave Tenny

unread,
May 2, 2014, 10:53:34 AM5/2/14
to clo...@googlegroups.com
I have a sequence from a call to 'sort'.
I want a list.

What is the best way to do this?

(apply list (sort ...))?

Will it have problems on large sequence inputs?


I can't use (into () (sort ...))
since that conjoins and ruins the sort order.


Plínio Balduino

unread,
May 2, 2014, 11:01:50 AM5/2/14
to Clojure Official
Hi Dave

Sorry if I didn't get it, but doesn't sort already return a list?

Could explain?

Plínio



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gary Trakhman

unread,
May 2, 2014, 11:07:56 AM5/2/14
to clo...@googlegroups.com
I believe what he wants is a persistentlist.  You could get one from (apply list) or more succinctly/esoterically (list* ..)

Sort returns a seq view over the array returned by java.util.Array.sort()

(defn sort
  "Returns a sorted sequence of the items in coll. If no comparator is
  supplied, uses compare. comparator must
  implement java.util.Comparator."
  {:added "1.0"
   :static true}
  ([coll]
   (sort compare coll))
  ([^java.util.Comparator comp coll]
   (if (seq coll)
     (let [a (to-array coll)]
       (. java.util.Arrays (sort a comp))
       (seq a))
     ())))

(class (sort (range 10)))
clojure.lang.ArraySeq

Gary Trakhman

unread,
May 2, 2014, 11:09:12 AM5/2/14
to clo...@googlegroups.com
Ah, scratch that, list* returns a seq as well.

Dave Tenny

unread,
May 2, 2014, 11:45:45 AM5/2/14
to clo...@googlegroups.com
Sort returns a seq,  but not necessarily something for which list? is true.


On Fri, May 2, 2014 at 11:01 AM, Plínio Balduino <pbal...@gmail.com> wrote:
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/sN37zftHSQ4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Dave Tenny

unread,
May 3, 2014, 9:30:14 AM5/3/14
to clo...@googlegroups.com
After nosing around all I've come up with via clojure mechanisms is to use (apply list (sort ...)).
It seems to work well enough for lists of arbitrary size (subject to usual memory/size limitations of large lists).

I also considered some native java abuse such as java.util.Arrays.asList(Enumeration),
though I didn't quickly find a way to convert the clojure.lang.ArraySeq from my sort() in testing to an Enumeration.

Guess I'm set for now, I was just hoping to avoid consing a new list on my sort result in order to get a specific collection type.


Sean Corfield

unread,
May 5, 2014, 9:01:00 PM5/5/14
to clo...@googlegroups.com
My question would be: why do you specifically need a list? i.e., why isn't a sequence good enough?

Sean
signature.asc

Dave Tenny

unread,
May 6, 2014, 6:56:02 AM5/6/14
to clo...@googlegroups.com
In my case I was just trying to ensure a list type for a java API, though perhaps the clojure reflection layer would have converted a non list seq to a list to match the call, I don't know.

Reply all
Reply to author
Forward
0 new messages