Skip to content

Commit

Permalink
Iterator#iterate based on comments from @danieldietrich and @paplorinc
Browse files Browse the repository at this point in the history
  • Loading branch information
jorander committed Jul 9, 2016
1 parent 3d76301 commit f369a5c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions javaslang/src/main/java/javaslang/collection/Iterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,11 @@ public T getNext() {
static <T> Iterator<T> iterate(T seed, Function<? super T, ? extends T> f) {
Objects.requireNonNull(f, "f is null");
return new AbstractIterator<T>() {
Function<? super T, ? extends T> nextFunc = (s) -> seed;
T previous = null;
Function<? super T, ? extends T> nextFunc = s -> {
nextFunc = f;
return seed;
};
T current = null;

@Override
public boolean hasNext() {
Expand All @@ -978,9 +981,8 @@ public boolean hasNext() {

@Override
public T getNext() {
previous = nextFunc.apply(previous);
nextFunc = f;
return previous;
current = nextFunc.apply(current);
return current;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void shouldGenerateInfiniteStreamBasedOnSupplierWithAccessToPreviousValue

@Test
public void shouldNotCallSupplierUntilNecessary() {
assertThat(iterate(2, (i) -> {throw new RuntimeException("The supplier function should not be called since the next value is not needed in order to get head.");}).head()).isEqualTo(2);
assertThat(iterate(2, (i) -> {throw new RuntimeException();}).head()).isEqualTo(2);
}

// ++++++ OBJECT ++++++
Expand Down

0 comments on commit f369a5c

Please sign in to comment.