Search in sources :

Example 1 with InfiniteClosingSpliteratorFromSupplier

use of com.oath.cyclops.internal.react.stream.InfiniteClosingSpliteratorFromSupplier in project cyclops by aol.

the class LazyReact method iterate.

/**
 * Iterate infinitely using the supplied seed and function
 * Iteration is synchronized to support multiple threads using the same iterator.
 *
 * <pre>
 * {@code
 *  new LazyReact().objectPoolingOn()
 *                       .iterate(1,i->i+1)
 *                       .limit(1_000_000)
 *                       .map(this::process)
 *                       .forEach(this::save);
 *
 * }
 * </pre>
 *
 * @see FutureStream#iterate for an alternative which does not synchronize iteration
 * @param seed Initial value
 * @param f Function that performs the iteration
 * @return FutureStream
 */
public <U> FutureStream<U> iterate(final U seed, final UnaryOperator<U> f) {
    final Subscription sub = new Subscription();
    final Supplier<U> supplier = new Supplier<U>() {

        @SuppressWarnings("unchecked")
        U t = (U) NONE;

        @Override
        public U get() {
            return t = t == NONE ? seed : f.apply(t);
        }
    };
    return construct(StreamSupport.<U>stream(new InfiniteClosingSpliteratorFromSupplier<U>(Long.MAX_VALUE, supplier, sub), false));
}
Also used : InfiniteClosingSpliteratorFromSupplier(com.oath.cyclops.internal.react.stream.InfiniteClosingSpliteratorFromSupplier) Supplier(java.util.function.Supplier) InfiniteClosingSpliteratorFromSupplier(com.oath.cyclops.internal.react.stream.InfiniteClosingSpliteratorFromSupplier) Subscription(com.oath.cyclops.react.async.subscription.Subscription)

Aggregations

InfiniteClosingSpliteratorFromSupplier (com.oath.cyclops.internal.react.stream.InfiniteClosingSpliteratorFromSupplier)1 Subscription (com.oath.cyclops.react.async.subscription.Subscription)1 Supplier (java.util.function.Supplier)1