-
Notifications
You must be signed in to change notification settings - Fork 253
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
Add skipLast(int) #156
base: master
Are you sure you want to change the base?
Add skipLast(int) #156
Conversation
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.
In general I'm not against adding such feature, but the implementation is quite poor, sorry. If you still like to improve it, you may use poor-man parallelism for general case (via AbstractSpliterator and friends), though it's possible to do better, but I expect to see SIZED/sequential and SIZED/SUBSIZED/parallel cases to be handled specially (probably via separate spliterators). Thank you.
|
||
@Override | ||
public Spliterator<T> trySplit() { | ||
return null; |
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.
Ignoring parallelism completely is not an option to my library.
BlockingDeque<T> buffer = new LinkedBlockingDeque<>(n + 1); | ||
Spliterator<T> source = this.spliterator(); | ||
|
||
return supply(StreamEx.of(new Spliterator<T>() { |
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.
Creating anonymous class you capture the outer this
which is unnecessary.
|
||
@Override | ||
public long estimateSize() { | ||
return source.estimateSize() - n; |
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 could return negative size here which has no meaning.
|
||
@Override | ||
public int characteristics() { | ||
return source.characteristics(); |
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 don't delegate comparator in case if original spliterator is sorted.
if (n == 0) | ||
return supply(this); | ||
|
||
BlockingDeque<T> buffer = new LinkedBlockingDeque<>(n + 1); |
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.
Note that an optimized solution could be provided if the stream is SIZED/sequential or SIZED/SUBSIZED/parallel. In this case you don't need buffering at all as you know exactly how many elements are left. Also why concurrent buffer if you just refuse to parallelize? ArrayDeque (or simply Object[] array) would suffice and would be much faster.
public int characteristics() { | ||
return source.characteristics(); | ||
} | ||
})).sequential(); |
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.
A forEachRemaining
method must be provided as it optimizes the common case (non-short-circuiting stream). In specific cases it could be much faster and memory friendly than tryAdvance
.
if (n == 0) | ||
return this; | ||
|
||
BlockingDeque<Double> buffer = new LinkedBlockingDeque<>(n + 1); |
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.
Unnecessary boxing is absolutely not an option. You could just create an array of n elements.
Adds a new method that is the natural counterpart to
skip(long)