-
-
Notifications
You must be signed in to change notification settings - Fork 639
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
some TODOs in Iterator fixed #487 #543
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -715,7 +715,104 @@ public Long next() { | |
} | ||
} | ||
|
||
// TODO: add static factory methods similar to Stream.from, Stream.gen, ... | ||
/** | ||
* Returns an infinitely iterator of int values starting from {@code from}. | ||
* <p> | ||
* The {@code Iterator} extends to {@code Integer.MIN_VALUE} when passing {@code Integer.MAX_VALUE}. | ||
* | ||
* @param value a start int value | ||
* @return a new {@code Iterator} of int values starting from {@code from} | ||
*/ | ||
static Iterator<Integer> from(int value) { | ||
return new AbstractIterator<Integer>() { | ||
private int next = value; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Integer next() { | ||
return next++; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Returns an infinitely iterator of long values starting from {@code from}. | ||
* <p> | ||
* The {@code Iterator} extends to {@code Long.MIN_VALUE} when passing {@code Long.MAX_VALUE}. | ||
* | ||
* @param value a start long value | ||
* @return a new {@code Iterator} of long values starting from {@code from} | ||
*/ | ||
static Iterator<Long> from(long value) { | ||
return new AbstractIterator<Long>() { | ||
private long next = value; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Long next() { | ||
return next++; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Generates an infinitely iterator using a value Supplier. | ||
* | ||
* @param supplier A Supplier of iterator values | ||
* @param <T> value type | ||
* @return A new {@code Iterator} | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
static <T> Iterator<T> gen(Supplier<? extends T> supplier) { | ||
Objects.requireNonNull(supplier, "supplier is null"); | ||
return new AbstractIterator<T>() { | ||
@Override | ||
public boolean hasNext() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
return supplier.get(); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Generates an infinitely iterator using a function to calculate the next value | ||
* based on the previous. | ||
* | ||
* @param seed The first value in the iterator | ||
* @param f A function to calculate the next value based on the previous | ||
* @param <T> value type | ||
* @return A new {@code Iterator} | ||
*/ | ||
static <T> Iterator<T> gen(T seed, Function<? super T, ? extends T> f) { | ||
Objects.requireNonNull(f, "f is null"); | ||
return new AbstractIterator<T>() { | ||
T next = seed; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
T result = next; | ||
next = f.apply(next); | ||
return result; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
default Iterator<T> clear() { | ||
|
@@ -1095,8 +1192,14 @@ public U next() { | |
@Override | ||
default Tuple2<Iterator<T>, Iterator<T>> partition(Predicate<? super T> predicate) { | ||
Objects.requireNonNull(predicate, "predicate is null"); | ||
// TODO | ||
throw new UnsupportedOperationException("TODO"); | ||
if (!hasNext()) { | ||
return Tuple.of(empty(), empty()); | ||
} else { | ||
final Stream<T> that = Stream.ofAll(this); | ||
final Iterator<T> first = that.iterator().filter(predicate); | ||
final Iterator<T> second = that.iterator().filter(predicate.negate()); | ||
return Tuple.of(first, second); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -1125,11 +1228,26 @@ public T next() { | |
} | ||
} | ||
|
||
@Override | ||
default T reduceLeft(BiFunction<? super T, ? super T, ? extends T> op) { | ||
Objects.requireNonNull(op, "op is null"); | ||
if (isEmpty()) { | ||
throw new NoSuchElementException("reduceLeft on Nil"); | ||
} else { | ||
Stream<T> stream = Stream.ofAll(this); | ||
return stream.tail().foldLeft(stream.head(), op::apply); | ||
} | ||
} | ||
|
||
@Override | ||
default T reduceRight(BiFunction<? super T, ? super T, ? extends T> op) { | ||
Objects.requireNonNull(op, "op is null"); | ||
// TODO | ||
throw new UnsupportedOperationException("TODO"); | ||
if (isEmpty()) { | ||
throw new NoSuchElementException("reduceLeft on Nil"); | ||
} else { | ||
Stream<T> reversed = Stream.ofAll(this).reverse(); | ||
return reversed.tail().foldLeft(reversed.head(), (xs, x) -> op.apply(x, xs)); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -1334,8 +1452,35 @@ public T next() { | |
|
||
@Override | ||
default Iterator<T> takeRight(int n) { | ||
// TODO | ||
throw new UnsupportedOperationException("TODO"); | ||
if (n <= 0) { | ||
return empty(); | ||
} else { | ||
final Iterator<T> that = this; | ||
return new Iterator<T>() { | ||
private Queue<T> queue = Queue.empty(); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
while (that.hasNext()) { | ||
queue = queue.append(that.next()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call |
||
if(queue.length() > n) { | ||
queue = queue.dequeue()._2; | ||
} | ||
} | ||
return queue.length() > 0; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (!hasNext()) { | ||
EMPTY.next(); | ||
} | ||
final Tuple2<T, Queue<T>> t = queue.dequeue(); | ||
queue = t._2; | ||
return t._1; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
@Override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!!