This ticket may be related, http://dev.clojure.org/jira/browse/CLJ-1093
On Friday, August 9, 2013 12:08:06 AM UTC+2, Jozef Wagner wrote:
It may be a bug somewhere in a Compiler. I've lost track at
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L6624
after
I am having trouble with an implementation of a custom reader literal
called #sorted-set.
Please see my short code first:
https://github.com/xpe/sorted-map-literal
Why does this work correctly:
(to-sorted-map '(1 2 3 4))
#sorted-map (1 2 3 4) ; correct
While this does not?
#sorted-map (1 2 3 4
, 2013 7:29:16 PM UTC+2, David James wrote:
I am having trouble with an implementation of a custom reader literal
called #sorted-set.
Please see my short code first:
https://github.com/xpe/sorted-map-literal
Why does this work correctly:
(to-sorted-map '(1 2 3 4))
#sorted-map (1 2 3 4
That's a good point about:
user= eval (to-sorted-map '(1 2 3 4)))
{1 2, 3 4}
But this should work, right?
user= (assoc #sorted-map (:a 1 :b 2) :c 3)
{:c 3, :a 1, :b 2} ; incorrect
--
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this
It seems there is something else in data reader which causes this change.
user= (class '#foo/sm (1 2 3 4))
clojure.lang.PersistentArrayMap
user= (class (read-string #foo/sm (1 2 3 4)))
clojure.lang.PersistentTreeMap
It's quite puzzling. In both cases the evaluation does not take place, but
I'd really appreciate if others could take a look. I wonder if it may
be a Clojure reader bug.
On Thu, Aug 8, 2013 at 3:55 PM, Jozef Wagner jozef.wag...@gmail.com wrote:
It seems there is something else in data reader which causes this change.
user= (class '#foo/sm (1 2 3 4))
It may be a bug somewhere in a Compiler. I've lost track at
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L6624
after debugging this:
user (def x `(quote ~(list 1 (clojure.lang.PersistentTreeMap/create (seq
[1 2 3 4])
#'user/x
user x
(quote (1
Hi all,
please consider the following record definition
(defrecord RDistance
[node dist visited])
as well as an instance and a tree set with a custom comparator ordering
content first by :visited and after that by :dist
(def d (RDistance. foo 1 0))
(def tree (sorted-set-by (comparator (juxt
tree (sorted-set-by (comparator (juxt :visited :dist)) d))
I expect conj d again to the set would change the set, but doing so in the
repl results in the set containing two equal elements
user= (conj tree d)
#{#user.RDistance{:node foo, :dist 1, :visited 0} #user.RDistance{:node
foo, :dist 1
Hi,
you are using comparator incorrectly. The function you pass there should
return true, when x is to the left of y when called as (f x y). See the
following example.
user= (defrecord Foo [a b])
user.Foo
; Wrong usage: your example (The new element is always smaller!)
user= (- (sorted-set
d (RDistance. foo 1 0))
(def tree (sorted-set-by (comparator (juxt :visited :dist)) d))
I expect conj d again to the set would change the set, but doing so in
the repl results in the set containing two equal elements
user= (conj tree d)
#{#user.RDistance{:node foo, :dist 1, :visited 0
a) (hash b))
(and
(== (hash a) (hash b))
(not= a b)
(or (contains? @m [a b])
(and
(not (contains? @m [b a]))
(swap! m conj [a b]))
(defn sorted-set-po [po keys]
(apply
Thanks for the explanations!
So is there a way to build a set or map that has sorting property
independent from the element lookup?
On Friday, March 16, 2012 2:03:15 AM UTC+1, Alan Malloy wrote:
And this is exactly as it should be. The sorted set has no way to
compare items other than
:
Thanks for the explanations!
So is there a way to build a set or map that has sorting property
independent from the element lookup?
On Friday, March 16, 2012 2:03:15 AM UTC+1, Alan Malloy wrote:
And this is exactly as it should be. The sorted set has no way to
compare items other than by your
presentation, by using a sort function that outputs a
seq. (Inefficient if the sort will then be performed often for a given
set.)
Option 3:
Implement your own sorted set type that handles sort-order collisions
by having plain sets as equality buckets, using deftype, gen-class, or
Java.
Option 4
])
(and
(not (contains? @m [b a]))
(swap! m conj [a b]))
(defn sorted-set-po [po keys]
(apply sorted-set-by (po-to po) keys))
Input: a partial order (may be true for a b, true for b a, or false for both).
Output: a sorted-set that uses a total order built
It seems that when using the sorted set with my own comparator
(sorted-set-by),
the lookup via 'contains?' function is based only on the part of the items
that participate in the ordering:
(contains? (sorted-set [1 :a] [2 :b]) [2 :c])
;= false
(contains? (sorted-set-by #( (%1 0) (%2 0)) [1
/master/src/jvm/clojure/lang/PersistentTreeMap.java#L285is
responsible for this behaviour.
Regards,
Stuart
On 13 March 2012 05:20, mnicky markus.mas...@gmail.com wrote:
It seems that when using the sorted set with my own comparator
(sorted-set-by),
the lookup via 'contains?' function is based
It's not a problem with Clojure, it's also how Java behaves.
For sorted-sets to work properly, your comparator *must* satisfy the
trichotomy property of orderings.
Consider a comparison predicate less?. The trichotomy property states that
for all x,y, exactly one of the following hold:
(less? x
And this is exactly as it should be. The sorted set has no way to
compare items other than by your comparator. If it just arbitrarily
decided to use = instead of checking that (zero? (compare x y)) it
would not be using your comparator.
Note also that the behavior of contains? is consistent
Hi,
How can I efficiently pick a random element from a sorted-set?
If I try rand-nth I get this:
user= (rand-nth (sorted-set 1 2 3))
java.lang.UnsupportedOperationException: nth not supported on this
type: PersistentTreeSet (NO_SOURCE_FILE:0)
I can get this expression to work if I naively apply
On Sep 26, 2011, at 8:12 AM, Paul Richards wrote:
How can I efficiently pick a random element from a sorted-set?
If your sorted set is densely packed (if most possible values do appear in the
set), then you could just pick a random value, see if it's in the set, and
repeat until you find
The reason that (rand-nth (seq (sorted-set 1 2 3))) performs badly on large
sets is probably because nth is O(n) on sequences. nth is much much faster
on vectors, so I would suggest trying out (rand-nth (vec (sorted-set 1 2
3))) and see if that works for your application.
--
You received
On Mon, Sep 26, 2011 at 9:12 AM, Paul Richards paul.richa...@gmail.com wrote:
Hi,
How can I efficiently pick a random element from a sorted-set?
If I try rand-nth I get this:
user= (rand-nth (sorted-set 1 2 3))
java.lang.UnsupportedOperationException: nth not supported on this
type
On Sep 26, 2:12 pm, Paul Richards paul.richa...@gmail.com wrote:
Hi,
How can I efficiently pick a random element from a sorted-set?
I've come up with a bit of a hack, which relies on me not caring about
non-uniform distributions. If I create a custom comparator with a
random backdoor, I can
On Sep 26, 6:13 pm, Jeremy Heiler jeremyhei...@gmail.com wrote:
On Mon, Sep 26, 2011 at 9:12 AM, Paul Richards paul.richa...@gmail.com
wrote:
Hi,
How can I efficiently pick a random element from a sorted-set?
If I try rand-nth I get this:
user= (rand-nth (sorted-set 1 2 3
On Sep 26, 6:04 pm, Benny Tsai benny.t...@gmail.com wrote:
The reason that (rand-nth (seq (sorted-set 1 2 3))) performs badly on large
sets is probably because nth is O(n) on sequences. nth is much much faster
on vectors, so I would suggest trying out (rand-nth (vec (sorted-set 1 2
3
On Monday, September 26, 2011 2:58:59 PM UTC-6, Paul Richards wrote:
This will replace an O(n) call to nth with an O(n) call to copy into
a vector, so still leaving me with O(n).
Oops, right :) If you're getting random elements out of the same sorted set
multiple times, then it might
On Sep 26, 2:12 pm, Paul Richards paul.richa...@gmail.com wrote:
On Sep 26, 2:12 pm, Paul Richards paul.richa...@gmail.com wrote:
Hi,
How can I efficiently pick a random element from a sorted-set?
I've come up with a bit of a hack, which relies on me not caring about
non-uniform
Trying to migrate some of my code from clojure and javascript to
ClojureScript and I'm missing 'sorted-set'
Does anyone know if is is one of those parts of the library that has
just not yet been migrated?
Or will not at all be migrated?
Or maybe I'm getting something wrong and its usage
Trying to migrate some of my code from clojure and javascript to
ClojureScript and I'm missing 'sorted-set'
Does anyone know if is is one of those parts of the library that has
just not yet been migrated?
Or will not at all be migrated?
Or maybe I'm getting something wrong and its usage
Hello everybody,
is there a function to find the first element after an element in the
sorted-set?
(first-element-after 3 (sorted-set 1 2 3 4 5 6))
should return 4?
thanks,
Sunil.
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group
On 24 May 2011 08:07, Sunil S Nandihalli sunil.nandiha...@gmail.com wrote:
Hello everybody,
is there a function to find the first element after an element in the
sorted-set?
(first-element-after 3 (sorted-set 1 2 3 4 5 6))
should return 4?
How about this?:
(first (drop-while (partial = 3
after an element in the
sorted-set?
(first-element-after 3 (sorted-set 1 2 3 4 5 6))
should return 4?
How about this?:
(first (drop-while (partial = 3) (sorted-set 1 2 3 4 5 6)))
--
Michael Wood esiot...@gmail.com
--
You received this message because you are subscribed to the Google
, 2011 at 11:45 AM, Michael Wood esiot...@gmail.com wrote:
On 24 May 2011 08:07, Sunil S Nandihalli sunil.nandiha...@gmail.com
wrote:
Hello everybody,
is there a function to find the first element after an element in the
sorted-set?
(first-element-after 3 (sorted-set 1 2 3 4 5 6))
should
Hi,
maybe subseq to the rescue?
user= (first (subseq (sorted-set 1 2 3 4 5 6) 3))
4
I'm not sure about the perfomance, but I'd think it's fast?
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email
thanks Meikel.. what you suggested might be it..
Sunil.
On Tue, May 24, 2011 at 1:48 PM, Meikel Brandmeyer m...@kotka.de wrote:
Hi,
maybe subseq to the rescue?
user= (first (subseq (sorted-set 1 2 3 4 5 6) 3))
4
I'm not sure about the perfomance, but I'd think it's fast?
Sincerely
Yes, subseq is fast.
On May 24, 1:18 am, Meikel Brandmeyer m...@kotka.de wrote:
Hi,
maybe subseq to the rescue?
user= (first (subseq (sorted-set 1 2 3 4 5 6) 3))
4
I'm not sure about the perfomance, but I'd think it's fast?
Sincerely
Meikel
--
You received this message because you
On Tue, May 24, 2011 at 4:18 AM, Meikel Brandmeyer m...@kotka.de wrote:
Hi,
maybe subseq to the rescue?
user= (first (subseq (sorted-set 1 2 3 4 5 6) 3))
4
I'm not sure about the perfomance, but I'd think it's fast?
Yes, this is right.
Note you can also walk the other direction using
On Fri, Dec 3, 2010 at 2:27 AM, Sunil S Nandihalli
sunil.nandiha...@gmail.com wrote:
Right now I am just seq ing it before using nth
Sunil.
The real oddity here is that nth doesn't seem to call seq on its
argument. It probably should (for non-vector arguments).
--
You received this message
Hello everybody,
I am thinking it would be nice to have nth work for sorted-set and
sorted-maps .. IMHO it makes at the least sense for sorted-set if not both
.. is there a reason why this is not implemented already?
Thanks,
Sunil.
--
You received this message because you are subscribed
Right now I am just seq ing it before using nth
Sunil.
On Fri, Dec 3, 2010 at 12:55 PM, Sunil S Nandihalli
sunil.nandiha...@gmail.com wrote:
Hello everybody,
I am thinking it would be nice to have nth work for sorted-set and
sorted-maps .. IMHO it makes at the least sense for sorted-set
Why does (transient #{}) works, but transient (sorted-set []) fails
with an exception:
clojure.lang.PersistentTreeSet cannot be cast to
clojure.lang.IEditableCollection
[Thrown class java.lang.ClassCastException]
--
Moritz Ulrich
Programmer, Student, Almost normal Guy
http://www.google.com
a transient hash set, and converting to a
persistent sorted set from the persistent hash set.
On Jul 8, 7:25 am, Moritz Ulrich ulrich.mor...@googlemail.com wrote:
Why does (transient #{}) works, but transient (sorted-set []) fails
with an exception:
clojure.lang.PersistentTreeSet cannot be cast
Hi,
Is this the intended behavior?
(sorted-set-by (constantly 0) 1 2 3 4) returns #{1}.
I would expect the set to contain all the elements, and the comparator
to only affect sort order.
Razvan
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post
Here's the source for sorted-set-by:
(defn sorted-set-by
Returns a new sorted set with supplied keys, using the supplied
comparator.
([comparator keys]
(clojure.lang.PersistentTreeSet/create comparator keys)))
This is because your comparator is saying that everything is equal
It doesn't seem confusing to me. You are taking complete control of
the set's local notion of ordering and equality. This is what I'd
expect.
Here's an example. First, a handy little function from Haskell:
(defn on [key f]
#(apply f (map key %)))
Then:
user (sorted-set-by (on :id compare
Why should sorting be related to the primary key? You should be able
to sort on any attribute. If you wanted to sort a set of people by age
would it make sense to only retain one person of each age? Sort order
and identity should be orthogonal. Besides, if you need a collection
based on primary
Maybe the example was poorly picked but the point stands: if you're
asking for a sorted set based on a comparator, you should expect
duplicate elements as dictated by comparator to be eliminated. If you
wanted to sort a set of people by age, you wouldn't use a sorted set
but a sorted sequence
I think the fns you're interested in are sort and sort-by, not sorted-
set-by.
I know you might not like it, but there is a convention in JavaLand
that a comparator value of 0 is identical in a sorted collection.
This causes orthogonal concepts of order identity to be entwined.
Sean
On Apr 16
CORRECTION, DON'T SHOOT!!
I should have order value, not order identity.
On Apr 16, 1:01 pm, Sean Devlin francoisdev...@gmail.com wrote:
I think the fns you're interested in are sort and sort-by, not sorted-
set-by.
I know you might not like it, but there is a convention in JavaLand
On Sat, Apr 17, 2010 at 12:01 AM, Sean Devlin francoisdev...@gmail.com wrote:
I know you might not like it, but there is a convention in JavaLand
that a comparator value of 0 is identical in a sorted collection.
It's not a Java convention. It's intrinsic to the business of sorting.
For sorting
The problem is you're unnecessarily conflating the value by which to
base a sort (x and y in your example) with the elements of the set.
Equality and ordinality are not the same thing. It should be
perfectly reasonable to hand someone a set of unique objects sorted by
some non-unique attribute of
Equality and ordinality are not the same thing. It should be
perfectly reasonable to hand someone a set of unique objects sorted by
some non-unique attribute of the elements of the set.
Sure, it's perfectly reasonable but it implies a different data
structure. It sounds like you want a data
On Sat, Apr 17, 2010 at 1:05 AM, Per Vognsen per.vogn...@gmail.com wrote:
You can only do slow
linear-time lookups by traversing the tree's nodes exhaustively.
Correction: You do not have to do a linear search on the whole tree
but only on the subset of the tree for which the comparator returns
On Apr 16, 8:25 am, Razvan gigi.clan...@gmail.com wrote:
Hi,
Is this the intended behavior?
(sorted-set-by (constantly 0) 1 2 3 4) returns #{1}.
I would expect the set to contain all the elements, and the comparator
to only affect sort order.
I expected the same thing at first
Hi all,
When creating a sorted set, the function sorted-set-by
cannot be called without at least one element, despite
the elements being optional.
Chouser on irc.freenode.net #clojure suggested this is a bug, because:
20:26 chouser leafw: sorted-set-by allows for an empty list of initial
On Nov 13, 6:42 am, Albert Cardona sapri...@gmail.com wrote:
20:26 chouser leafw: sorted-set-by allows for an empty list of initial
values, but passes that on to PersistentTreeSet/create as
null, but create doesn't check for null it just blindly calls
Hi hjlee,
there is already a filed issue and a patch from Timothy Pratley which
adds sorted-set-by to clojure:
http://code.google.com/p/clojure/issues/detail?id=76colspec=ID%20Type%20Status%20Priority%20Reporter%20Owner%20Summary
But its priority is set tow low.
erik
On Wed, Mar 25, 2009 at 7:21 AM, hoeck i_am_wea...@kittymail.com wrote:
there is already a filed issue and a patch from Timothy Pratley which
adds sorted-set-by to clojure:
http://code.google.com/p/clojure/issues/detail?id=76colspec=ID%20Type%20Status%20Priority%20Reporter%20Owner%20Summary
Hi, all.
I'm experimenting clojure.
Some code, I needed something like sorted-set-by, but no such
thing.
So i used sorted-map-by ignoring value part.
Is it deliberate for some reason?
Or just not there?
--~--~-~--~~~---~--~~
You received this message because you
Hi all.
I'm experimenting cljoure.
some code, i needed something like sorted-set-by. but, no such
thing, so I emulated that using sorted-map-by.
Is it deliberate for some reason?
or just not there?
--~--~-~--~~~---~--~~
You received this message because you
Hi, all.
I'm experimenting clojure.
Some code, I needed something like sorted-set-by, but no such
thing.
So i used sorted-map-by ignoring value part.
Is it deliberate for some reason?
Or just not there?
--~--~-~--~~~---~--~~
You received this message because you
Hi, all.
I don't know why my previous posts ignored.
spam filtering? so i changed title.
I'm experimenting clojure.
Some code, I needed something like sorted-set-by, but no such
thing.
So i used sorted-map-by ignoring value part.
Is it deliberate for some reason?
Or just
Hello!
Yes, you are right, sorted-set-by is missing. Good news is - it is
on the way :-)
See issue 76:
http://code.google.com/p/clojure/issues/detail?id=76
Frantisek
On Mar 24, 5:35 am, hjlee hj.d@gmail.com wrote:
Hi, all.
I don't know why my previous posts ignored.
spam filtering? so
sorted-set works for vectors, but doesn't work for lists, maps and
sets:
user= (sorted-set [1 4])
#{[1 4]}
user= (sorted-set [1 4] [1 4])
#{[1 4]}
user= (sorted-set [4 1] [1 4])
#{[1 4] [4 1]}
user= (sorted-set '(1 4))
#{(1 4)}
user= (sorted-set '(1 4) '(1 4))
java.lang.ClassCastException
I'm pretty sure that sorted-set works only with values that are
instances of a class that implements Comparable.
user= (instance? Comparable [])
true
user= (instance? Comparable {})
false
user= (instance? Comparable ())
false
user=
On Feb 20, 2:21 pm, Frantisek Sodomka fsodo...@gmail.com wrote
It looks that it is more complicated than that:
user= (sorted-set () ())
#{()}
user= (sorted-set {} {})
#{{}}
user= (sorted-set #{} #{})
#{#{}}
Frantisek
On 20 Ún, 20:33, Vincent Foley vfo...@gmail.com wrote:
I'm pretty sure that sorted-set works only with values that are
instances of a class
It probably does an identical? call on a pair before calling
compare, for efficiency. In other words, it may work on non-
comparable types, but only when passed n instances of the exact same
object:
user (sorted-set '(1) '(1))
; Exception
user (let [x '(1)] (sorted-set x x))
#{(1
object:
user (sorted-set '(1) '(1))
; Exception
user (let [x '(1)] (sorted-set x x))
#{(1)}
This is almost certainly not documented behavior and should not be
relied upon.
Cheers,
Jason
On Feb 20, 11:42 am, Frantisek Sodomka fsodo...@gmail.com wrote:
It looks that it is more
, but it would still beg the question as to where
to place the bounds, if other than exact match. For instance:
(count #{(hash-set 1) (sorted-set 1)}) === ???
they are both sets but not the same exact type.
Note that things are no different in Java, which also has polymorphic
collections
It is making more sense now.
One other interesting thing that surprised me is: There is not a
total ordering across types.
See discussion:
http://groups.google.com/group/clojure/browse_frm/thread/710848919c68981f/51ede18b2fd7ab96?lnk=gstq=sorted-set#51ede18b2fd7ab96
Therefore things like (sort
2]])
#{[] [1 2]}
user= (set [[] [1 2] 1])
#{[] 1 [1 2]}
user= (set [[] [1 2] ()])
#{[] [1 2]}
user= (set [() [] [1 2]])
#{() [1 2]}
What data types is sorted-set supposed to work on? When used with
different data types, it errors out:http://clojure.org/data_structures
user= (doc sorted
On Sat, Feb 14, 2009 at 7:19 PM, Stephen C. Gilardi squee...@mac.com wrote:
set is a hash set. It will never contain two items with equal hashes.
I don't think that's quite right. I don't think it matters in this
case, but hash values aren't guaranteed unique. A hash-map can have
two keys
Added a patch as issue 76 http://code.google.com/p/clojure/issues/detail?id=76
user= (sorted-set-by #( (:hat %1) (:hat %2)) {:hat 2} {:hat 3} {:hat
1})
#{{:hat 3} {:hat 2} {:hat 1}}
Yes, this is just an API gap. Issue/patch welcome.
--~--~-~--~~~---~--~~
You
On Feb 7, 2009, at 3:58 AM, puzzler wrote:
Still, I think it's a good point that since Clojure has sorted-map-by,
it seems logical to expect that it would also have sorted-set-by.
Yes, this is just an API gap. Issue/patch welcome.
Rich
On further reflection, perhaps the best approach would use sorted-map:
(defstruct example :msg :order)
(def a (struct there 2))
(def b (struct hi 1))
(def c (struct everyone 3))
We want our map to be sorted on the :order key. So:
(sorted-map (:order a) a (:order b) b (:order c) c)
I could
On further reflection, perhaps the best approach would use sorted-map:
Just curious, does the key need to be in the struct? (seeing you'll
get key-value pairs anyhow if you use first/last etc - the info will
still be there)
If you do need the key in both places, perhaps something like this
Just curious, does the key need to be in the struct? (seeing you'll
get key-value pairs anyhow if you use first/last etc - the info will
still be there)
Excellent point! Given that I'll be using a sorted-map now, I don't
even need the structmap!
Thanks for the code...I like what you did
79 matches
Mail list logo