Skip to content

Commit 35ac22c

Browse files
nabylamaibin
authored andcommitted
FunctionalJava article (eugenp#2586)
* Update pom.xml with functionalJava dependencies * Add functionalJava classes * update README.md
1 parent cc304b0 commit 35ac22c

4 files changed

Lines changed: 116 additions & 1 deletion

File tree

‎libraries/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
- [Guide to Apache Commons CircularFifoQueue](http://www.baeldung.com/commons-circular-fifo-queue)
3232
- [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss)
3333
- [Introduction to NoException](http://www.baeldung.com/no-exception)
34-
34+
- [Introduction to FunctionalJava](http://www.baeldung.com/functional-java)
3535

3636
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.
3737

‎libraries/pom.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,26 @@
554554
<artifactId>protonpack</artifactId>
555555
<version>${protonpack.version}</version>
556556
</dependency>
557+
<dependency>
558+
<groupId>org.functionaljava</groupId>
559+
<artifactId>functionaljava</artifactId>
560+
<version>4.7</version>
561+
</dependency>
562+
<dependency>
563+
<groupId>org.functionaljava</groupId>
564+
<artifactId>functionaljava-java8</artifactId>
565+
<version>4.7</version>
566+
</dependency>
567+
<dependency>
568+
<groupId>org.functionaljava</groupId>
569+
<artifactId>functionaljava-quickcheck</artifactId>
570+
<version>4.7</version>
571+
</dependency>
572+
<dependency>
573+
<groupId>org.functionaljava</groupId>
574+
<artifactId>functionaljava-java-core</artifactId>
575+
<version>4.7</version>
576+
</dependency>
557577
</dependencies>
558578
<repositories>
559579
<repository>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.fj;
2+
3+
import fj.F;
4+
import fj.F1Functions;
5+
import fj.Unit;
6+
import fj.data.IO;
7+
import fj.data.IOFunctions;
8+
9+
public class FunctionalJavaIOMain {
10+
11+
public static IO<Unit> printLetters(final String s) {
12+
return () -> {
13+
for (int i = 0; i < s.length(); i++) {
14+
System.out.println(s.charAt(i));
15+
}
16+
return Unit.unit();
17+
};
18+
}
19+
20+
public static void main(String[] args) {
21+
22+
F<String, IO<Unit>> printLetters = i -> printLetters(i);
23+
24+
IO<Unit> lowerCase = IOFunctions
25+
.stdoutPrintln("What's your first Name ?");
26+
27+
IO<Unit> input = IOFunctions.stdoutPrint("First Name: ");
28+
29+
IO<Unit> userInput = IOFunctions.append(lowerCase, input);
30+
31+
IO<String> readInput = IOFunctions.stdinReadLine();
32+
33+
F<String, String> toUpperCase = i -> i.toUpperCase();
34+
35+
F<String, IO<Unit>> transformInput = F1Functions
36+
.<String, IO<Unit>, String> o(printLetters).f(toUpperCase);
37+
38+
IO<Unit> readAndPrintResult = IOFunctions.bind(readInput,
39+
transformInput);
40+
41+
IO<Unit> program = IOFunctions.bind(userInput,
42+
nothing -> readAndPrintResult);
43+
44+
IOFunctions.toSafe(program).run();
45+
46+
}
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.fj;
2+
3+
import fj.F;
4+
import fj.Show;
5+
import fj.data.Array;
6+
import fj.data.List;
7+
import fj.data.Option;
8+
import fj.function.Characters;
9+
import fj.function.Integers;
10+
11+
public class FunctionalJavaMain {
12+
13+
public static final F<Integer, Boolean> isEven = i -> i % 2 == 0;
14+
15+
public static void main(String[] args) {
16+
17+
List<Integer> fList = List.list(3, 4, 5, 6);
18+
List<Boolean> evenList = fList.map(isEven);
19+
Show.listShow(Show.booleanShow).println(evenList);
20+
21+
fList = fList.map(i -> i + 1);
22+
Show.listShow(Show.intShow).println(fList);
23+
24+
Array<Integer> a = Array.array(17, 44, 67, 2, 22, 80, 1, 27);
25+
Array<Integer> b = a.filter(Integers.even);
26+
Show.arrayShow(Show.intShow).println(b);
27+
28+
Array<String> array = Array.array("Welcome", "To", "baeldung");
29+
Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase));
30+
System.out.println(isExist);
31+
32+
Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27);
33+
int sum = intArray.foldLeft(Integers.add, 0);
34+
System.out.println(sum);
35+
36+
Option<Integer> n1 = Option.some(1);
37+
Option<Integer> n2 = Option.some(2);
38+
39+
F<Integer, Option<Integer>> f1 = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none();
40+
41+
Option<Integer> result1 = n1.bind(f1);
42+
Option<Integer> result2 = n2.bind(f1);
43+
44+
Show.optionShow(Show.intShow).println(result1);
45+
Show.optionShow(Show.intShow).println(result2);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)