I want to test and see if everything in an array passes my check, here is how I am presently doing it and my fantasy code which crashes the compiler.
Current:
def mylist = [1,2,3,4]
def presumeTrue = true
mylist.each{
if(it<0)presumeTrue=false
}
if(presumeTrue)println "Everything was greater than 0!!"
Fantasy:
def mylist = [1,2,3,4]
if(mylist*>0)println "Everything was greater than 0, but this sugar doesn't work."
Is there a correct way to apply an if test to everything in a list one one line?
Use the
everymethod:The operator you were trying to use is “spread dot”, which is
*.(not*). You’d need to use the method name (compareTo), which takes an argument. Butmapisn’t what you’re trying to do.You’re not trying to apply the method to all of
mylist‘s members, you’re trying to aggregate the result of applying the method to all the members, more like: