Checking that all items in a collection match a predicate in Scala

萝らか妹 提交于 2020-02-12 08:27:07

问题


What's the most idiomatic way to test whether all items of a collection match a predicate?

Any item?


回答1:


There are built-in functions for this:

List(1,2,3,4).forall(x => x < 5)
res0: Boolean = true

for any:

List(1,2,3,4).exists(x => x > 3)
res1: Boolean = true


来源:https://stackoverflow.com/questions/15932137/checking-that-all-items-in-a-collection-match-a-predicate-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!