Skip to content

Commit d4ed933

Browse files
committed
Reviewed java 8 examples
1 parent 8197d91 commit d4ed933

File tree

1 file changed

+100
-71
lines changed

1 file changed

+100
-71
lines changed

src/jbake/content/examples-java8.adoc

Lines changed: 100 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/
2323

2424
Checks for the existence of a String that has all lower case characters. In this case it is true (since, the case of "what"), but without this specific case, it is false. This example uses List.forall to test the characters of the String.
2525

26-
2726
[source,java]
2827
----
2928
import fj.data.Array;
3029
import static fj.data.Array.array;
3130
import static fj.data.List.fromString;
32-
import static java.lang.Character.isLowerCase;
31+
import static fj.function.Characters.isLowerCase;
3332
3433
public final class Array_exists {
35-
public static void main(final String[] args) {
36-
final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT");
37-
final boolean b = a.exists(s -> fromString(s).forall(isLowerCase));
38-
System.out.println(b); // true ("what" provides the only example; try removing it)
39-
}
34+
public static void main(final String[] args) {
35+
final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT");
36+
final boolean b = a.exists(s -> fromString(s).forall(isLowerCase));
37+
System.out.println(b); // true ("what" is the only value that qualifies; try removing it)
38+
}
4039
}
4140
----
4241

@@ -51,13 +50,15 @@ import fj.data.Array;
5150
import static fj.data.Array.array;
5251
import static fj.Show.arrayShow;
5352
import static fj.Show.intShow;
53+
import static fj.function.Integers.even;
5454
5555
public final class Array_filter {
56-
public static void main(final String[] args) {
57-
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
58-
final Array<Integer> b = a.filter(i -> i % 2 == 0);
59-
arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42}
60-
}
56+
public static void main(final String[] args) {
57+
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
58+
final Array<Integer> b = a.filter(even);
59+
final Array<Integer> c = a.filter(i -> i % 2 == 0);
60+
arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42}
61+
}
6162
}
6263
----
6364

@@ -68,15 +69,20 @@ Reduces the list applying a function per element. In this case, the fold sums th
6869

6970
[source,java]
7071
----
72+
import fj.F;
7173
import fj.data.Array;
7274
import static fj.data.Array.array;
75+
import static fj.function.Integers.add;
7376
7477
public final class Array_foldLeft {
75-
public static void main(final String[] args) {
76-
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
77-
final int b = a.<Integer>foldLeft(i -> (j -> i + j), 0);
78-
System.out.println(b); // 1774
79-
}
78+
public static void main(final String[] args) {
79+
final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
80+
final int b = a.foldLeft(add, 0);
81+
82+
F<Integer, F<Integer, Integer>> add2 = i -> (j -> i + j);
83+
final int c = a.foldLeft(add2, 0);
84+
System.out.println(b); // 1774
85+
}
8086
}
8187
----
8288

@@ -91,35 +97,38 @@ Checks that all Strings in the array have lower case characters. In this case, t
9197
import fj.data.Array;
9298
import static fj.data.Array.array;
9399
import static fj.data.List.fromString;
94-
import static java.lang.Character.isLowerCase;
100+
import static fj.function.Characters.isLowerCase;
95101
96102
public final class Array_forall {
97-
public static void main(final String[] args) {
98-
final Array<String> a = array("hello", "There", "what", "day", "is", "it");
99-
final boolean b = a.forall(s -> fromString(s).forall(isLowerCase));
100-
System.out.println(b); // false ("There" is a counter-example; try removing it)
101-
}
103+
public static void main(final String[] args) {
104+
final Array<String> a = array("hello", "There", "what", "day", "is", "it");
105+
final boolean b = a.forall(s -> fromString(s).forall(isLowerCase));
106+
System.out.println(b); // false ("There" is a counter-example; try removing it)
107+
}
102108
}
103109
----
104110

105111
== Array Map [[arrayMap]]
106112
https://github.com/functionaljava/functionaljava/blob/master/demo/src/main/java/fj/demo/Array_map.java[Github Source]
107113

108-
Maps a function across the array of integers. In this case, the function adds 42 to each element of the array to produce a new array.</p>
114+
Maps a function across the array of integers. In this case, the function adds 42 to each element of the array to produce a new array.
109115

110116
[source,java]
111117
----
112118
import fj.data.Array;
113119
import static fj.data.Array.array;
120+
import static fj.function.Integers.add;
114121
import static fj.Show.arrayShow;
115122
import static fj.Show.intShow;
116123
117124
public final class Array_map {
118-
public static void main(final String[] args) {
119-
final Array<Integer> a = array(1, 2, 3);
120-
final Array<Integer> b = a.map(i -> i + 42);
121-
arrayShow(intShow).println(b); // {43,44,45}
122-
}
125+
public static void main(final String[] args) {
126+
final Array<Integer> a = array(1, 2, 3);
127+
final Array<Integer> b = a.map(add.f(42));
128+
final Array<Integer> c = a.map(i -> i + 42);
129+
arrayShow(intShow).println(b); // {43,44,45}
130+
arrayShow(intShow).println(c); // {43,44,45}
131+
}
123132
}
124133
----
125134

@@ -130,17 +139,19 @@ Maps a function across a list of integers. This is similar to the Array map. Thi
130139

131140
[source,java]
132141
----
133-
import static fj.data.List.list;
134142
import fj.data.List;
143+
import static fj.data.List.list;
144+
import static fj.function.Integers.add;
135145
import static fj.Show.intShow;
136146
import static fj.Show.listShow;
137147
138148
public final class List_map {
139-
public static void main(final String[] args) {
140-
final List<Integer> a = list(1, 2, 3);
141-
final List<Integer> b = a.map(i -> i + 42);
142-
listShow(intShow).println(b); // [43,44,45]
143-
}
149+
public static void main(final String[] args) {
150+
final List<Integer> a = list(1, 2, 3);
151+
final List<Integer> b = a.map(add.f(42));
152+
final List<Integer> c = a.map(i -> i = 42);
153+
listShow(intShow).println(b); // [43,44,45]
154+
}
144155
}
145156
----
146157

@@ -152,24 +163,28 @@ Binds a function across the optional value type. The function checks if the cont
152163

153164
[source,java]
154165
----
166+
import fj.F;
155167
import fj.data.Option;
156-
import static fj.data.Option.none;
157-
import static fj.data.Option.some;
158168
import static fj.Show.intShow;
159169
import static fj.Show.optionShow;
170+
import static fj.data.Option.none;
171+
import static fj.data.Option.some;
160172
161173
public final class Option_bind {
162-
public static void main(final String[] args) {
163-
final Option<Integer> o1 = some(7);
164-
final Option<Integer> o2 = some(8);
165-
final Option<Integer> o3 = none();
166-
final Option<Integer> p1 = o1.bind(i -> i % 2 == 0 ? some(i * 3) : Option.<Integer>none());
167-
final Option<Integer> p2 = o2.bind(i -> i % 2 == 0 ? some(i * 3) : Option.<Integer>none());
168-
final Option<Integer> p3 = o3.bind(i -> i % 2 == 0 ? some(i * 3) : Option.<Integer>none());
169-
optionShow(intShow).println(p1); // None
170-
optionShow(intShow).println(p2); // Some(24)
171-
optionShow(intShow).println(p3); // None
172-
}
174+
public static void main(final String[] args) {
175+
final Option<Integer> o1 = some(7);
176+
final Option<Integer> o2 = some(8);
177+
final Option<Integer> o3 = none();
178+
179+
F<Integer, Option<Integer>> f = i -> i % 2 == 0 ? some(i * 3) : none();
180+
final Option<Integer> o4 = o1.bind(f);
181+
final Option<Integer> o5 = o2.bind(f);
182+
final Option<Integer> o6 = o3.bind(f);
183+
184+
optionShow(intShow).println(o4); // None
185+
optionShow(intShow).println(o5); // Some(24)
186+
optionShow(intShow).println(o6); // None
187+
}
173188
}
174189
----
175190

@@ -183,24 +198,33 @@ Removes the value from the optional value if it does not match a given predicate
183198

184199
[source,java]
185200
----
201+
import fj.F;
186202
import fj.data.Option;
187-
import static fj.data.Option.none;
188-
import static fj.data.Option.some;
189203
import static fj.Show.intShow;
190204
import static fj.Show.optionShow;
205+
import static fj.data.Option.none;
206+
import static fj.data.Option.some;
207+
import static fj.function.Integers.even;
191208
192209
public final class Option_filter {
193-
public static void main(final String[] args) {
194-
final Option<Integer> o1 = some(7);
195-
final Option<Integer> o2 = none();
196-
final Option<Integer> o3 = some(8);
197-
final Option<Integer> p1 = o1.filter(i -> i % 2 == 0);
198-
final Option<Integer> p2 = o2.filter(i -> i % 2 == 0);
199-
final Option<Integer> p3 = o3.filter(i -> i % 2 == 0);
200-
optionShow(intShow).println(p1); // None
201-
optionShow(intShow).println(p2); // None
202-
optionShow(intShow).println(p3); // Some(8)
203-
}
210+
public static void main(final String[] args) {
211+
final Option<Integer> o1 = some(7);
212+
final Option<Integer> o2 = none();
213+
final Option<Integer> o3 = some(8);
214+
215+
final Option<Integer> o4 = o1.filter(even);
216+
final Option<Integer> o5 = o2.filter(even);
217+
final Option<Integer> o6 = o3.filter(even);
218+
219+
F<Integer, Boolean> f = i -> i % 2 == 0;
220+
final Option<Integer> o7 = o1.filter(f);
221+
final Option<Integer> o8 = o1.filter(f);
222+
final Option<Integer> o9 = o1.filter(i -> i % 2 == 0);
223+
224+
optionShow(intShow).println(o4); // None
225+
optionShow(intShow).println(o5); // None
226+
optionShow(intShow).println(o6); // Some(8)
227+
}
204228
}
205229
----
206230

@@ -212,20 +236,25 @@ Maps a function across the optional value type. The function adds 42 to any cont
212236
[source,java]
213237
----
214238
import fj.data.Option;
215-
import static fj.data.Option.none;
216-
import static fj.data.Option.some;
217239
import static fj.Show.intShow;
218240
import static fj.Show.optionShow;
241+
import static fj.data.Option.none;
242+
import static fj.data.Option.some;
243+
import static fj.function.Integers.add;
219244
220245
public final class Option_map {
221-
public static void main(final String[] args) {
222-
final Option<Integer> o1 = some(7);
223-
final Option<Integer> o2 = none();
224-
final Option<Integer> p1 = o1.map(i -> i + 42);
225-
final Option<Integer> p2 = o2.map(i -> i + 42);
226-
optionShow(intShow).println(p1); // Some(49)
227-
optionShow(intShow).println(p2); // None
228-
}
246+
public static void main(final String[] args) {
247+
final Option<Integer> o1 = some(7);
248+
final Option<Integer> o2 = none();
249+
final Option<Integer> p1 = o1.map(add.f(42));
250+
final Option<Integer> p2 = o2.map(add.f(42));
251+
252+
final Option<Integer> p3 = o1.map(i -> i + 42);
253+
final Option<Integer> p4 = o2.map(i -> i + 42);
254+
255+
optionShow(intShow).println(p1); // Some(49)
256+
optionShow(intShow).println(p2); // None
257+
}
229258
}
230259
----
231260

0 commit comments

Comments
 (0)