classlib: added Stream iterate method (#721)

This commit is contained in:
Ivan Hetman 2023-07-05 20:40:08 +03:00 committed by GitHub
parent eed42e33b0
commit ac2e5d6fa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 63 additions and 0 deletions

View File

@ -145,6 +145,10 @@ public interface TDoubleStream extends TBaseStream<Double, TDoubleStream> {
return new TIterateDoubleStream(seed, f);
}
static TDoubleStream iterate(double seed, DoublePredicate pr, DoubleUnaryOperator f) {
return new TIterateDoubleStream(seed, pr, f);
}
static TDoubleStream generate(DoubleSupplier s) {
return new TGenerateDoubleStream(s);
}

View File

@ -151,6 +151,10 @@ public interface TIntStream extends TBaseStream<Integer, TIntStream> {
return new TIterateIntStream(seed, f);
}
static TIntStream iterate(int seed, IntPredicate pr, IntUnaryOperator f) {
return new TIterateIntStream(seed, pr, f);
}
static TIntStream generate(IntSupplier s) {
return new TGenerateIntStream(s);
}

View File

@ -149,6 +149,10 @@ public interface TLongStream extends TBaseStream<Long, TLongStream> {
return new TIterateLongStream(seed, f);
}
static TLongStream iterate(long seed, LongPredicate pr, LongUnaryOperator f) {
return new TIterateLongStream(seed, pr, f);
}
static TLongStream generate(LongSupplier s) {
return new TGenerateLongStream(s);
}

View File

@ -176,6 +176,10 @@ public interface TStream<T> extends TBaseStream<T, TStream<T>> {
return new TIterateStream<>(seed, f);
}
static <T> TStream<T> iterate(T seed, Predicate<? super T> pr, UnaryOperator<T> f) {
return new TIterateStream<>(seed, pr, f);
}
static <T> TStream<T> generate(Supplier<T> s) {
return new TGenerateStream<>(s);
}

View File

@ -20,16 +20,25 @@ import java.util.function.DoubleUnaryOperator;
public class TIterateDoubleStream extends TSimpleDoubleStreamImpl {
private double value;
private DoublePredicate pr;
private DoubleUnaryOperator f;
public TIterateDoubleStream(double value, DoubleUnaryOperator f) {
this(value, t -> true, f);
}
public TIterateDoubleStream(double value, DoublePredicate pr, DoubleUnaryOperator f) {
this.value = value;
this.pr = pr;
this.f = f;
}
@Override
public boolean next(DoublePredicate consumer) {
while (true) {
if (!pr.test(value)) {
return false;
}
double valueToReport = value;
value = f.applyAsDouble(value);
if (!consumer.test(valueToReport)) {

View File

@ -20,16 +20,25 @@ import java.util.function.UnaryOperator;
public class TIterateStream<T> extends TSimpleStreamImpl<T> {
private T value;
private Predicate<? super T> pr;
private UnaryOperator<T> f;
public TIterateStream(T value, UnaryOperator<T> f) {
this(value, t -> true, f);
}
public TIterateStream(T value, Predicate<? super T> pr, UnaryOperator<T> f) {
this.value = value;
this.pr = pr;
this.f = f;
}
@Override
public boolean next(Predicate<? super T> consumer) {
while (true) {
if (!pr.test(value)) {
return false;
}
T valueToReport = value;
value = f.apply(value);
if (!consumer.test(valueToReport)) {

View File

@ -20,16 +20,25 @@ import java.util.function.IntUnaryOperator;
public class TIterateIntStream extends TSimpleIntStreamImpl {
private int value;
private IntPredicate pr;
private IntUnaryOperator f;
public TIterateIntStream(int value, IntUnaryOperator f) {
this(value, t -> true, f);
}
public TIterateIntStream(int value, IntPredicate pr, IntUnaryOperator f) {
this.value = value;
this.pr = pr;
this.f = f;
}
@Override
public boolean next(IntPredicate consumer) {
while (true) {
if (!pr.test(value)) {
return false;
}
int valueToReport = value;
value = f.applyAsInt(value);
if (!consumer.test(valueToReport)) {

View File

@ -20,16 +20,25 @@ import java.util.function.LongUnaryOperator;
public class TIterateLongStream extends TSimpleLongStreamImpl {
private long value;
private LongPredicate pr;
private LongUnaryOperator f;
public TIterateLongStream(long value, LongUnaryOperator f) {
this(value, t -> true, f);
}
public TIterateLongStream(long value, LongPredicate pr, LongUnaryOperator f) {
this.value = value;
this.pr = pr;
this.f = f;
}
@Override
public boolean next(LongPredicate consumer) {
while (true) {
if (!pr.test(value)) {
return false;
}
long valueToReport = value;
value = f.applyAsLong(value);
if (!consumer.test(valueToReport)) {

View File

@ -457,4 +457,15 @@ public class StreamTest {
}).toArray();
assertArrayEquals(new int[] {0, 2}, mappedInt);
}
@Test
public void iterateWorks() {
for (int c = 0; c < 10; c++) {
int cnt = c;
int sum = Stream.iterate(1, i -> i < 2 * cnt, i -> i + 2).mapToInt(Integer::intValue).sum();
assertEquals(cnt * cnt, sum);
}
List<String> repetitions = Stream.iterate("", s -> s.length() < 5, s -> s + "a").toList();
assertEquals(List.of("", "a", "aa", "aaa", "aaaa"), repetitions);
}
}