Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement countByEach on RichIterable. #476

Closed
donraab opened this issue Mar 8, 2018 · 7 comments
Closed

Implement countByEach on RichIterable. #476

donraab opened this issue Mar 8, 2018 · 7 comments

Comments

@donraab
Copy link
Contributor

donraab commented Mar 8, 2018

This can be implemented as a default method on RichIterable as countBy was. It will need appropriate co-variant overrides as default methods and optimizations for Bag implementations.

The signature of countByEach should be:

default <V> Bag<V> countByEach(Function<? super T, ? extends Iterable<V>> function)

The naming is based on the pattern we used for groupBy/groupByEach.

This code in the Pet Kata can then be improved.

Before:

MutableList<Person> people = ...;
MutableList<PetType> petTypes = people.flatCollect(Person::getPetTypes);
Bag<PetType> petTypeCounts = petTypes.toBag();
int cats = petTypeCounts.occurrencesOf(PetType.CAT);

After:

MutableList<Person> people = ...;
Bag<PetType> petTypeCounts = people.countByEach(Person::getPetTypes);
int cats = petTypeCounts.occurrencesOf(PetType.CAT);

http://eclipse.github.io/eclipse-collections-kata/pet-kata/#/6/2

@donraab donraab changed the title Implement countByEach on RichIterable Implement countByEach on RichIterable. Mar 8, 2018
@TheLoneKing
Copy link
Contributor

Should the implementation be lazy?

@nikhilnanivadekar
Copy link
Contributor

Yes similar to countBy()
countBy(): this.asLazy().<V>collect(function).toBag();

countByEach(): this.asLazy().<V>flatCollect(function).toBag();

@TheLoneKing
Copy link
Contributor

the test case for this is failing with the error No iteration patterns should delegate to iterator().

Failed tests:
UnifiedMapNoIteratorTest.RichIterable_countByEach No iteration patterns should delegate to iterator()
UnifiedMapWithHashingStrategyNoIteratorTest.RichIterable_countByEach No iteration patterns should delegate to iterator()

My test case is here: https://github.com/TheLoneKing/eclipse-collections/blob/0dd3ca235272c9f9dc023ab9f90afee69c892b69/unit-tests-java8/src/test/java/org/eclipse/collections/test/RichIterableTestCase.java#L1626

Can someone please help me with the cause of this error?

@nikhilnanivadekar
Copy link
Contributor

@TheLoneKing you are missing overrides for the countByEach() implementation. Take a look at countBy() for the overrides that are present.
I have simplified the test case and fixed it for Map here: nikhilnanivadekar@6f9ea99
Take a look and let me know if you have any questions.

Eclipse Collections Maps and Sets are backed by flattened arrays so iterating over them using an iterator() is inefficient.

TheLoneKing added a commit to TheLoneKing/eclipse-collections that referenced this issue Apr 4, 2018
Signed-off-by: TheLoneKing <dkumar.kovai@gmail.com>
TheLoneKing added a commit to TheLoneKing/eclipse-collections that referenced this issue May 21, 2018
Signed-off-by: TheLoneKing <dkumar.kovai@gmail.com>
TheLoneKing added a commit to TheLoneKing/eclipse-collections that referenced this issue Jun 2, 2018
Signed-off-by: TheLoneKing <dkumar.kovai@gmail.com>
TheLoneKing added a commit to TheLoneKing/eclipse-collections that referenced this issue Jun 25, 2018
Signed-off-by: TheLoneKing <dkumar.kovai@gmail.com>
TheLoneKing added a commit to TheLoneKing/eclipse-collections that referenced this issue Jul 15, 2018
Signed-off-by: TheLoneKing <dkumar.kovai@gmail.com>
@joshuacheng
Copy link

Working on this at UIUC HackNight.

@joshuakim0724
Copy link

I'll help you work on this @joshuacheng

@brianwang1217
Copy link

me too @joshuacheng @joshuakim0724

nikhilnanivadekar added a commit that referenced this issue Nov 23, 2018
Implement countByEach on RichIterable. Resolves #476
TheLoneKing added a commit to TheLoneKing/eclipse-collections that referenced this issue Jul 15, 2019
Signed-off-by: TheLoneKing <dkumar.kovai@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants