Skip to content

Commit 617d807

Browse files
committed
Fibonacci Tuples+
1 parent d900a53 commit 617d807

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/com/java8in/action/ch5/BuildingStream.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.file.Files;
99
import java.nio.file.Paths;
1010
import java.util.Arrays;
11+
import java.util.Date;
1112
import java.util.stream.Stream;
1213

1314
/**
@@ -41,6 +42,12 @@ public static void main(String[] args) {
4142
} catch (IOException e) {
4243
System.out.println("IO exception");
4344
}
45+
46+
//Stream iterate and generate
47+
48+
Stream.iterate(2, n-> n+2).limit(10).forEach(System.out::println);
49+
Stream.generate(Math::random).limit(10).forEach(System.out::println);
50+
4451

4552
}
4653

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
*
3+
*/
4+
package com.java8in.action.ch5;
5+
6+
import java.util.stream.Stream;
7+
8+
/**
9+
* eg. (0,1)(1,1)(1,2)(2,3)(3,5)(5,8)....
10+
* @author Sanket Gupta
11+
*
12+
*/
13+
public class FibonacciTuple {
14+
15+
/**
16+
* @param args
17+
*/
18+
public static void main(String[] args) {
19+
System.out.println("Printing tuples");
20+
Stream.iterate(new int []{0,1}, t -> new int[]{t[1],t[0]+t[1]})
21+
.limit(10)
22+
.forEach(t->System.out.print("("+t[0]+","+t[1]+")"));
23+
24+
System.out.println("Printing series");
25+
Stream.iterate(new int []{0,1}, t -> new int[]{t[1],t[0]+t[1]})
26+
.map(t->t[0])
27+
.limit(10)
28+
.forEach(System.out::println);
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)