且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 8:使用compose和andThen组合函数

更新时间:2021-10-25 04:46:15

在这篇文章中,我将讨论利用 Function 接口提供的两个组合函数—— compose 和 andThen 来实现函数的组合。

什么是函数组合?

首先需要创建一些小的可重用函数,然后将这些小函数组合为新函数。

现在,我们怎么利用 compose 和 andThen 方法来完成函数的组合?

首先,定义两个简单的函数—— times2 和 squared。


  1. Function<Integer, Integer> times2 = e -> e * 2
  2. Function<Integer, Integer> squared = e -> e * e; 

接下来,使用 compose 和 andThen 将它们连起来。


  1. times2.compose(squared).apply(4);  
  2. // Returns 32 
  3. times2.andThen(squared).apply(4);  
  4. // Returns 64 

如你所见,compose 和 andThen 的不同之处是函数执行的顺序不同。compose 函数先执行参数,然后执行调用者,而 andThen 先执行调用者,然后再执行参数。

我们开始组合函数

我们先创建一个示例,演示如何利用这种方式创建可重用的小代码片段——然后我们以不同的方式组合这些代码片段。

考虑下面的问题。

有一个文章列表,现在需要根据不同的需求来过滤这些文章。

首先,我们介绍两个基本功能—— byAuthor 和 byTag——基于作者和标签来过滤文章。


  1. BiFunction<String, List<Article>, List<Article>> byAuthor = 
  2.     (name, articles) -> articles.stream() 
  3.         .filter(a -> a.getAuthor().equals(name)) 
  4.         .collect(Collectors.toList()); 
  5. BiFunction<String, List<Article>, List<Article>> byTag =  
  6.     (tag, articles) -> articles.stream() 
  7.         .filter(a -> a.getTags().contains(tag)) 
  8.         .collect(Collectors.toList()); 

两个函数都是 BiFunction——意味着需要两个参数。

byAuthor 接收作者名称和文章列表两个参数,返回根据作者过滤后的文章列表。

byTag 与此相同,接收标签和文章列表两个参数,返回根据标签过滤后的文章列表。

由于 BiFunction 接收两个参数,它只提供 andThen 函数。你不能将一个函数的结果放在一个接收两个参数的函数中,因此没有 compose 函数。

继续,我们还有一个基本功能,需对文章列表从新到旧进行排序,并返回排序后的文章列表。


  1. Function<List<Article>, List<Article>> sortByDate =  
  2.     articles -> articles.stream() 
  3.         .sorted((x, y) -> y.published().compareTo(x.published())) 
  4.         .collect(Collectors.toList()); 
  5. Function<List<Article>, Optional<Article>> first =  
  6.     a -> a.stream().findFirst(); 

现在,我们已经有了基本的函数,现在看我们怎么利用这些函数来组合成新的函数。

首先,我们组合一个返回最近发表的文章列表函数。


  1. Function<List<Article>, Optional<Article>> newest =  
  2.     first.compose(sortByDate); 

使用 first 这个函数以及我们之前创建的 sortByDate,我们能创建一个新的函数,该函数返回给定文章列表的最新文章。

我们可以继续通过不同的方式混合这些函数,从而可以组合出不同意义的函数,而不需要重复写代码。

找出作者的最新文章:


  1. BiFunction<String, List<Article>, Optional<Article>> newestByAuthor =  
  2.     byAuthor.andThen(newest); 

或者对某一作者的文章进行排序


  1. BiFunction<String, List<Article>, List<Article>> byAuthorSorted =  
  2.     byAuthor.andThen(sortByDate); 

或者你可能不关心作者,只想根据你喜欢标签获取最新的文章:


  1. BiFunction<String, List<Article>, Optional<Article>> newestByTag =  
  2.     byTag.andThen(newest); 

我想要表达的观点是:通过 Function 接口及其组合功能,可以创建小的代码块,再将其组合来满足你的需求,这样可以可以更简单、更有意思地实现 DRY 原则。

就这样了——利用 compose 和 andThen 来使用简单的方式组合功能。你也试试吧!


来源:51CTO