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

Shaked iterator #9

Closed

Conversation

JackSparrow4747
Copy link

No description provided.

}

/**
* @return the last element if not empty.
*/
default Optional<A> lastOptional() {
throw new NotImplementedException();
if (!this.hasNext()) {
Copy link
Owner

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

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

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

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

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

Comment on lines +235 to +245
return new RichIterator<B>() {
@Override
public boolean hasNext() {
return RichIterator.this.hasNext();
}

@Override
public B next() {
return f.apply(RichIterator.this.next());
}
};
Copy link
Owner

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

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

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

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

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);

@guyeise5 guyeise5 closed this Feb 3, 2024
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.

2 participants