static <T> Gen<T> of(T seed, Function<? super T, ? extends T> next) { Objects.requireNonNull(next, "next is null"); final Iterator<T> iterator = Iterator.iterate(seed, next); return ignored -> iterator.next(); }
/** * Generates a (theoretically) infinitely long Stream using a function to calculate the next value * based on the previous. * * @param seed The first value in the Stream * @param f A function to calculate the next value based on the previous * @param <T> value type * @return A new Stream */ static <T> Stream<T> iterate(T seed, Function<? super T, ? extends T> f) { Objects.requireNonNull(f, "f is null"); return Stream.ofAll(Iterator.iterate(seed, f)); }
/** * Generates a (theoretically) infinitely long Stream using a repeatedly invoked supplier * that provides a {@code Some} for each next value and a {@code None} for the end. * The {@code Supplier} will be invoked only that many times until it returns {@code None}, * and repeated iteration over the stream will produce the same values in the same order, * without any further invocations to the {@code Supplier}. * * @param supplier A Supplier of iterator values * @param <T> value type * @return A new Stream */ static <T> Stream<T> iterate(Supplier<? extends Option<? extends T>> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return Stream.ofAll(Iterator.iterate(supplier)); }
/** * Generates a (theoretically) infinitely long Stream using a function to calculate the next value * based on the previous. * * @param seed The first value in the Stream * @param f A function to calculate the next value based on the previous * @param <T> value type * @return A new Stream */ static <T> Stream<T> iterate(T seed, Function<? super T, ? extends T> f) { Objects.requireNonNull(f, "f is null"); return Stream.ofAll(Iterator.iterate(seed, f)); }
/** * Generates a (theoretically) infinitely long Stream using a repeatedly invoked supplier * that provides a {@code Some} for each next value and a {@code None} for the end. * The {@code Supplier} will be invoked only that many times until it returns {@code None}, * and repeated iteration over the stream will produce the same values in the same order, * without any further invocations to the {@code Supplier}. * * @param supplier A Supplier of iterator values * @param <T> value type * @return A new Stream */ static <T> Stream<T> iterate(Supplier<? extends Option<? extends T>> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return Stream.ofAll(Iterator.iterate(supplier)); }