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

Add skipLast(int) #156

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

epkugelmass
Copy link

Adds a new method that is the natural counterpart to skip(long)

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 98.444% when pulling b23a588 on epkugelmass:elan/skiplast into 59d5cee on amaembo:master.

Copy link
Owner

@amaembo amaembo left a 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;
Copy link
Owner

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>() {
Copy link
Owner

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;
Copy link
Owner

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();
Copy link
Owner

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);
Copy link
Owner

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();
Copy link
Owner

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);
Copy link
Owner

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.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 98.277% when pulling 2607371 on epkugelmass:elan/skiplast into 5fbb114 on amaembo:master.

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

Successfully merging this pull request may close these issues.

3 participants