forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.java
More file actions
29 lines (24 loc) · 768 Bytes
/
Problem2.java
File metadata and controls
29 lines (24 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package fj.demo.euler;
import fj.F2;
import fj.data.Stream;
import static fj.data.Stream.cons;
import static fj.function.Integers.even;
import static fj.function.Integers.sum;
import static fj.Ord.intOrd;
import static java.lang.System.out;
/**
* Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
*/
public class Problem2 {
public static void main(final String[] args) {
java7();
}
static void java7() {
final Stream<Integer> fibs = new F2<Integer, Integer, Stream<Integer>>() {
public Stream<Integer> f(final Integer a, final Integer b) {
return cons(a, curry().f(b).lazy().f(a + b));
}
}.f(1, 2);
out.println(sum(fibs.filter(even).takeWhile(intOrd.isLessThan(4000001)).toList()));
}
}