Skip to content

Commit ff9925e

Browse files
committed
Merge pull request #103 from mperry/review-java8-examples
Reviewed examples shown on the website for java 8
2 parents e946674 + 097b298 commit ff9925e

9 files changed

Lines changed: 86 additions & 96 deletions

File tree

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package fj.demo;
22

3-
import fj.F;
43
import fj.data.Array;
54
import static fj.data.Array.array;
65
import static fj.data.List.fromString;
76
import static fj.function.Characters.isLowerCase;
87

98
public final class Array_exists {
10-
public static void main(final String[] args) {
11-
final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT");
12-
final boolean b = a.exists(new F<String, Boolean>() {
13-
public Boolean f(final String s) {
14-
return fromString(s).forall(isLowerCase);
15-
}
16-
});
17-
final boolean b2 = a.exists(s -> fromString(s).forall(isLowerCase));
18-
System.out.println(b); // true ("what" provides the only example; try removing it)
19-
}
9+
public static void main(final String[] args) {
10+
final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT");
11+
final boolean b = a.exists(s -> fromString(s).forall(isLowerCase));
12+
System.out.println(b); // true ("what" is the only value that qualifies; try removing it)
13+
}
2014
}

demo/src/main/java/fj/demo/Array_filter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import static fj.function.Integers.even;
88

99
public final class Array_filter {
10-
public static void main(final String[] args) {
11-
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
12-
final Array<Integer> b = a.filter(even);
13-
final Array<Integer> c = a.filter(i -> i % 2 == 0);
14-
arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42}
15-
}
10+
public static void main(final String[] args) {
11+
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
12+
final Array<Integer> b = a.filter(even);
13+
final Array<Integer> c = a.filter(i -> i % 2 == 0);
14+
arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42}
15+
}
1616
}

demo/src/main/java/fj/demo/Array_foldLeft.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
import static fj.function.Integers.add;
77

88
public final class Array_foldLeft {
9-
public static void main(final String[] args) {
10-
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
11-
final int b = a.foldLeft(add, 0);
9+
public static void main(final String[] args) {
10+
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
11+
final int b = a.foldLeft(add, 0);
1212

13-
// WARNING: In JDK 8, update 20 and 25 (current version) the following code triggers an internal JDK compiler error, likely due to https://bugs.openjdk.java.net/browse/JDK-8062253. The code below is a workaround for this compiler bug.
14-
// final int c = a.foldLeft(i -> (j -> i + j), 0);
15-
F<Integer, F<Integer, Integer>> add2 = i -> (j -> i + j);
16-
final int c = a.foldLeft(add2, 0);
17-
18-
System.out.println(b); // 1774
19-
}
13+
// WARNING: In JDK 8, update 20 and 25 (current version) the following code triggers an internal JDK compiler error, likely due to https://bugs.openjdk.java.net/browse/JDK-8062253. The code below is a workaround for this compiler bug.
14+
// final int c = a.foldLeft(i -> (j -> i + j), 0);
15+
F<Integer, F<Integer, Integer>> add2 = i -> (j -> i + j);
16+
final int c = a.foldLeft(add2, 0);
17+
System.out.println(b); // 1774
18+
}
2019
}
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package fj.demo;
22

3-
import fj.F;
43
import fj.data.Array;
54
import static fj.data.Array.array;
65
import static fj.data.List.fromString;
76
import static fj.function.Characters.isLowerCase;
87

98
public final class Array_forall {
10-
public static void main(final String[] args) {
11-
final Array<String> a = array("hello", "There", "what", "day", "is", "it");
12-
final boolean b = a.forall(new F<String, Boolean>() {
13-
public Boolean f(final String s) {
14-
return fromString(s).forall(isLowerCase);
15-
}
16-
});
17-
final boolean b2 = a.forall(s -> fromString(s).forall(isLowerCase));
18-
System.out.println(b); // false ("There" is a counter-example; try removing it)
19-
}
9+
public static void main(final String[] args) {
10+
final Array<String> a = array("hello", "There", "what", "day", "is", "it");
11+
final boolean b = a.forall(s -> fromString(s).forall(isLowerCase));
12+
System.out.println(b); // false ("There" is a counter-example; try removing it)
13+
}
2014
}

demo/src/main/java/fj/demo/Array_map.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fj.demo;
22

33
import fj.data.Array;
4-
54
import static fj.data.Array.array;
65
import static fj.function.Integers.add;
76
import static fj.Show.arrayShow;

demo/src/main/java/fj/demo/List_map.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fj.demo;
22

33
import fj.data.List;
4-
54
import static fj.data.List.list;
65
import static fj.function.Integers.add;
76
import static fj.Show.intShow;

demo/src/main/java/fj/demo/Option_bind.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,40 @@
22

33
import fj.F;
44
import fj.data.Option;
5-
import static fj.data.Option.none;
6-
import static fj.data.Option.some;
75
import static fj.Show.intShow;
86
import static fj.Show.optionShow;
7+
import static fj.data.Option.none;
8+
import static fj.data.Option.some;
99

1010
public final class Option_bind {
11-
public static void main(final String[] args) {
12-
final Option<Integer> o1 = some(7);
13-
final Option<Integer> o2 = some(8);
14-
final Option<Integer> o3 = none();
15-
final Option<Integer> p1 = o1.bind(new F<Integer, Option<Integer>>() {
16-
public Option<Integer> f(final Integer i) {
17-
return i % 2 == 0 ? some(i * 3) : Option.<Integer>none();
18-
}
19-
});
20-
final Option<Integer> p2 = o2.bind(new F<Integer, Option<Integer>>() {
21-
public Option<Integer> f(final Integer i) {
22-
return i % 2 == 0 ? some(i * 3) : Option.<Integer>none();
23-
}
24-
});
25-
final Option<Integer> p3 = o3.bind(new F<Integer, Option<Integer>>() {
26-
public Option<Integer> f(final Integer i) {
27-
return i % 2 == 0 ? some(i * 3) : Option.<Integer>none();
28-
}
29-
});
11+
public static void main(final String[] args) {
12+
final Option<Integer> o1 = some(7);
13+
final Option<Integer> o2 = some(8);
14+
final Option<Integer> o3 = none();
15+
16+
F<Integer, Option<Integer>> f = i -> i % 2 == 0 ? some(i * 3) : none();
17+
final Option<Integer> o4 = o1.bind(f);
18+
final Option<Integer> o5 = o2.bind(f);
19+
final Option<Integer> o6 = o3.bind(f);
3020

31-
final Option<Integer> p4 = o1.bind(i -> i % 2 == 0 ? some(i * 3) : Option.<Integer>none());
32-
final Option<Integer> p5 = o2.bind(i -> i % 2 == 0 ? some(i * 3) : Option.<Integer>none());
33-
final Option<Integer> p6 = o3.bind(i -> i % 2 == 0 ? some(i * 3) : Option.<Integer>none());
21+
final Option<Integer> p1 = o1.bind(new F<Integer, Option<Integer>>() {
22+
public Option<Integer> f(final Integer i) {
23+
return i % 2 == 0 ? some(i * 3) : Option.<Integer>none();
24+
}
25+
});
26+
final Option<Integer> p2 = o2.bind(new F<Integer, Option<Integer>>() {
27+
public Option<Integer> f(final Integer i) {
28+
return i % 2 == 0 ? some(i * 3) : Option.<Integer>none();
29+
}
30+
});
31+
final Option<Integer> p3 = o3.bind(new F<Integer, Option<Integer>>() {
32+
public Option<Integer> f(final Integer i) {
33+
return i % 2 == 0 ? some(i * 3) : Option.<Integer>none();
34+
}
35+
});
3436

35-
optionShow(intShow).println(p1); // None
36-
optionShow(intShow).println(p2); // Some(24)
37-
optionShow(intShow).println(p3); // None
38-
}
37+
optionShow(intShow).println(o4); // None
38+
optionShow(intShow).println(o5); // Some(24)
39+
optionShow(intShow).println(o6); // None
40+
}
3941
}
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
package fj.demo;
22

3+
import fj.F;
34
import fj.data.Option;
5+
import static fj.Show.intShow;
6+
import static fj.Show.optionShow;
47
import static fj.data.Option.none;
58
import static fj.data.Option.some;
69
import static fj.function.Integers.even;
7-
import static fj.Show.intShow;
8-
import static fj.Show.optionShow;
910

1011
public final class Option_filter {
11-
public static void main(final String[] args) {
12-
final Option<Integer> o1 = some(7);
13-
final Option<Integer> o2 = none();
14-
final Option<Integer> o3 = some(8);
15-
final Option<Integer> p1 = o1.filter(even);
16-
final Option<Integer> p2 = o2.filter(even);
17-
final Option<Integer> p3 = o3.filter(even);
12+
public static void main(final String[] args) {
13+
final Option<Integer> o1 = some(7);
14+
final Option<Integer> o2 = none();
15+
final Option<Integer> o3 = some(8);
16+
17+
final Option<Integer> o4 = o1.filter(even);
18+
final Option<Integer> o5 = o2.filter(even);
19+
final Option<Integer> o6 = o3.filter(even);
1820

19-
final Option<Integer> p4 = o1.filter(i -> i % 2 == 0);
20-
final Option<Integer> p5 = o1.filter(i -> i % 2 == 0);
21-
final Option<Integer> p6 = o1.filter(i -> i % 2 == 0);
21+
F<Integer, Boolean> f = i -> i % 2 == 0;
22+
final Option<Integer> o7 = o1.filter(f);
23+
final Option<Integer> o8 = o1.filter(f);
24+
final Option<Integer> o9 = o1.filter(i -> i % 2 == 0);
2225

23-
optionShow(intShow).println(p1); // None
24-
optionShow(intShow).println(p2); // None
25-
optionShow(intShow).println(p3); // Some(8)
26-
}
26+
optionShow(intShow).println(o4); // None
27+
optionShow(intShow).println(o5); // None
28+
optionShow(intShow).println(o6); // Some(8)
29+
}
2730
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package fj.demo;
22

33
import fj.data.Option;
4+
import static fj.Show.intShow;
5+
import static fj.Show.optionShow;
46
import static fj.data.Option.none;
57
import static fj.data.Option.some;
68
import static fj.function.Integers.add;
7-
import static fj.Show.intShow;
8-
import static fj.Show.optionShow;
99

1010
public final class Option_map {
11-
public static void main(final String[] args) {
12-
final Option<Integer> o1 = some(7);
13-
final Option<Integer> o2 = none();
14-
final Option<Integer> p1 = o1.map(add.f(42));
15-
final Option<Integer> p2 = o2.map(add.f(42));
11+
public static void main(final String[] args) {
12+
final Option<Integer> o1 = some(7);
13+
final Option<Integer> o2 = none();
14+
final Option<Integer> p1 = o1.map(add.f(42));
15+
final Option<Integer> p2 = o2.map(add.f(42));
1616

17-
final Option<Integer> p3 = o1.map(i -> i + 42);
18-
final Option<Integer> p4 = o2.map(i -> i + 42);
17+
final Option<Integer> p3 = o1.map(i -> i + 42);
18+
final Option<Integer> p4 = o2.map(i -> i + 42);
1919

20-
optionShow(intShow).println(p1); // Some(49)
21-
optionShow(intShow).println(p2); // None
22-
}
20+
optionShow(intShow).println(p1); // Some(49)
21+
optionShow(intShow).println(p2); // None
22+
}
2323
}

0 commit comments

Comments
 (0)