dart - 相当于 Dart
问题描述
我刚刚开始探索 Dart 语言,我想测试我用 Java 编写的现有代码:
public interface Condition {
Condition FALSE = facts->false;
Boolean evaluate(Fact<?> fact);
default Condition and(Condition other) {
return fact-> this.evaluate(fact) && other.evaluate(fact);
}
default Condition or(Condition other) {
return fact-> this.evaluate(fact) || other.evaluate(fact);
}
}
调用者将其称为:
@Test
public void testCondition() {
String str = "A String";
Condition a = fact -> !str.isBlank();
Condition b = fact -> str.contains("A");
a.and(b);
}
使用它的完整测试类是:
public class AnonymousLoopTest {
@Test
public void test() {
RulesEngine rulesEngine = new InferenceRuleEngine();
List<Name> names = NamesFactory.fetchNames();
Rules rules = new Rules();
Facts facts = new Facts();
AtomicReference<Integer> countRef = new AtomicReference<>(1);
names.forEach(personName -> {
facts.put("name-" + countRef.get(), personName);
countRef.set(countRef.get()+1);
Condition condition = fact -> !personName.name().isEmpty();
//Hack the comparator logic of DefaultRule/BasicRule in order to override its internal logic as below.
//This is needed to register our Rule with Rules which uses a Set<Rule> to register new Rules
//with the comparator logic written in BasicRule.
Rule nameRule = new RuleBuilder((o1, o2) -> personName.name().compareTo(o1.getName()))
.when(condition).then(action -> System.out.println("In Action:" + personName)).build();
rules.register(nameRule);
});
rulesEngine.fire(rules, facts);
}
}
record Name(Integer id, String name){}
class NamesFactory{
static List<Name> fetchNames(){
return List.of(new Name(10, "Sara"), new Name(20, "Zara"), new Name(30, ""),new Name(40, "Lara"));
}
}
条件被when()
方法使用。在给定的示例中,空白名称将被过滤掉。其他三个名称将被打印。
我试图在 Dart 中编写和等效,但我只是卡住了。在 Dart 中编写这段代码的方法是什么?
解决方案
这看起来像我会做的事情:
typedef Condition = bool Function(Fact);
bool falseCondition(Fact _) => false;
extension ConditionComposition on Condition {
Condition operator &(Condition other) => (Fact fact) => this(fact) && other(fact);
Condition operator |(Condition other) => (Fact fact) => this(fact) || other(fact);
Condition operator ~() => (Fact fact) => !this(fact);
}
如果您坚持为函数对象提供一个包装类,我会这样做:
class Condition {
static const Condition falseCondition = Condition(_kFalse);
final bool Function(Fact) _test;
const Condition(bool test(Fact fact)) : _test = test;
bool evaluate(Fact fact) => _test(fact);
Condition operator &(Condition other) => Condition((fact) =>
this.evaluate(fact) && other.evaluate(fact));
Condition operator |(Condition other) => Condition((fact) =>
this.evaluate(fact) || other.evaluate(fact));
static bool _kFalse(_) => false;
}
但是对于实际上只是一个简单功能的东西来说,一个类似乎有点矫枉过正。Dart 具有一流的功能。
您可以将以前的版本用作:
test("test Condition", () {
var str = "A String";
Condition a = (fact) => str.isNotEmpty();
Condition b = (fact) => str.contains("A");
var both = a & b;
expect(both(someDefaultFact), true);
}
推荐阅读
- java - java - 如何在Java Spring Boot中将一个对象映射到另一个对象
- json - 如何在父级别要求中引用子属性
- python - 如何独立运行 websockets
- html - 如何使用表单操作将数据从 HTML FORM 发送到 http 服务器?
- amazon-web-services - 将 aws 负载均衡器流量重定向到 azure VM
- python - 我如何匹配 SequenceMatcher 的最佳比例
- javascript - 数月经验 - Javascript
- angular - 我如何在同一个角度项目中实现 2 个 html 主题
- sql - 对 Presto 中的所有按值分组的值执行按位运算
- linq - 将复杂的 IQueryable LINQ 重构为子方法