@SuppressWarnings({ "rawtypes", "unchecked" })
    static <T, U> Seq<T> unfold(U seed, Function<? super U, Optional<Tuple2<T, U>>> unfolder) {
        Tuple2<? extends T, ? extends U>[] unfolded = new Tuple2[] { tuple((T) null, seed) };

        return seq((FunctionalSpliterator<T>) action -> {
            Optional<? extends Tuple2<? extends T, ? extends U>> result = unfolder.apply(unfolded[0].v2);

            if (result.isPresent())
                action.accept((unfolded[0] = result.get()).v1);

            return result.isPresent();
        });
    }
Unfold a function into a stream.
<p>
<pre><code>
// (1, 2, 3, 4, 5)
Seq.unfold(1, i -> i &lt;= 6 ? Optional.of(tuple(i, i + 1)) : Optional.empty())
</code></pre>
jOOL-java-8/src/main/java/org/jooq/lambda/Seq.java