|
| 1 | +--- |
| 2 | +title: Combinator |
| 3 | +category: Idiom |
| 4 | +language: zh |
| 5 | +tag: |
| 6 | + - Reactive |
| 7 | +--- |
| 8 | + |
| 9 | +## 或称 |
| 10 | + |
| 11 | +构图模式 |
| 12 | + |
| 13 | +## 目的 |
| 14 | + |
| 15 | +功能模式代表了一种以组合功能为中心的图书馆组织风格。 |
| 16 | +简单地说,有一些类型 T,一些用于构造类型 T 的“原始”值的函数,以及一些可以以各种方式组合类型 T 的值以构建更复杂的类型 T 值的“组合器”。 |
| 17 | + |
| 18 | +## 解释 |
| 19 | + |
| 20 | +真实世界例子 |
| 21 | + |
| 22 | +> 在计算机科学中,组合逻辑被用作计算的简化模型,用于可计算性理论和证明理论。 尽管组合逻辑很简单,但它捕获了计算的许多基本特征。 |
| 23 | +> |
| 24 | +
|
| 25 | +通俗的说 |
| 26 | +> 组合器允许从先前定义的“事物”创建新的“事物”。 |
| 27 | +> |
| 28 | +
|
| 29 | +维基百科说 |
| 30 | + |
| 31 | +> 组合器是一个高阶函数,仅使用函数应用程序和之前定义的组合器来定义其参数的结果。 |
| 32 | +> |
| 33 | +
|
| 34 | +**程序示例** |
| 35 | + |
| 36 | +翻译上面的组合器示例。 首先,我们有一个由几个方法`contains`, `not`, `or`, `and`组成的接口。 |
| 37 | + |
| 38 | +```java |
| 39 | +// 用于查找文本中的行的功能界面。 |
| 40 | +public interface Finder { |
| 41 | + |
| 42 | + // 在文本中查找行的函数。 |
| 43 | + List<String> find(String text); |
| 44 | + |
| 45 | + // 函数{@link #find(String)}的简单实现。 |
| 46 | + static Finder contains(String word) { |
| 47 | + return txt -> Stream.of(txt.split("\n")) |
| 48 | + .filter(line -> line.toLowerCase().contains(word.toLowerCase())) |
| 49 | + .collect(Collectors.toList()); |
| 50 | + } |
| 51 | + |
| 52 | + // 组合器:not。 |
| 53 | + default Finder not(Finder notFinder) { |
| 54 | + return txt -> { |
| 55 | + List<String> res = this.find(txt); |
| 56 | + res.removeAll(notFinder.find(txt)); |
| 57 | + return res; |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + // 组合器:or。 |
| 62 | + default Finder or(Finder orFinder) { |
| 63 | + return txt -> { |
| 64 | + List<String> res = this.find(txt); |
| 65 | + res.addAll(orFinder.find(txt)); |
| 66 | + return res; |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + // 组合器:and。 |
| 71 | + default Finder and(Finder andFinder) { |
| 72 | + return |
| 73 | + txt -> this |
| 74 | + .find(txt) |
| 75 | + .stream() |
| 76 | + .flatMap(line -> andFinder.find(line).stream()) |
| 77 | + .collect(Collectors.toList()); |
| 78 | + } |
| 79 | + ... |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +然后我们还有另一个组合器用于一些复杂的查找器`advancedFinder`, `filteredFinder`, `specializedFinder`和`expandedFinder`。 |
| 84 | + |
| 85 | +```java |
| 86 | +// 由简单取景器组成的复杂取景器。 |
| 87 | +public class Finders { |
| 88 | + |
| 89 | + private Finders() { |
| 90 | + } |
| 91 | + |
| 92 | + // Finder 用于查找复杂的查询。 |
| 93 | + public static Finder advancedFinder(String query, String orQuery, String notQuery) { |
| 94 | + return |
| 95 | + Finder.contains(query) |
| 96 | + .or(Finder.contains(orQuery)) |
| 97 | + .not(Finder.contains(notQuery)); |
| 98 | + } |
| 99 | + |
| 100 | + // 过滤查找器也会查找包含排除查询的查询。 |
| 101 | + public static Finder filteredFinder(String query, String... excludeQueries) { |
| 102 | + var finder = Finder.contains(query); |
| 103 | + |
| 104 | + for (String q : excludeQueries) { |
| 105 | + finder = finder.not(Finder.contains(q)); |
| 106 | + } |
| 107 | + return finder; |
| 108 | + } |
| 109 | + |
| 110 | + // 专门查询。 每个下一个查询都会在上一个结果中查找。 |
| 111 | + public static Finder specializedFinder(String... queries) { |
| 112 | + var finder = identMult(); |
| 113 | + |
| 114 | + for (String query : queries) { |
| 115 | + finder = finder.and(Finder.contains(query)); |
| 116 | + } |
| 117 | + return finder; |
| 118 | + } |
| 119 | + |
| 120 | + // 扩展查询。 寻找替代品。 |
| 121 | + public static Finder expandedFinder(String... queries) { |
| 122 | + var finder = identSum(); |
| 123 | + |
| 124 | + for (String query : queries) { |
| 125 | + finder = finder.or(Finder.contains(query)); |
| 126 | + } |
| 127 | + return finder; |
| 128 | + } |
| 129 | + ... |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +现在我们已经创建了组合器的接口和方法。 现在我们有一个处理这些组合器的应用程序。 |
| 134 | + |
| 135 | +```java |
| 136 | +var queriesOr = new String[]{"many", "Annabel"}; |
| 137 | +var finder = Finders.expandedFinder(queriesOr); |
| 138 | +var res = finder.find(text()); |
| 139 | +LOGGER.info("the result of expanded(or) query[{}] is {}", queriesOr, res); |
| 140 | + |
| 141 | +var queriesAnd = new String[]{"Annabel", "my"}; |
| 142 | +finder = Finders.specializedFinder(queriesAnd); |
| 143 | +res = finder.find(text()); |
| 144 | +LOGGER.info("the result of specialized(and) query[{}] is {}", queriesAnd, res); |
| 145 | + |
| 146 | +finder = Finders.advancedFinder("it was", "kingdom", "sea"); |
| 147 | +res = finder.find(text()); |
| 148 | +LOGGER.info("the result of advanced query is {}", res); |
| 149 | + |
| 150 | +res = Finders.filteredFinder(" was ", "many", "child").find(text()); |
| 151 | +LOGGER.info("the result of filtered query is {}", res); |
| 152 | + |
| 153 | +private static String text() { |
| 154 | + return |
| 155 | + "It was many and many a year ago,\n" |
| 156 | + + "In a kingdom by the sea,\n" |
| 157 | + + "That a maiden there lived whom you may know\n" |
| 158 | + + "By the name of ANNABEL LEE;\n" |
| 159 | + + "And this maiden she lived with no other thought\n" |
| 160 | + + "Than to love and be loved by me.\n" |
| 161 | + + "I was a child and she was a child,\n" |
| 162 | + + "In this kingdom by the sea;\n" |
| 163 | + + "But we loved with a love that was more than love-\n" |
| 164 | + + "I and my Annabel Lee;\n" |
| 165 | + + "With a love that the winged seraphs of heaven\n" |
| 166 | + + "Coveted her and me."; |
| 167 | + } |
| 168 | +``` |
| 169 | + |
| 170 | +**程序输出:** |
| 171 | + |
| 172 | +```java |
| 173 | +the result of expanded(or) query[[many, Annabel]] is [It was many and many a year ago,, By the name of ANNABEL LEE;, I and my Annabel Lee;] |
| 174 | +the result of specialized(and) query[[Annabel, my]] is [I and my Annabel Lee;] |
| 175 | +the result of advanced query is [It was many and many a year ago,] |
| 176 | +the result of filtered query is [But we loved with a love that was more than love-] |
| 177 | +``` |
| 178 | + |
| 179 | +现在我们可以设计我们的应用程序,使其具有查询查找功能`expandedFinder`, `specializedFinder`, `advancedFinder`, `filteredFinder`,这些功能均派生自`contains`, `or`, `not`, `and`。 |
| 180 | + |
| 181 | + |
| 182 | +## 类图 |
| 183 | + |
| 184 | + |
| 185 | +## 适用性 |
| 186 | +在以下情况下使用组合器模式: |
| 187 | + |
| 188 | +- 您可以从更简单的值创建更复杂的值,但具有相同的类型(它们的组合) |
| 189 | + |
| 190 | +## 好处 |
| 191 | + |
| 192 | +- 从开发人员的角度来看,API 由领域中的术语组成。 |
| 193 | +- 组合阶段和应用阶段之间有明显的区别。 |
| 194 | +- 首先构造一个实例,然后执行它。 |
| 195 | +- 这使得该模式适用于并行环境。 |
| 196 | + |
| 197 | + |
| 198 | +## 现实世界的例子 |
| 199 | + |
| 200 | +- java.util.function.Function#compose |
| 201 | +- java.util.function.Function#andThen |
| 202 | + |
| 203 | +## 鸣谢 |
| 204 | + |
| 205 | +- [Example for java](https://gtrefs.github.io/code/combinator-pattern/) |
| 206 | +- [Combinator pattern](https://wiki.haskell.org/Combinator_pattern) |
| 207 | +- [Combinatory logic](https://wiki.haskell.org/Combinatory_logic) |
0 commit comments