Skip to content

Instantly share code, notes, and snippets.

@alexandreaquiles
Last active December 12, 2015 09:59
Show Gist options
  • Select an option

  • Save alexandreaquiles/4756087 to your computer and use it in GitHub Desktop.

Select an option

Save alexandreaquiles/4756087 to your computer and use it in GitHub Desktop.
Lazy Fibonacci implementation in Java using the Functional Java library.
import fj.F2;
import fj.data.List;
import fj.data.Stream;
public class FunctionalJavaFibonacci {
public static final Stream<Long> fibonacciSequence = new F2<Long, Long, Stream<Long>>() {
public Stream<Long> f(final Long a, final Long b) {
return Stream.cons(a, curry().f(b).lazy().f(a + b));
}
}.f(0L, 1L);
public static void main(String[] args) {
List<Long> fibonacciUpto25 = fibonacciSequence.take(25).toList();
System.out.println(fibonacciUpto25);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment