检查集合中的所有项目是否与 Scala 中的谓词匹配 [英] Checking that all items in a collection match a predicate in Scala
本文介绍了检查集合中的所有项目是否与 Scala 中的谓词匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
测试集合的所有项目是否与谓词匹配的最惯用的方法是什么?
What's the most idiomatic way to test whether all items of a collection match a predicate?
任何项目?
推荐答案
为此有内置函数:
List(1,2,3,4).forall(x => x < 5)
res0: Boolean = true
对于任何:
List(1,2,3,4).exists(x => x > 3)
res1: Boolean = true
这篇关于检查集合中的所有项目是否与 Scala 中的谓词匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文