How to convert a Data.Set to a Descending List?

Hello everybody, Is there a more efficient way to convert a Set to a descending list? I was looking for a function similar to Set.toAscList something like Set.toDecList . I feel first converting to a ascending list and then reversing may be in-efficient given that I actually don't need all the elements only a last couple of elements.. Thanks, Sunil.

Hi! On Sun, Aug 21, 2011 at 01:48:05PM +0530, Sunil S Nandihalli wrote:
Is there a more efficient way to convert a Set to a descending list? I was looking for a function similar to Set.toAscList something like Set.toDecList . I feel first converting to a ascending list and then reversing may be in-efficient given that I actually don't need all the elements only a last couple of elements..
Why don't you use something like take N $ Set.toAscList s then? "A last couple of elements" in descending list would be "a first couple of elements" in ascending list. That approach may even be *faster* than building descending list, because to get your elements you'll build only beginning of the list, while with descending list, you'll need to build whole list in order to get last elements. -- Regards, Alexander Batischev 1024D/69093C81 F870 A381 B5F5 D2A1 1B35 4D63 A1A7 1C77 6909 3C81

Hi Alex,
Sorry for the miswording of my question. The think I am interested in
the first couple in the descending list (which would be last few in
the ascending list)
Thanks
Sunil.
On Sun, Aug 21, 2011 at 2:23 PM, Alexander Batischev
Hi!
On Sun, Aug 21, 2011 at 01:48:05PM +0530, Sunil S Nandihalli wrote:
Is there a more efficient way to convert a Set to a descending list? I was looking for a function similar to Set.toAscList something like Set.toDecList . I feel first converting to a ascending list and then reversing may be in-efficient given that I actually don't need all the elements only a last couple of elements..
Why don't you use something like
take N $ Set.toAscList s
then? "A last couple of elements" in descending list would be "a first couple of elements" in ascending list. That approach may even be *faster* than building descending list, because to get your elements you'll build only beginning of the list, while with descending list, you'll need to build whole list in order to get last elements.
-- Regards, Alexander Batischev
1024D/69093C81 F870 A381 B5F5 D2A1 1B35 4D63 A1A7 1C77 6909 3C81
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux)
iEYEARECAAYFAk5Qx6UACgkQoaccd2kJPIHfpwCfVhaOZJnfxA3wJVW6EImZX5EE 7Q4AnAu8oUiFTnek0lkBdq7HJLHREZsg =LXPG -----END PGP SIGNATURE-----
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sun, Aug 21, 2011 at 5:18 AM, Sunil S Nandihalli
Is there a more efficient way to convert a Set to a descending list? I was looking for a function similar to Set.toAscList something like Set.toDecList . I feel first converting to a ascending list and then reversing may be in-efficient given that I actually don't need all the elements only a last couple of elements..
Implementing toDecList should be easy, but it's not implemented. So I can see a couple of ideas: a) Instead of using a 'Set X', where X is your datatype, use 'Set (Rev X)' where newtype Rev t = Rev t instance Eq t => Eq (Rev t) where Rev x == Rev y = x == y instance Ord t => Ord (Rev t) where compare (Rev x) (Rev y) = compare y x Then you would just need to use toAscList =). b) If you need both the few first and few last elements to toAscList, then option a doesn't work. But if you really need just a few elements, you may use maxKey: toDecList s = case maxView s of Nothing -> [] Just (x, s') -> x : toDecList s' The problem is that 'take k . toDecList' take O(k log n) instead of O(k). c) Data.Set.fold says that the order is unspecified. But the current implementation does the fold in ascending order. You may cheat and implement: toDecList s = fold (\a b -> b . (a:)) id s [] Cheers, =) -- Felipe.

Hi Felipe,
Thanks a lot for your response. I thought I should describe what I
want in a little more detail. You may have a better suggestion to
solve the problem.
I currently have a Set I want to take a couple of elements on either
side of a given key. How can I do this? Here is a description of the
subseq function in clojure.
http://clojuredocs.org/clojure_core/clojure.core/subseq
Thanks,
Sunil.
On Sun, Aug 21, 2011 at 6:00 PM, Felipe Almeida Lessa
On Sun, Aug 21, 2011 at 5:18 AM, Sunil S Nandihalli
wrote: Is there a more efficient way to convert a Set to a descending list? I was looking for a function similar to Set.toAscList something like Set.toDecList . I feel first converting to a ascending list and then reversing may be in-efficient given that I actually don't need all the elements only a last couple of elements..
Implementing toDecList should be easy, but it's not implemented. So I can see a couple of ideas:
a) Instead of using a 'Set X', where X is your datatype, use 'Set (Rev X)' where
newtype Rev t = Rev t instance Eq t => Eq (Rev t) where Rev x == Rev y = x == y instance Ord t => Ord (Rev t) where compare (Rev x) (Rev y) = compare y x
Then you would just need to use toAscList =).
b) If you need both the few first and few last elements to toAscList, then option a doesn't work. But if you really need just a few elements, you may use maxKey:
toDecList s = case maxView s of Nothing -> [] Just (x, s') -> x : toDecList s'
The problem is that 'take k . toDecList' take O(k log n) instead of O(k).
c) Data.Set.fold says that the order is unspecified. But the current implementation does the fold in ascending order. You may cheat and implement:
toDecList s = fold (\a b -> b . (a:)) id s []
Cheers, =)
-- Felipe.

On Aug 21, 2011, at 9:46 AM, Sunil S Nandihalli wrote:
I currently have a Set I want to take a couple of elements on either side of a given key. How can I do this?
I'll bet this is very easy using Finger Trees.
http://hackage.haskell.org/packages/archive/fingertree/0.0/doc/html/Data-Fin...
____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com
participants (4)
-
Alexander Batischev
-
David Place
-
Felipe Almeida Lessa
-
Sunil S Nandihalli