??????????????У???????????? 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 ????ü?????????????????????