question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Streams.iterate(T seed, UnaryOperator<T> f, Predicate<T> stop)

See original GitHub issue

Stream has a two argument version of iterate that takes a seed (T seed) and an iterative function (UnaryOperator<T> f) and generates an infinite stream by repeatedly applying the function. This is an almost perfect replacement for a traditional for loop, except that it’s missing a termination predicate. Java 9 adds takeWhile which can be used to terminate a stream generated this way, but unfortunately some of us aren’t beyond Java 8 yet.

It might be nice to add a static method <T> Stream<T> iterate(T seed, UnaryOperator<T> f, Predicate<T> stop) to the Streams utility class that could fill this niche, at least until Java 11 is out and widely considered stable. An alternative (or possibly additional) approach would be adding a static takeWhile method to the same class that would limit the Stream by some predicate, though that’s a bit clunkier.

Thanks, and let me know if you need more details or any clarification.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
fluentfuturecommented, Sep 6, 2018

Well. That’s just my personal observation as I don’t run into needing this often.

Let’s see what the Guava maintainers think. 😃

0reactions
fluentfuturecommented, Sep 6, 2018

Speculating about the API, I think the iterate(initial, step, stop) signature as is may not be self-evident enough. It’s equally likely to think of the predicate as while instead of until because it’s what a for-loop will look like.

And I suppose adding two similar APIs like iterateUntil and iterateWhile might be too much for little gain.

On the other hand, the more generic generate(initial, iterate) signature could be used to create a less ambiguous syntax, but you’ll need to add two helpers streamWhile() and streamUntil() in the local code base.

Function<T, Stream<T>> streamUntil(Predicate<T> condition, UnaryOperator<T> step) {
  return v -> condition.test(v) ? Stream.of(step.apply(v)) : Stream.empty();
}
Function<T, Stream<T>> streamWhile(Predicate<T> condition, UnaryOperator<T> step) {
  return streamUntil(condition.negate(), step);
}

Then:

generate(initial, streamUntil(this::shouldStop, this::nextValue));
generate(initial, streamWhile(this::shouldContinue, this::nextValue));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Stream iterate(T,Predicate,UnaryOperator) method in Java ...
Parameters: This method accepts three parameters: seed: which is the initial element,; hasNext: which is a predicate to apply to elements to ...
Read more >
Java 9 Stream.iterate​(T seed, Predicate<? super T> hasNext ...
iterate should produce the same sequence of elements as produced by the corresponding for-loop: for (T index=seed; hasNext.test(index); index = ...
Read more >
Java streams 8. Create using Stream.iterate() - Nick Samoylov
The static Stream.iterate(T seed, UnaryOperator f) method returns Stream<T> object that emits an infinite number of values of type T, ...
Read more >
Stream iterate(T seed, UnaryOperator f) example - Java2s.com
Java Streams - Stream iterate(T seed, UnaryOperator f) example.
Read more >
Chapter 5. Working with streams - Java 8 in Action
Filtering and slicing. In this section, we look at how to select elements of a stream: filtering with a predicate, filtering only unique...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found