-
Notifications
You must be signed in to change notification settings - Fork 4
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
Shaked iterator #9
Conversation
} | ||
|
||
/** | ||
* @return the last element if not empty. | ||
*/ | ||
default Optional<A> lastOptional() { | ||
throw new NotImplementedException(); | ||
if (!this.hasNext()) { |
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.
nice
|
||
return lastElement; | ||
} catch (Exception exception) { | ||
throw new NoSuchElementException(); |
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.
Throwing new exception is redundant here because calling next
on empty iterator throws NoSuchElementException
.
Moreover it loses the data from the original exception. The exception should be called with something like:
new NoSuchElementExpcetion(exception)
@@ -124,8 +172,24 @@ default boolean sameElements(Iterator<A> that) { | |||
* @return an iterator that contains only this element | |||
*/ | |||
static <A> RichIterator<A> pure(A elem) { | |||
// TODO: implement this method | |||
throw new NotImplementedException(); | |||
return new RichIterator<A>() { |
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.
👍
throw new NotImplementedException(); | ||
try { | ||
|
||
return Collections.max(this.toList(), comparator); |
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.
This should be implemented without external libraries.
|
||
return Collections.max(this.toList(), comparator); | ||
} catch (Exception exception) { | ||
throw new NoSuchElementException(); |
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.
Same comment on the exception as before
return new RichIterator<B>() { | ||
@Override | ||
public boolean hasNext() { | ||
return RichIterator.this.hasNext(); | ||
} | ||
|
||
@Override | ||
public B next() { | ||
return f.apply(RichIterator.this.next()); | ||
} | ||
}; |
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.
great
@@ -236,7 +357,30 @@ default String mkString() { | |||
* RichIterator.apply(1,2,3,4).append(5) // 1,2,3,4,5 | |||
*/ | |||
default RichIterator<A> append(A elem) { | |||
throw new NotImplementedException(); | |||
return new RichIterator<A>() { |
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.
Can be reduced to
return appendAll(pure(elem))
@@ -260,7 +424,29 @@ default RichIterator<A> appendAll(Iterator<A> elems) { | |||
* RichIterator.apply(1,2,3,4).prepend(0) // 0,1,2,3,4 | |||
*/ | |||
default RichIterator<A> prepend(A elem) { | |||
throw new NotImplementedException(); |
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.
Can be reduced to
pure(elem).appendAll(this)
@@ -272,7 +458,26 @@ default RichIterator<A> prepend(A elem) { | |||
* RichIterator.apply(4,5,6,7).prependAll(Arrays.asList(1,2,3)) // 1,2,3,4,5,6,7 | |||
*/ | |||
default RichIterator<A> prependAll(Iterator<A> elems) { | |||
throw new NotImplementedException(); | |||
return new RichIterator<A>() { |
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.
from(elems).appendAll(this)
default RichIterator<A> distinct() { | ||
throw new NotImplementedException(); | ||
return new RichIterator<A>() { |
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.
Alternative solution
Set<A> visited = new HashSet<>();
return this.filter(x -> !visited.contains(x)).tapEach(visited::add);
No description provided.