Stream of infinite number of elements
Daniel Kristensen
daniel.kristensen at gmail.com
Sun Dec 30 07:55:54 PST 2012
Hi,
Did you consider pushing the strict operations to a sub-interface of
Stream. A few examples of where methods would go:
public interface Stream<T> extends BaseStream<T> {
Stream<T> filter(Predicate<? super T> predicate);
<R> Stream<R> map(Mapper<? extends R, ? super T> mapper);
...
FiniteStream<T> limit(int n);
...
}
public interface FiniteStream<T> extends Stream<T> {
FiniteStream<T> sorted(Comparator<? super T> comparator);
void forEach(Block<? super T> block);
<A extends Destination<? super T>> A into(A target);
Object[] toArray();
...
}
This would increase type safety, and would remove the needto document
in javadoc whether methods that take streams as parameters require
them to be finite etc.
Of course a drawback would be that FiniteStream is not quite as nicely
succinct as Stream.
Best regards
Daniel
> Message: 6
> Date: Fri, 28 Dec 2012 17:14:12 +0100
> From: Paul Sandoz <paul.sandoz at oracle.com>
> Subject: Re: Stream of infinite number of elements
> Cc: lambda-dev at openjdk.java.net
> Message-ID: <BFF9D2A3-DF34-4C71-95FC-BD658AC8EE4D at oracle.com>
> Content-Type: text/plain; charset=us-ascii
>
> On Dec 26, 2012, at 3:23 PM, Denis DEBARBIEUX <denis.debarbieux at inria.fr>
> wrote:
> > Hi all,
> >
> > To implement the interface /Stream/, I have to implement methods like
> > /max, min, sorted/.
> >
> > What is expected by the interface when my stream is infinite?
> Undetermined behaviour, unless those terminal operations are prefixed with
> an intermediate limit operation.
> The first two might never return and the latter one will most likely throw
> an OOE.
>
> > Throw an
> > exception (does the interface provide an InfiniteStreamException?) or
> > implement them as an any time algorithm?
> >
> We are considering a cancel or while operation that on say a met temporal
> condition will ensure no more elements are available, thus
> non-short-circuit terminal operations should terminate under such
> conditions.
> Originally we tried to detect cases when an infinite stream was hooked up
> to something that would be known to not terminate but we cannot capture all
> such cases so rather than partially supporting this we decided to go for
> the "developer beware" option.
> Paul.
More information about the lambda-dev
mailing list