【问题标题】:Equivalent in Dart相当于 Dart
【发布时间】:2020-08-11 15:56:54
【问题描述】:

我刚刚开始探索 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 怎么写?

【问题讨论】:

  • 您能否通过一个示例来详细介绍一下这个接口是如何实现的。还举一个这个类的使用例子。

标签: dart


【解决方案1】:

这看起来像我会做的事情:

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);
  }

【讨论】:

  • 第一种方法很简洁,看起来很有前途。我将尝试实施第一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 2012-09-23
  • 2016-09-29
  • 2020-01-29
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
相关资源
最近更新 更多