groovy

sum, min and friends should work on the same types that collect, each and their friends work on.

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.5.1
  • Fix Version/s: 1.5.5, 1.6-beta-1
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    1

Description

[1,2,3].sum() works nicely.

But [1,2,3].iterator().sum() fails.

In contrast, this code

[1,2,3].iterator().each {
println it
}

works just fine.

On aesthetic grounds, there shouldn't be any difference in what works and what doesn't. More importantly, there are situations where an iterator is available (due to pre-existing API's) but a collection is not.

Activity

Hide
Ted Dunning added a comment -


Here are some simple test cases for this enhancement.

Show
Ted Dunning added a comment - Here are some simple test cases for this enhancement.
Hide
Paul King added a comment - - edited

The collect method already works with an iterable object.

The workaround for the other methods is simply to call toList() as follows:

assert ([5,1,2,3,3].iterator().toList().sum() == [5,1,2,3,3].sum())
assert ([5,1,2,3,3].iterator().toList().min() == [5,1,2,3,3].min())
assert ([5,1,2,3,3].iterator().toList().max() == [5,1,2,3,3].max())
assert ([5,1,2,3,3].iterator().toList().sort() == [5,1,2,3,3].sort())
assert ([5,1,2,3,3].iterator().toList().unique() == [5,1,2,3,3].unique())

It doesn't make much sense here (because you need neither the iterator() nor toList() calls) but also works for non-collections with an iterator() method.

You are correct that it would be nice to have all of these available for Iterator or for that matter any iterable object. The most general solution would be to change the three min, three max, three unique, four sort and six sum methods to work on Object rather than Collection and in the code call InvokerHelper.asIterator(self) instead of self.iterator() in the appropriate place. The advantage of this is you wouldn't need to call the iterator() method at all just sum() on any object that supported iterator(). The downside from an implementation point of view is that it clutters up the available methods namespace on Object slowing down all calls. I guess the compromise would be to implement all the variations of all the methods mentioned above for Iterator. This isn't the same thing though as you can always leave out the iterator() call with each and collect.

Show
Paul King added a comment - - edited The collect method already works with an iterable object. The workaround for the other methods is simply to call toList() as follows:
assert ([5,1,2,3,3].iterator().toList().sum() == [5,1,2,3,3].sum())
assert ([5,1,2,3,3].iterator().toList().min() == [5,1,2,3,3].min())
assert ([5,1,2,3,3].iterator().toList().max() == [5,1,2,3,3].max())
assert ([5,1,2,3,3].iterator().toList().sort() == [5,1,2,3,3].sort())
assert ([5,1,2,3,3].iterator().toList().unique() == [5,1,2,3,3].unique())
It doesn't make much sense here (because you need neither the iterator() nor toList() calls) but also works for non-collections with an iterator() method. You are correct that it would be nice to have all of these available for Iterator or for that matter any iterable object. The most general solution would be to change the three min, three max, three unique, four sort and six sum methods to work on Object rather than Collection and in the code call InvokerHelper.asIterator(self) instead of self.iterator() in the appropriate place. The advantage of this is you wouldn't need to call the iterator() method at all just sum() on any object that supported iterator(). The downside from an implementation point of view is that it clutters up the available methods namespace on Object slowing down all calls. I guess the compromise would be to implement all the variations of all the methods mentioned above for Iterator. This isn't the same thing though as you can always leave out the iterator() call with each and collect.
Hide
Paul King added a comment -

Added 3 min, 3 max, 3 sort, 3 unique and 2 sum methods for Iterator plus size() and reverse() for Iterator.

Show
Paul King added a comment - Added 3 min, 3 max, 3 sort, 3 unique and 2 sum methods for Iterator plus size() and reverse() for Iterator.
Hide
Paul King added a comment -

While working on PLEAC and langref.org I also noticed a couple of other inconsistencies which I have added:
count and join on iterator
head, tail, first, last, push on lists
reverse on arrays

Show
Paul King added a comment - While working on PLEAC and langref.org I also noticed a couple of other inconsistencies which I have added: count and join on iterator head, tail, first, last, push on lists reverse on arrays

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: