clojure.lang.RT.count checks whether the object being counted implements Counted, and only calls .count on it if so. IPersistentCollection does not implement Counted, so it calls (seq) on your object and walks over it to get the count. You return (mapcat ...), when asked for a seq, which is fully lazy and thus non- nil even for an empty bag. You can fix the immediate problem by adjusting the seq function to return (seq (mapcat ...)): in my REPL this causes (count (bag)) to return 0 instead of 1.
You could also make Bag derive Counted, so that your .count method will be called to save some time. On Mar 14, 12:49 am, Ken Wesson <kwess...@gmail.com> wrote: > > Well, except that count and empty are broken for some reason: > > > user=> (.count (Bag. {} 0)) > > 0 > > user=> (count (Bag. {} 0)) > > 1 > > > I don't understand what's causing this, but empty bags are always > > returning a count of 1 (and false from empty?) although the .count > > method correctly returns zero. I've traced the problem as far as the > > Java method clojure.lang.RT/count but no farther at this time. > > Argh. This same bug causes union to blow up in the case of (union (bag > 1) (bag)). It works with (union #{1} #{}), by contrast. It traces back > to empty? wrongly returning true, and thus to count wrongly returning > 1 even though .count correctly returns 0. -- 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