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

Reimplemented Collections.scan methods #1684

Merged
merged 4 commits into from
Nov 23, 2016

Conversation

ashrwin
Copy link
Member

@ashrwin ashrwin commented Nov 17, 2016

PR for #1610

@codecov-io
Copy link

codecov-io commented Nov 17, 2016

Current coverage is 97.01% (diff: 100%)

Merging #1684 into master will decrease coverage by <.01%

@@             master      #1684   diff @@
==========================================
  Files            89         89          
  Lines         11264      11243    -21   
  Methods           0          0          
  Messages          0          0          
  Branches       1878       1877     -1   
==========================================
- Hits          10928      10907    -21   
  Misses          196        196          
  Partials        140        140          

Powered by Codecov. Last update 2625b10...c8c3ef5

Copy link
Contributor

@danieldietrich danieldietrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ashrko619,
thank you, well done - nice and clean!
Because we are already on this topic, could you please change the method I mentioned?
Many thanks!
Daniel

@@ -809,13 +809,13 @@ public CharSeq reverse() {
@Override
public <U> IndexedSeq<U> scanLeft(U zero, BiFunction<? super U, ? super Character, ? extends U> operation) {
Objects.requireNonNull(operation, "operation is null");
return Collections.scanLeft(this, zero, operation, Vector.empty(), Vector::append, Function.identity());
return Collections.scanLeft(this, zero, operation, Iterator::toVector);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've recognized one thing that is outside (but related) to this PR: CharSeq.scan() currently returns IndexedSeq<Character> but it should return CharSeq.

Could you please change the return type of scan() to CharSeq?

@Override
public IndexedSeq<Character> scan(Character zero, BiFunction<? super Character, ? super Character, ? extends Character> operation) {
    return Collections.scanLeft(this, zero, operation, Iterator::toCharSeq);
}

}

@Override
public <U> PriorityQueue<U> scanLeft(U zero, BiFunction<? super U, ? super T, ? extends U> operation) {
Objects.requireNonNull(operation, "operation is null");
return Collections.scanLeft(this, zero, operation, empty(naturalComparator()), PriorityQueue::enqueue, Function.identity());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: unnecessary newline

@@ -803,7 +803,7 @@ public CharSeq reverse() {

@Override
public IndexedSeq<Character> scan(Character zero, BiFunction<? super Character, ? super Character, ? extends Character> operation) {
return scanLeft(zero, operation);
return Collections.scanLeft(this, zero, operation, Iterator::toCharSeq);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, that was fast :-)
Can the method return CharSeq instead of IndexedSeq<Character>?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danieldietrich: Ran into a bug, have opened an issue #1685

@danieldietrich
Copy link
Contributor

@ashrko619 because of ongoing PRs the branch conflicts need to be resolved. With your fix of #1685 the Iterator.toCharSeq method should work now.

As soon as you merged your local branch I will pull this PR! Thank you :)

final Seq<? extends T> result = (iterable instanceof Seq) ? (Seq<T>) iterable
: Vector.ofAll(iterable);
return result.reverseIterator();
return Iterator.ofAll(iterable).foldLeft(List.<T>empty(), List::prepend).iterator();
Copy link
Contributor

@l0rinc l0rinc Nov 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use a stack here instead of a Vector (which would require a size hint).
I suggest you use pushAll instead of foldLeft:

List.<T> empty().pushAll(iterable).iterator();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use a stack here instead of a Vector (which would require a size hint).
Can you explain ? 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're using a stack's iterator here (i.e. List is a Stack), which is basically a reverse iterator.
It's a better solution to the problem, than what I've written previously, i.e. create a Vector from it and traverse it backwards.

Objects.requireNonNull(operation, "operation is null");
return Collections.scanLeft(map, zero, operation, emptySupplier.get(), Maps::put, Function.identity());
return (M) Collections.scanLeft(map, zero, operation, finisher);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, why wasn't a cast needed before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am also not sure why it wasn't needed 🤔

@@ -779,20 +779,17 @@ public int length() {
@Override
public TreeSet<T> scan(T zero, BiFunction<? super T, ? super T, ? extends T> operation) {
Objects.requireNonNull(operation, "operation is null");
return Collections.scanLeft(this, zero, operation, TreeSet.empty(comparator()), TreeSet::add, Function.identity());
return Collections.scanLeft(this, zero, operation, it -> TreeSet.ofAll(comparator(), it));
Copy link
Contributor

@l0rinc l0rinc Nov 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it = iterator?
if you want to use an abbreviation, please consider using iter, it's more commonly used for iterator

static <T, U, R extends Traversable<U>> R scanRight(Traversable<? extends T> traversable,
U zero, BiFunction<? super T, ? super U, ? extends U> operation, Function<Iterator<U>, R> finisher) {

final Iterator<? extends T> reversedElements = reverseIterator(traversable.iterator());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for using the new reverseIterator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the .iterator() exactly, reverseIterator(traversable) should work also?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right

@l0rinc
Copy link
Contributor

l0rinc commented Nov 20, 2016

  • To make this mergeable, please rebase on master
  • Only push, once all tests are passing
  • Commits should contain coherent units of work. Please reorder you commits logically, not incidentally, i.e. if you've made a mistake in your previous commit, edit that one and force push, instead of creating many altering commits - it helps the reviewer.

* @return a new map with the same keySet but transformed values.
*/
Map<K, V> replaceAll(BiFunction<? super K, ? super V, ? extends V> function);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged the changes but am not sure why these are added as part of this commit 🙈

Copy link
Contributor

@danieldietrich danieldietrich Nov 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmhh, today I did a push --force on a branch/PR that wasn't merged yet. I think something went wrong o_O.

Don't know how this can be repaired 🙈 However, the current master contains these lines already: https://github.com/javaslang/javaslang/blob/master/javaslang/src/main/java/javaslang/collection/Map.java#L677-L703

@paplorinc is there some git magic that can be applied here? E.g. rebasing / rewriting this PR ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashrko619 don't merge, rebase :)
From Idea you can do that from the VCS -> Git -> rebase my github fork, and solve the conflicts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to rebase there are no conflicts now 😕 Should I rollback to an older commit and try ?

@danieldietrich
Copy link
Contributor

Well done @ashrko619, many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants