This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // instead of | |
| public java.util.List<Integer> legacyIterate(Integer... ints) { | |
| return java.util.Arrays | |
| .stream(ints) | |
| .map(i -> i + 1) | |
| .peek(System.out::println) | |
| .collect(Collectors.toList()); | |
| } | |
| // do it the functional way |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // instead of | |
| public String legacyJoin(String... args) { | |
| return java.util.Arrays | |
| .stream(args) | |
| .collect(Collectors.joining(", ")); | |
| } | |
| // do it the functional way | |
| public String vavrJoin(String... args) { | |
| return io.vavr.collection.List |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // instead of | |
| public java.util.List<String> legacyIteratingWithIndex1(String... args) { | |
| java.util.List<String> result = new ArrayList<>(); | |
| for (int i = 0; i < args.length; i++) { | |
| result.add(i + ". " + args[i]); | |
| } | |
| return result; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // instead of | |
| public java.util.List<String> legacyFlatMap1(java.util.List<Optional<String>> args) { | |
| return args | |
| .stream() | |
| .filter(Optional::isPresent) | |
| .map(Optional::get) | |
| .map(String::toLowerCase) | |
| .collect(Collectors.toList()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // instead of | |
| public Skills legacyFold(java.util.List<String> args) { | |
| Skills skills = new Skills(args.get(0)); | |
| args.remove(0); // someone may be surprised that passing you a list makes the first element disappearing | |
| args.forEach(skills::addSkill); | |
| return skills; | |
| } | |
| // do it the functional way | |
| public Skills vavrFold(io.vavr.collection.Seq<String> args) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // instead of | |
| public java.util.Map legacyJoiningLists1(java.util.List<String> fruits, java.util.List<BigDecimal> priceList) { | |
| java.util.Map<String, BigDecimal> map = new java.util.HashMap<String, BigDecimal>(); | |
| for (int i = 0; i < fruits.size(); i++) { | |
| BigDecimal price = i < priceList.size() ? priceList.get(i) : BigDecimal.ZERO; | |
| map.put(fruits.get(i), price); | |
| } | |
| return map; | |
| } | |
| // or |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public List<String> flatMapListOfLists( | |
| List<String> a1, List<String> a2, List<String> a3, List<String> a | |
| ) { | |
| List<List<String>> listOfLists = List.of(a1, a2, a3, a4); | |
| return listOfLists.flatMap(innerList -> innerList); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public List<String> flatMapListOfListsWithInnerMap( | |
| List<String> a1, List<String> a2, List<String> a3, List<String> a4 | |
| ) { | |
| List<List<String>> listOfLists = List.of(a1, a2, a3, a4); | |
| return listOfLists.flatMap(innerList -> innerList.map(s -> s.toLowerCase())); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public List<String> flatMapListOfOptions( | |
| Option<String> a1, Option<String> a2, Option<String> a3 | |
| ) { | |
| List<Option<String>> b = List.of(a1, a2, a3); | |
| return b.flatMap(innerOption -> innerOption); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public List<String> flatMapListOfOptionsWithInnerMap( | |
| Option<String> a1, Option<String> a2, Option<String> a3 | |
| ) { | |
| List<Option<String>> b = List.of(a1, a2, a3); | |
| return b.flatMap(innerOption -> innerOption.map(wrappedString -> wrappedString.toLowerCase())); | |
| } |