haskell prelude
David Conrad
drconrad at gmail.com
Fri Jun 22 14:00:31 PDT 2012
On Fri, 22 Jun 2012 09:29:19 +0300, Olexandr Demura <
oleksander.demura at gmail.com> wrote:
>
> that library written with haskell laziness in mind which is not present in
> java.
> you can add additional laziness by replacing `fibs.eval(y, z + y)`
> with Iterable `() -> fibs.eval(y, z + y).iterator()`,
> but that would not help, since `cons` requests iterator immediately,
> causing the same problem.
> so you need one more additional layer - lazy Iterator.
> but Iterator is not a SAM, so it can't be named elegant:
>
>
It is possible, and maybe even advisable, to make a SAM
type which is a subtype of Iterator, for infinite sequences:
interface InfiniteIterator<E> extends Iterator<E> {
boolean hasNext() default {
return true;
}
}
interface InfiniteIterable<E> extends Iterable<E> {
InfiniteIterator<E> iterator();
}
This has the great advantage that it also provides a tagging
Interface that one can check for in Iterables::count or
Iterables::forEach or a hypothetical Iterables::last(int n) or
other places where blindly consuming an infinite sequence
would do Bad Things(TM).
If this became idiomatic and standard APIs looked for it, it
might head off some problems.
David
More information about the lambda-dev
mailing list