返回布尔语句列表的惰性求值,只要其中任何一个为真。

huangapple go评论65阅读模式
英文:

Return lazy evaluation of list of boolean statements , with any of it true

问题

有一系列布尔决策每个决策都有不同的条件需要评估如果其中任何一个评估为true就应该返回问题是我不希望它们被急切地评估我这里有两种实现第一种可以工作但是我有10个这样的决策要做所以我希望将它们全部作为列表获取但是一旦我把它们放入列表/流中它就会评估所有的决策从而失去了目的我想要的是如果任何一个条件评估为true它应该立即停止并返回其余条件不应该被执行

```java
BooleanSupplier a = () -> compute("bb");
BooleanSupplier b = () -> computeSecond("aa");

// 这个可以工作
System.out.println("Lazy匹配 => " + lazyMatch(a, b));

// 这个不可以
System.out.println("Lazy匹配列表 => " + lazyMatchList(Stream.of(a, b)));

static boolean lazyMatch(BooleanSupplier a, BooleanSupplier b) {
    return a.getAsBoolean() || b.getAsBoolean();
}

static boolean lazyMatchList(Stream<BooleanSupplier> lazyBooleanList) {
    return lazyBooleanList.anyMatch(BooleanSupplier::getAsBoolean);
}

static boolean compute(String str) {
    System.out.println("执行中...");
    return str.contains("ac");
}

// computeSecond类似于compute
英文:

I have a series of boolean decision, each having a different condition to evaluate. If any of it evaluates to true it should return. Problem is, I do not want them to be eagerly evaluated. I have two implementations here, the first one works, but i have 10 such decisions to make so i want to get them all as a list, as soon as I put them into list/stream it evaluates all the decisions and fails the purpose. What I want is, if any of the condition evaluates to true it should stop right there and return, rest of the conditions should not get executed.

BooleanSupplier a = () -&gt; compute(&quot;bb&quot;);
BooleanSupplier b = () -&gt; computeSecond(&quot;aa&quot;);

// this works
System.out.println(&quot;Lazy match =&gt; &quot; + lazyMatch(a, b));

// this doesn&#39;t
System.out.println(&quot;Lazy match list =&gt; &quot; + lazyMatchList(Stream.of(a,b)));

static boolean lazyMatch(BooleanSupplier a, BooleanSupplier b) {
	    return a.getAsBoolean() || b.getAsBoolean();
}

static boolean lazyMatchList(Stream&lt;BooleanSupplier&gt; lazyBooleanList) {
	    return  lazyBooleanList.anyMatch(BooleanSupplier::getAsBoolean);
}

static boolean compute(String str) {
	    System.out.println(&quot;executing...&quot;);
	    return str.contains(&quot;ac&quot;);
}

// compute2 is something similar to compute

答案1

得分: 1

尝试这个:

List<Predicate<String>> predicates = Arrays.asList(this::compute1, this::compute2, ...);

boolean anyTrue(String str) {
    return predicates.stream()
      .anyMatch(p -> p.test(str));
}

这会在第一个谓词返回 true 时返回 true

将这个方法用作过滤谓词:

someStringStream.filter(this::anyTrue)...
英文:

Try this:

List&lt;Predicate&lt;String&gt;&gt; predicates = Arrays.asList(this::compute1, this::compute2, ...);

boolean anyTrue(String str) {
    return predicates.stream()
      .anyMatch(p -&gt; p.test(str));
}

This returns true when the first predicate returns true.

Use the method as a filter predicate:

someStringStream.filter(this::anyTrue)...

huangapple
  • 本文由 发表于 2020年10月4日 16:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64192393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定