Sorted Sets With Duplicate Elements
First of all: I don't EXACTLY mean duplicate elements. I just mean duplicates in those parts of the elements which are compared. For instance, I recently tried to have a sorted set of 2-element vectors where the comparator was used on the second element, however, something like this happened: (sorted-set-by #( (second %) (second %2)) [:a 1] [:b 1] [:c 1]) == #{[:a 1]} Does anyone know how I could have a set that includes all of these elements? -- 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
Re: Sorted Sets With Duplicate Elements
what about using = as sorting fuction? 2012/11/20 JvJ kfjwhee...@gmail.com First of all: I don't EXACTLY mean duplicate elements. I just mean duplicates in those parts of the elements which are compared. For instance, I recently tried to have a sorted set of 2-element vectors where the comparator was used on the second element, however, something like this happened: (sorted-set-by #( (second %) (second %2)) [:a 1] [:b 1] [:c 1]) == #{[:a 1]} Does anyone know how I could have a set that includes all of these elements? -- 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 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
Re: Sorted Sets With Duplicate Elements
Simple solution. It works! Thanks. On Tuesday, 20 November 2012 10:59:23 UTC-5, Bronsa wrote: what about using = as sorting fuction? 2012/11/20 JvJ kfjwh...@gmail.com javascript: First of all: I don't EXACTLY mean duplicate elements. I just mean duplicates in those parts of the elements which are compared. For instance, I recently tried to have a sorted set of 2-element vectors where the comparator was used on the second element, however, something like this happened: (sorted-set-by #( (second %) (second %2)) [:a 1] [:b 1] [:c 1]) == #{[:a 1]} Does anyone know how I could have a set that includes all of these elements? -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clo...@googlegroups.comjavascript: 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 javascript: 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 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
Re: Sorted Sets With Duplicate Elements
I would be surprised if you are still happy with that solution a few minutes after doing some testing with it. I've added some examples to ClojureDocs.org at the link below, with a suggested better comparison function. Take a look and let me know if it seems clear: http://clojuredocs.org/clojure_core/clojure.core/sorted-set-by I've written some text explaining why the simpler comparison functions fail, but it is over 100 lines long right now and I should trim it down or find a different place for it than ClojureDocs.org. Andy On Nov 20, 2012, at 8:01 AM, JvJ wrote: Simple solution. It works! Thanks. On Tuesday, 20 November 2012 10:59:23 UTC-5, Bronsa wrote: what about using = as sorting fuction? 2012/11/20 JvJ kfjwh...@gmail.com First of all: I don't EXACTLY mean duplicate elements. I just mean duplicates in those parts of the elements which are compared. For instance, I recently tried to have a sorted set of 2-element vectors where the comparator was used on the second element, however, something like this happened: (sorted-set-by #( (second %) (second %2)) [:a 1] [:b 1] [:c 1]) == #{[:a 1]} Does anyone know how I could have a set that includes all of these elements? -- 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
Re: Sorted Sets With Duplicate Elements
On Tue, Nov 20, 2012 at 8:01 AM, JvJ kfjwhee...@gmail.com wrote: Simple solution. It works! Thanks. No, it doesn't work. It may print correctly now, but it won't actually behave the way you expect. Clojure's sorted collections must be provided with a sorting function where items tie if and only if they are equal. (sorted-set-by #(compare [(second %) %] [(second %2) %2]) [:a 1] [:b 1] [:c 1])) Your use case makes me think you might actually want to investigate clojure.data.priority-map though: https://github.com/clojure/data.priority-map If that's a better fit for what you are trying to do, just insert [org.clojure/data.priority-map 0.0.2] into your project.clj dependencies. -- 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
Re: Sorted Sets With Duplicate Elements
Thanks for the tip. On Tuesday, 20 November 2012 13:13:09 UTC-5, puzzler wrote: On Tue, Nov 20, 2012 at 8:01 AM, JvJ kfjwh...@gmail.com javascript:wrote: Simple solution. It works! Thanks. No, it doesn't work. It may print correctly now, but it won't actually behave the way you expect. Clojure's sorted collections must be provided with a sorting function where items tie if and only if they are equal. (sorted-set-by #(compare [(second %) %] [(second %2) %2]) [:a 1] [:b 1] [:c 1])) Your use case makes me think you might actually want to investigate clojure.data.priority-map though: https://github.com/clojure/data.priority-map If that's a better fit for what you are trying to do, just insert [org.clojure/data.priority-map 0.0.2] into your project.clj dependencies. -- 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
Re: Sorted Sets With Duplicate Elements
On Nov 20, 2012, at 10:12 AM, Mark Engelberg wrote: Clojure's sorted collections must be provided with a sorting function where items tie if and only if they are equal. (sorted-set-by #(compare [(second %) %] [(second %2) %2]) [:a 1] [:b 1] [:c 1])) Mark, I like the brevity of this way of writing comparison functions with tie breakers, and see that it would extend well to multiple sort keys, and ascending or descending order on each key can be chosen independently. My question is perhaps of the is-the-number-of-angels-that-can-dance-on-the-head-of-a-pin-finite-or-infinite kind. Does Clojure promise anywhere in its documentation that it compares vectors and sequences in lexicographic order? I know the implementation does in fact do this today, and probably has since compare was implemented for vectors, and there is no good reason I know of that it might ever change. For most purposes, I'd say that is good enough to run with it and tell the whole world. The only nagging doubt is relying on undocumented behavior. I guess my real question is: Would it be good for Clojure to document and promise this comparison behavior? Andy -- 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
Re: Sorted Sets With Duplicate Elements
On Tuesday, November 20, 2012 10:29:18 AM UTC-8, Andy Fingerhut wrote: On Nov 20, 2012, at 10:12 AM, Mark Engelberg wrote: Clojure's sorted collections must be provided with a sorting function where items tie if and only if they are equal. (sorted-set-by #(compare [(second %) %] [(second %2) %2]) [:a 1] [:b 1] [:c 1])) Mark, I like the brevity of this way of writing comparison functions with tie breakers, and see that it would extend well to multiple sort keys, and ascending or descending order on each key can be chosen independently. My question is perhaps of the is-the-number-of-angels-that-can-dance-on-the-head-of-a-pin-finite-or-infinite kind. Does Clojure promise anywhere in its documentation that it compares vectors and sequences in lexicographic order? I hope not, because that's not what it does! Sequences don't implement Comparable at all, so unless you supply a comparator they can't be sorted. And vectors are compared first by length; only if they have the same length are their elements considered for a lexicographical comparison. -- 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
Re: Sorted Sets With Duplicate Elements
On Nov 20, 2012, at 11:04 AM, Alan Malloy wrote: On Tuesday, November 20, 2012 10:29:18 AM UTC-8, Andy Fingerhut wrote: On Nov 20, 2012, at 10:12 AM, Mark Engelberg wrote: Clojure's sorted collections must be provided with a sorting function where items tie if and only if they are equal. (sorted-set-by #(compare [(second %) %] [(second %2) %2]) [:a 1] [:b 1] [:c 1])) Mark, I like the brevity of this way of writing comparison functions with tie breakers, and see that it would extend well to multiple sort keys, and ascending or descending order on each key can be chosen independently. My question is perhaps of the is-the-number-of-angels-that-can-dance-on-the-head-of-a-pin-finite-or-infinite kind. Does Clojure promise anywhere in its documentation that it compares vectors and sequences in lexicographic order? I hope not, because that's not what it does! Sequences don't implement Comparable at all, so unless you supply a comparator they can't be sorted. And vectors are compared first by length; only if they have the same length are their elements considered for a lexicographical comparison. Thanks for the corrections. Then my question is still: Would it be good for Clojure to document and promise this comparison behavior? If not for all cases, at least for equal-length vectors being compared lexicographically? Andy -- 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
Re: Sorted Sets With Duplicate Elements
On Tue, Nov 20, 2012 at 11:08 AM, Andy Fingerhut andy.finger...@gmail.comwrote: Then my question is still: Would it be good for Clojure to document and promise this comparison behavior? If not for all cases, at least for equal-length vectors being compared lexicographically? I frequently rely on Clojure's behavior that equal-length vectors are compared lexicographically, so I certainly hope that this is an implicit promise, but I don't know that I've seen it documented anywhere. I've thought about how it would be nice to have a library with several useful comparators for complex objects. For example: 1. True lexicographic comparison for all sequences (both vectors and lists). Right now, lists can't be compared. Vectors compare by length first. 2. Something that can compare all possible Clojure objects in an arbitrary but consistent way. There are a lot of times where you just want distinct objects to have some sort of tiebreaker so that sorting will work, but you don't really care what order tied items end up in. For example, I may want to create a sorted collection out of [[1 :a] [2 :b] [1 c]] in a way that guarantees that the 1 entries are before the 2 entry, but I don't really care about the exact order of the 1 entries. Several other languages handle this by ensuring that all objects can be compared, I think Python is an example of this. This way the trick I showed of (sort-by #(compare [(attribute-I-care-about %) %] [(attribute-I-care-about %2) %2)]) ...) will always work. Haven't gotten around to implementing said library, although I probably should since I seem to end up re-implementing bits and pieces of these ideas in my own code all the time. -- 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