On Fri, Feb 20, 2009 at 1:59 PM, Steffen Glückselig <goo...@gungfu.de> wrote: > > I was trying to use Clojure to verify the behavior some method in > Java. > > Namely I wanted to quickly check whether Collections.sort does the > sorting I need. > > So I came up with: > > (let [list '("1" "KB" "K6" "2" "EÜ" "EZ" "ES")] > (do > (java.util.Collections/sort list) > list)) > > But since Collections/sort mutates the list in place I cannot get a > result. > > Is there a way in Clojure to get at the result of operations like > these?
With a little poking around I found what you need. First, what args is sort actually taking? user=> (use '[clojure.contrib.repl-utils :only (show)]) nil user=> (show java.util.Collections "sort") === public java.util.Collections === [11] static checkedSortedMap : SortedMap (SortedMap,Class,Class) [12] static checkedSortedSet : SortedSet (SortedSet,Class) [40] static sort : void (List) [41] static sort : void (List,Comparator) [47] static synchronizedSortedMap : SortedMap (SortedMap) [48] static synchronizedSortedSet : SortedSet (SortedSet) [53] static unmodifiableSortedMap : SortedMap (SortedMap) [54] static unmodifiableSortedSet : SortedSet (SortedSet) nil user=> (show java.util.Collections 40) #<Method public static void java.util.Collections.sort(java.util.List)> Ah, a java.util.List. But I seem to recall that being an interface, so I need something else that's concrete and mutable, but implements List. I seem to recall something called ArrayList... user=> (isa? java.util.ArrayList java.util.List) true Good enough, now how do you make one? user=> (show java.util.ArrayList) === public java.util.ArrayList === [ 0] <init> () [ 1] <init> (Collection) [ 2] <init> (int) [..snip..] So it can take a collection, like a clojure vector: user=> (def lst (java.util.ArrayList. ["1" "KB" "K6" "2" "EÜ" "EZ" "ES"])) #'user/lst And finally, the sort: user=> (java.util.Collections/sort lst) nil Bleh... not functional at all, are we? Oh well: user=> lst #<ArrayList [1, 2, ES, EZ, EÜ, K6, KB]> And there you go. --Chouser --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---