haskell prelude
Olexandr Demura
oleksander.demura at gmail.com
Thu Jun 21 23:29:19 PDT 2012
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:
final private static Function2<Integer, Integer, Iterable<Integer>> fibs
= (z, y) -> cons(z, cons(y, () -> new Iterator<Integer>() {
Iterator<Integer> delegate;
Iterator<Integer> getDelegate() {
if (delegate == null) delegate = fibs.eval(y, z + y).iterator();
return delegate;
}
public boolean hasNext() { return true; }
public Integer next() { return getDelegate().next(); }
}));
IMO it is not any better that plain old
final private static Function2<Integer, Integer, Iterable<Integer>> fibs
= (z, y) -> (() -> new Iterator<Integer> {
int curr = z;
int next = y;
public boolean hasNext() { return true; }
public Integer next() {
int r = curr;
curr = next;
next = r + curr;
return r;
}
});
> Date: Thu, 21 Jun 2012 15:33:10 +0200
> From: Luc Duponcheel <luc.duponcheel at gmail.com>
> Subject: haskell prelude
> To: lambda-dev at openjdk.java.net
> Message-ID:
> <CADO5TNtNy+4Td1cXvVVV-kysVhsp2LZzcj1wxEKUQb0QNAWp2w at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Some time ago Sven Eric Panitz posted an experimental haskell prelude
> library to this group (one of his goals being to work with "infinite
> iterables" (right?))
>
>
> So I tried out something similar to
>
> public class Test{
> final private static Function2<Integer, Integer, Iterable<Integer>> fibs =
> (Integer z, Integer y) -> cons(z, cons(y, fibs.eval(y, z + y)));
> public static void main(String[] args){
> System.out.println(take(5, fibs.eval(1, 1)));
> }
> }
>
> but this results in a stack overflow
>
> So my question to Sven (and others (?)) is:
> is there an elegant similar way to define fibs without this issue?
>
>
> thx
>
> Luc
>
> --
> __~O
> -\ <,
> (*)/ (*)
>
> reality goes far beyond imagination
More information about the lambda-dev
mailing list