Java 8?????compose??andThen??????
???????????? ???????[ 2015/12/22 10:54:07 ] ??????????????? ???????????
??????????????У???????????? Function ??????????????????—— compose ?? andThen ??????????????
???????????????
????????????????ЩС??????ú??????????ЩС?????????o?????
???????????????????? compose ?? andThen ??????????????????
????????????????????????—— times2 ?? squared??
????Function<Integer?? Integer> times2 = e -> e * 2;
????Function<Integer?? Integer> squared = e -> e * e;
??????????????? compose ?? andThen ??????????????
????times2.compose(squared).apply(4);
????// Returns 32
????times2.andThen(squared).apply(4);
????// Returns 64
??????????????compose ?? andThen ??????????????е???????compose ????????????????????е???????? andThen ????е?????????????в?????
????????????????
????????????????????????????????????????????????С???????——????????????????????Щ??????Ρ?
?????????????????
??????????????б?????????????????????????????Щ?????
????????????????????????????—— byAuthor ?? byTag——??????????????????????
????BiFunction<String?? List<Article>?? List<Article>> byAuthor =
????(name?? articles) -> articles.stream()
????.filter(a -> a.getAuthor().equals(name))
????.collect(Collectors.toList());
????BiFunction<String?? List<Article>?? List<Article>> byTag =
????(tag?? articles) -> articles.stream()
????.filter(a -> a.getTags().contains(tag))
????.collect(Collectors.toList());
???????????????? BiFunction——??ζ???????????????
????byAuthor ??????????????????б????????????????????????????????б??
????byTag ?????????????????????б???????????????????????????????б??
???????? BiFunction ??????????????????? andThen ???????????????????????????????????????????????У??????? compose ??????
?????????????????????????????????????б???μ???????????????????????????б??
????Function<List<Article>?? List<Article>> sortByDate =
????articles -> articles.stream()
????.sorted((x?? y) -> y.published().compareTo(x.published()))
????.collect(Collectors.toList());
????Function<List<Article>?? Optional<Article>> first =
????a -> a.stream().findFirst();
????????????????????????????????????????????????Щ???????????μ??????
??????????????????????????????????б??????
????Function<List<Article>?? Optional<Article>> newest =
????first.compose(sortByDate);
??????? first ?????????????????????? sortByDate???????????????μ???????ú???????????????б?????????
?????????????????????????????Щ????????????????????????????????????????д????
?????????????????£?
????BiFunction<String?? List<Article>?? Optional<Article>> newestByAuthor =
????byAuthor.andThen(newest);
???????????????????????????
????BiFunction<String?? List<Article>?? List<Article>> byAuthorSorted =
????byAuthor.andThen(sortByDate);
?????????????????????????????????????????μ????£?
????BiFunction<String?? List<Article>?? Optional<Article>> newestByTag =
????byTag.andThen(newest);
????????????????????? Function ????????????????????С?????飬????????????????????????????????????????????????? DRY ???
??????????——???? compose ?? andThen ????ü?????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11