Skip to content

Commit

Permalink
Merge pull request vavr-io#1148 from mvh77/master
Browse files Browse the repository at this point in the history
renamed gen + repeat methods to iterate and continually in Iterator and Stream
  • Loading branch information
danieldietrich committed Feb 27, 2016
2 parents 64e4e28 + 1df4725 commit 056aa27
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 49 deletions.
2 changes: 1 addition & 1 deletion javaslang-test/src/main/java/javaslang/test/Gen.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static <T> Gen<T> of(T t) {
}

static <T> Gen<T> of(T seed, Function<? super T, ? extends T> next) {
final Iterator<T> iterator = Stream.gen(seed, next).iterator();
final Iterator<T> iterator = Stream.iterate(seed, next).iterator();
return ignored -> iterator.next();
}

Expand Down
2 changes: 1 addition & 1 deletion javaslang-test/src/test/java/javaslang/test/GenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public int nextInt(int bound) {
}
};
final Gen<Integer> gen = Gen.choose(1, 2);
final Number actual = Stream.gen(() -> gen.apply(rng)).take(10).sum();
final Number actual = Stream.continually(() -> gen.apply(rng)).take(10).sum();
assertThat(actual).isEqualTo(10L);
}

Expand Down
2 changes: 1 addition & 1 deletion javaslang/src/main/java/javaslang/collection/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ public Array<T> padTo(int length, T element) {
if (length <= length()) {
return this;
} else {
return appendAll(Iterator.gen(() -> element).take(length - length()));
return appendAll(Iterator.continually(element).take(length - length()));
}
}

Expand Down
70 changes: 62 additions & 8 deletions javaslang/src/main/java/javaslang/collection/Iterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ public Long getNext() {
}

/**
* Returns an infinitely iterator of int values starting from {@code from}.
* Returns an infinite iterator of int values starting from {@code value}.
* <p>
* The {@code Iterator} extends to {@code Integer.MIN_VALUE} when passing {@code Integer.MAX_VALUE}.
*
Expand All @@ -866,7 +866,34 @@ public Integer getNext() {
}

/**
* Returns an infinitely iterator of long values starting from {@code from}.
* Returns an infinite iterator of int values starting from {@code value} and spaced by {@code step}.
* <p>
* The {@code Iterator} extends to {@code Integer.MIN_VALUE} when passing {@code Integer.MAX_VALUE}.
*
* @param value a start int value
* @param step the step by which to advance on each iteration
* @return a new {@code Iterator} of int values starting from {@code from}
*/
static Iterator<Integer> from(int value, int step) {
return new AbstractIterator<Integer>() {
private int next = value;

@Override
public boolean hasNext() {
return true;
}

@Override
public Integer getNext() {
int result = next;
next += step;
return result;
}
};
}

/**
* Returns an infinite iterator of long values starting from {@code value}.
* <p>
* The {@code Iterator} extends to {@code Long.MIN_VALUE} when passing {@code Long.MAX_VALUE}.
*
Expand All @@ -890,13 +917,40 @@ public Long getNext() {
}

/**
* Generates an infinitely iterator using a value Supplier.
* Returns an infinite iterator of long values starting from {@code value} and spaced by {@code step}.
* <p>
* The {@code Iterator} extends to {@code Long.MIN_VALUE} when passing {@code Long.MAX_VALUE}.
*
* @param value a start long value
* @param step the step by which to advance on each iteration
* @return a new {@code Iterator} of long values starting from {@code from}
*/
static Iterator<Long> from(long value, long step) {
return new AbstractIterator<Long>() {
private long next = value;

@Override
public boolean hasNext() {
return true;
}

@Override
public Long getNext() {
long result = next;
next += step;
return result;
}
};
}

/**
* Generates an infinite iterator using a value Supplier.
*
* @param supplier A Supplier of iterator values
* @param <T> value type
* @return A new {@code Iterator}
*/
static <T> Iterator<T> gen(Supplier<? extends T> supplier) {
static <T> Iterator<T> continually(Supplier<? extends T> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
return new AbstractIterator<T>() {
@Override
Expand All @@ -912,15 +966,15 @@ public T getNext() {
}

/**
* Generates an infinitely iterator using a function to calculate the next value
* Generates an infinite 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) {
static <T> Iterator<T> iterate(T seed, Function<? super T, ? extends T> f) {
Objects.requireNonNull(f, "f is null");
return new AbstractIterator<T>() {
T next = seed;
Expand All @@ -940,13 +994,13 @@ public T getNext() {
}

/**
* Repeats an element infinitely often.
* Creates an infinite iterator returning the given element.
*
* @param t An element
* @param <T> Element type
* @return A new Iterator containing infinite {@code t}'s.
*/
static <T> Iterator<T> repeat(T t) {
static <T> Iterator<T> continually(T t) {
return new AbstractIterator<T>() {
@Override
public boolean hasNext() {
Expand Down
2 changes: 1 addition & 1 deletion javaslang/src/main/java/javaslang/collection/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ default List<T> padTo(int length, T element) {
if (length <= length()) {
return this;
} else {
return appendAll(Iterator.gen(() -> element).take(length - length()));
return appendAll(Iterator.continually(element).take(length - length()));
}
}

Expand Down
26 changes: 13 additions & 13 deletions javaslang/src/main/java/javaslang/collection/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
*
* // generators
* Stream.cons(Object, Supplier) // e.g. Stream.cons(current, () -&gt; next(current));
* Stream.gen(Supplier) // e.g. Stream.gen(Math::random);
* Stream.gen(Object, Function) // e.g. Stream.gen(1, i -&gt; i * 2);
* Stream.continually(Supplier) // e.g. Stream.continually(Math::random);
* Stream.iterate(Object, Function)// e.g. Stream.iterate(1, i -&gt; i * 2);
* </code>
* </pre>
*
Expand Down Expand Up @@ -75,7 +75,7 @@
* <pre>
* <code>
* // = Stream(2L, 3L, 5L, 7L, ...)
* Stream.gen(2L, PrimeNumbers::nextPrimeFrom)
* Stream.iterate(2L, PrimeNumbers::nextPrimeFrom)
*
* // helpers
*
Expand Down Expand Up @@ -148,9 +148,9 @@ static Stream<Long> from(long value) {
* @param <T> value type
* @return A new Stream
*/
static <T> Stream<T> gen(Supplier<? extends T> supplier) {
static <T> Stream<T> continually(Supplier<? extends T> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
return Stream.ofAll(Iterator.gen(supplier));
return Stream.ofAll(Iterator.continually(supplier));
}

/**
Expand All @@ -162,9 +162,9 @@ static <T> Stream<T> gen(Supplier<? extends T> supplier) {
* @param <T> value type
* @return A new Stream
*/
static <T> Stream<T> gen(T seed, Function<? super T, ? extends T> f) {
static <T> Stream<T> iterate(T seed, Function<? super T, ? extends T> f) {
Objects.requireNonNull(f, "f is null");
return Stream.ofAll(Iterator.gen(seed, f));
return Stream.ofAll(Iterator.iterate(seed, f));
}

/**
Expand Down Expand Up @@ -597,8 +597,8 @@ static Stream<Long> rangeClosedBy(long from, long toInclusive, long step) {
* @param <T> Element type
* @return A new Stream containing infinite {@code t}'s.
*/
static <T> Stream<T> repeat(T t) {
return Stream.ofAll(Iterator.repeat(t));
static <T> Stream<T> continually(T t) {
return Stream.ofAll(Iterator.continually(t));
}

@Override
Expand Down Expand Up @@ -907,7 +907,7 @@ default Stream<T> padTo(int length, T element) {
if (length <= 0) {
return this;
} else if (isEmpty()) {
return Stream.ofAll(Iterator.gen(() -> element).take(length));
return Stream.ofAll(Iterator.continually(element).take(length));
} else {
return cons(head(), () -> tail().padTo(length - 1, element));
}
Expand Down Expand Up @@ -1338,7 +1338,7 @@ default Stream<Tuple2<T, Long>> zipWithIndex() {
* @return new {@code Stream} composed from this stream extended with a Stream of provided value
*/
default Stream<T> extend(T next) {
return Stream.ofAll(this.appendAll(Stream.repeat(next)));
return Stream.ofAll(this.appendAll(Stream.continually(next)));
}

/**
Expand All @@ -1349,7 +1349,7 @@ default Stream<T> extend(T next) {
*/
default Stream<T> extend(Supplier<? extends T> nextSupplier) {
Objects.requireNonNull(nextSupplier, "nextSupplier is null");
return Stream.ofAll(appendAll(Stream.gen(nextSupplier)));
return Stream.ofAll(appendAll(Stream.continually(nextSupplier)));
}

/**
Expand All @@ -1373,7 +1373,7 @@ default Stream<T> extend(Function<? super T, ? extends T> nextFunction) {
@Override
protected T getNext() {
if (stream.isEmpty()) {
stream = Stream.gen(nextFunction.apply(last), nextFunction);
stream = Stream.iterate(nextFunction.apply(last), nextFunction);
}
last = stream.head();
stream = stream.tail();
Expand Down
2 changes: 1 addition & 1 deletion javaslang/src/main/java/javaslang/collection/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public Vector<T> padTo(int length, T element) {
if (length <= length()) {
return this;
} else {
return appendAll(Iterator.gen(() -> element).take(length - length()));
return appendAll(Iterator.continually(element).take(length - length()));
}
}

Expand Down
18 changes: 14 additions & 4 deletions javaslang/src/test/java/javaslang/collection/IteratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ public void shouldGenerateIntStream() {
assertThat(Iterator.from(-1).take(3)).isEqualTo(Iterator.of(-1, 0, 1));
}

@Test
public void shouldGenerateIntStreamWithStep() {
assertThat(Iterator.from(-1, 6).take(3)).isEqualTo(Iterator.of(-1, 5, 11));
}

@Test
public void shouldGenerateTerminatingIntStream() {
//noinspection NumericOverflow
Expand All @@ -311,24 +316,29 @@ public void shouldGenerateLongStream() {
assertThat(Iterator.from(-1L).take(3)).isEqualTo(Iterator.of(-1L, 0L, 1L));
}

@Test
public void shouldGenerateLongStreamWithStep() {
assertThat(Iterator.from(-1L, 5L).take(3)).isEqualTo(Iterator.of(-1L, 4L, 9L));
}

@Test
public void shouldGenerateTerminatingLongStream() {
//noinspection NumericOverflow
assertThat(Iterator.from(Long.MAX_VALUE).take(2)).isEqualTo(Iterator.of(Long.MAX_VALUE, Long.MAX_VALUE + 1));
}

// -- static gen(Supplier)
// -- static continually(Supplier)

@Test
public void shouldGenerateInfiniteStreamBasedOnSupplier() {
assertThat(Iterator.gen(() -> 1).take(13).reduce((i, j) -> i + j)).isEqualTo(13);
assertThat(Iterator.continually(() -> 1).take(13).reduce((i, j) -> i + j)).isEqualTo(13);
}

// -- static gen(T, Function)
// -- static iterate(T, Function)

@Test
public void shouldGenerateInfiniteStreamBasedOnSupplierWithAccessToPreviousValue() {
assertThat(Iterator.gen(2, (i) -> i + 2).take(3).reduce((i, j) -> i + j)).isEqualTo(12);
assertThat(Iterator.iterate(2, (i) -> i + 2).take(3).reduce((i, j) -> i + j)).isEqualTo(12);
}

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

0 comments on commit 056aa27

Please sign in to comment.