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));
}
Aggregations