- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我通过几个谓词过滤列表,例如
val xs = List(1, 0, -1, 2, 3, 4, 5, -6, 5, 0)
val pred1: Int => Boolean = _ > 0
val pred2: Int => Boolean = _ < 0
val pred3: Int => Boolean = _ % 2 == 0
val xs1 = xs.filter(pred1) // List(1, 2, 3, 4, 5, 5)
val xs2 = xs.filter(pred2) // List(-1, -6)
val xs3 = xs.filter(pred3) // List(0, 2, 4, -6, 0)
如何一次通过所有这些谓词过滤列表?
def filterByFew(xs: List[Int], preds: List[Int => Boolean]): List[List[Int]] = ???
filterByFew(xs, List(pred1, pred2, pred3))
应该返回
列表(列表(1, 2, 3, 4, 5, 5), 列表(-1, -6), 列表(0, 2, 4, -6, 0))
最佳答案
仍然多次遍历集合的一行答案:
List(pred1, pred2, pred3).map(xs.filter)
作为方法:
def filterByFew(xs: List[Int], preds: List[Int => Boolean]): List[List[Int]] =
preds.map(xs.filter)
它以几乎相同的方式处理流:
val p1 = (x: Int) => x % 2 == 0
val p2 = (x: Int) => x % 3 == 0
val preds = List(p1, p2)
val str = Stream.from(0)
val filteredStreams = preds.map(str.filter)
filteredStreams foreach { s => println(s.take(10).toList) }
// Output:
// List(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)
// List(0, 3, 6, 9, 12, 15, 18, 21, 24, 27)
但不要在 REPL 中尝试:REPL 挂起为什么要显示中间结果。
遍历集合一次
如果你真的无法承受多次遍历集合,那么我看不到任何有效的解决方法,最简单的事情似乎是重新实现 filter
,但使用多个可变构建器:
def filterByMultiple[A](
it: Iterator[A],
preds: List[A => Boolean]
): List[List[A]] = {
val n = preds.size
val predsArr = preds.toArray
val builders = Array.fill(n){
new collection.mutable.ListBuffer[A]
}
for (a <- it) {
for (j <- 0 until n) {
if (predsArr(j)(a)) {
builders(j) += a
}
}
}
builders.map(_.result)(collection.breakOut)
}
filterByMultiple((0 to 30).iterator, preds) foreach println
// Output:
// List(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30)
// List(0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30)
如果您是通过 Google 搜索来到这里的,您可能还想要其他东西:
AND 多个谓词:
def filterByAnd(xs: List[Int], preds: List[Int => Boolean]) =
xs.filter(x => preds.forall(p => p(x)))
OR-ing 多个谓词:
def filterByOr(xs: List[Int], preds: List[Int => Boolean]) =
xs.filter(x => preds.exists(p => p(x)))
关于scala - 如何一次通过多个谓词过滤列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53652520/
我正在开发一个包含 TreeViewer 的 RCP 应用程序,我想在其上激活多个应与“OR”谓词配合使用的过滤器,例如: A |--B |--|--redColor |--|--blueColor
我的问题是关于 enable_if通常标准库中的谓词,但我将在迭代器类型的上下文中构建它,因为这是我目前遇到此问题的地方。 我有一个自定义迭代器类型 It , 这样 std::iterator_tra
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我需要使用自然数为 2 的幂创建一个 Prolog 谓词。 自然数是:0、s(0)、s(s(0)) 等等。 例如: ?- pow2(s(0),P). P = s(s(0)); false. ?- po
我正在尝试创建一个 NSPredicate 来查找在特定日期范围内包含“ session ”的“项目”。我一开始尝试过这个: [NSPredicate predicateWithFormat:@"AN
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
在JOOQ中,我可以编写如下SQL这样的代码吗? 我不知道如何编写具有多个字段的in谓词。 select some_value from t1 where (t1.id1, t1.id2) i
我正在用 PROLOG 编写一个数独求解器。我希望求解器能够处理所有可能大小的数独,因此我自然需要构造采用可变数量参数的谓词。 (例如在数独中构建“ block ”。) 如何构造或模拟具有可变数量参数
我有一个包含一些 id 的列表,我希望我的 ebean 查询排除这些 id。所以基本上我需要在 ebean 中使用“not in”谓词,就像 sql 一样,但遗憾的是找不到。还有其他方法可以实现这一目
我正在构建一个通用接口(interface)以从类中公开选定的字符串属性,然后我想在每个字段中搜索文本,以检查它是否匹配。 这是我的 IFieldExposer接口(interface): using
我将 Spring Boot 与 Spring JPA 和 Specification Executor 结合使用。我的规范/谓词组合成功地在我的类中搜索了简单的属性。但是,我在搜索其中的对象时遇到了
如果下面的 last_name 为 NULL,它会跳过该列的 WHERE 比较以提高性能吗? AND (last_name IS NULL OR sp.last_name LIKE CONCAT('%
出于好奇:如果我有一个接受多个参数(通常为 1 或 2)并返回 3 个值中的 1 个(而不是 bool 值 true 或 false)的类运算符(或函数等),它是否仍应被调用谓词?还是模糊逻辑的特例?
是否可以创建一个采用装箱值类型并返回该值类型是否等于该类型默认值的方法? 所以我想创建一个具有以下签名的方法: bool IsDefault(object boxedValueType); 注意:当
let selectedConsoles = ["Xbox", "Playstation 4"] let players = realm.objects(Person).filter("console
我正在尝试根据用户搜索文本过滤来自核心数据的结果,但效果很好。我正在努力做到有几个关键术语可以返回特定结果。 我有一个Colour 实体,它与另一个实体ProjectColour 具有对多 关系。 P
std::vector lines; typedef std::vector::iterator iterator_t; iterator_t eventLine = std::find_if(lin
我想在一个列表中找到一个元素的索引,该列表匹配某个谓词,有没有比以下更好的方法: var index = list.IndexOf(list.Find(predicate)); ? 最佳答案 你在找
我正在使用缺少 findall 的高阶 Prolog 变体. 还有一个关于实现我们自己的问题 findall这里:Getting list of solutions in Prolog . 低效的实现
我正在使用 Breeze 过滤客户端请求的数据。我的代码看起来有点像这样: 客户端 - 创建过滤谓词 var predicates = []; var criteriaPredicate = null
我是一名优秀的程序员,十分优秀!