mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2025-01-08 16:04:10 -08:00
classlib: added Stream iterate method (#721)
This commit is contained in:
parent
eed42e33b0
commit
ac2e5d6fa0
|
@ -145,6 +145,10 @@ public interface TDoubleStream extends TBaseStream<Double, TDoubleStream> {
|
||||||
return new TIterateDoubleStream(seed, f);
|
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) {
|
static TDoubleStream generate(DoubleSupplier s) {
|
||||||
return new TGenerateDoubleStream(s);
|
return new TGenerateDoubleStream(s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,6 +151,10 @@ public interface TIntStream extends TBaseStream<Integer, TIntStream> {
|
||||||
return new TIterateIntStream(seed, f);
|
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) {
|
static TIntStream generate(IntSupplier s) {
|
||||||
return new TGenerateIntStream(s);
|
return new TGenerateIntStream(s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,6 +149,10 @@ public interface TLongStream extends TBaseStream<Long, TLongStream> {
|
||||||
return new TIterateLongStream(seed, f);
|
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) {
|
static TLongStream generate(LongSupplier s) {
|
||||||
return new TGenerateLongStream(s);
|
return new TGenerateLongStream(s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,6 +176,10 @@ public interface TStream<T> extends TBaseStream<T, TStream<T>> {
|
||||||
return new TIterateStream<>(seed, f);
|
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) {
|
static <T> TStream<T> generate(Supplier<T> s) {
|
||||||
return new TGenerateStream<>(s);
|
return new TGenerateStream<>(s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,16 +20,25 @@ import java.util.function.DoubleUnaryOperator;
|
||||||
|
|
||||||
public class TIterateDoubleStream extends TSimpleDoubleStreamImpl {
|
public class TIterateDoubleStream extends TSimpleDoubleStreamImpl {
|
||||||
private double value;
|
private double value;
|
||||||
|
private DoublePredicate pr;
|
||||||
private DoubleUnaryOperator f;
|
private DoubleUnaryOperator f;
|
||||||
|
|
||||||
public TIterateDoubleStream(double value, 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.value = value;
|
||||||
|
this.pr = pr;
|
||||||
this.f = f;
|
this.f = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean next(DoublePredicate consumer) {
|
public boolean next(DoublePredicate consumer) {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (!pr.test(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
double valueToReport = value;
|
double valueToReport = value;
|
||||||
value = f.applyAsDouble(value);
|
value = f.applyAsDouble(value);
|
||||||
if (!consumer.test(valueToReport)) {
|
if (!consumer.test(valueToReport)) {
|
||||||
|
|
|
@ -20,16 +20,25 @@ import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
public class TIterateStream<T> extends TSimpleStreamImpl<T> {
|
public class TIterateStream<T> extends TSimpleStreamImpl<T> {
|
||||||
private T value;
|
private T value;
|
||||||
|
private Predicate<? super T> pr;
|
||||||
private UnaryOperator<T> f;
|
private UnaryOperator<T> f;
|
||||||
|
|
||||||
public TIterateStream(T value, 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.value = value;
|
||||||
|
this.pr = pr;
|
||||||
this.f = f;
|
this.f = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean next(Predicate<? super T> consumer) {
|
public boolean next(Predicate<? super T> consumer) {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (!pr.test(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
T valueToReport = value;
|
T valueToReport = value;
|
||||||
value = f.apply(value);
|
value = f.apply(value);
|
||||||
if (!consumer.test(valueToReport)) {
|
if (!consumer.test(valueToReport)) {
|
||||||
|
|
|
@ -20,16 +20,25 @@ import java.util.function.IntUnaryOperator;
|
||||||
|
|
||||||
public class TIterateIntStream extends TSimpleIntStreamImpl {
|
public class TIterateIntStream extends TSimpleIntStreamImpl {
|
||||||
private int value;
|
private int value;
|
||||||
|
private IntPredicate pr;
|
||||||
private IntUnaryOperator f;
|
private IntUnaryOperator f;
|
||||||
|
|
||||||
public TIterateIntStream(int value, 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.value = value;
|
||||||
|
this.pr = pr;
|
||||||
this.f = f;
|
this.f = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean next(IntPredicate consumer) {
|
public boolean next(IntPredicate consumer) {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (!pr.test(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
int valueToReport = value;
|
int valueToReport = value;
|
||||||
value = f.applyAsInt(value);
|
value = f.applyAsInt(value);
|
||||||
if (!consumer.test(valueToReport)) {
|
if (!consumer.test(valueToReport)) {
|
||||||
|
|
|
@ -20,16 +20,25 @@ import java.util.function.LongUnaryOperator;
|
||||||
|
|
||||||
public class TIterateLongStream extends TSimpleLongStreamImpl {
|
public class TIterateLongStream extends TSimpleLongStreamImpl {
|
||||||
private long value;
|
private long value;
|
||||||
|
private LongPredicate pr;
|
||||||
private LongUnaryOperator f;
|
private LongUnaryOperator f;
|
||||||
|
|
||||||
public TIterateLongStream(long value, 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.value = value;
|
||||||
|
this.pr = pr;
|
||||||
this.f = f;
|
this.f = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean next(LongPredicate consumer) {
|
public boolean next(LongPredicate consumer) {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (!pr.test(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
long valueToReport = value;
|
long valueToReport = value;
|
||||||
value = f.applyAsLong(value);
|
value = f.applyAsLong(value);
|
||||||
if (!consumer.test(valueToReport)) {
|
if (!consumer.test(valueToReport)) {
|
||||||
|
|
|
@ -457,4 +457,15 @@ public class StreamTest {
|
||||||
}).toArray();
|
}).toArray();
|
||||||
assertArrayEquals(new int[] {0, 2}, mappedInt);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user