-
-
Notifications
You must be signed in to change notification settings - Fork 631
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
Conversation
Current coverage is 97.01% (diff: 100%)@@ 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
|
There was a problem hiding this 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); |
There was a problem hiding this comment.
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()); | ||
|
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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>?
There was a problem hiding this comment.
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
@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(); |
There was a problem hiding this comment.
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();
There was a problem hiding this comment.
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 ? 😄
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's right
|
b6cc582
to
2548f3a
Compare
* @return a new map with the same keySet but transformed values. | ||
*/ | ||
Map<K, V> replaceAll(BiFunction<? super K, ? super V, ? extends V> function); | ||
|
There was a problem hiding this comment.
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 🙈
There was a problem hiding this comment.
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 ...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
2548f3a
to
c8c3ef5
Compare
Well done @ashrko619, many thanks! |
PR for #1610