Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy equivalent for ruby all and none

I want to know if all (or none) of the items of a Collection in Groovy satisfy certain condition.

I know that in ruby (and in c# with linq), you can call methods like all? and none? and pass the condition as a closure to accomplish this.

Is there an equivalent for this methods in Groovy?

like image 690
Tomas Romero Avatar asked Jun 20 '12 04:06

Tomas Romero


1 Answers

Yes, its !any (for Ruby's none) and every (for Ruby's all):

def list = [1, 2]
assert !list.any { it < 0 }
assert list.every { it > 0 }

See also documentation at http://groovy.codehaus.org/JN1015-Collections

like image 76
Igor Artamonov Avatar answered Oct 31 '22 09:10

Igor Artamonov