|
| 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