# Iterable collections any()

‘’’
any(i → ( println(i);i > 5),3:8) && println(“great”)
‘’’
output is :

3
4
5
6
great

‘’’
any(i → ( println(i);i > 5),3:8) || println(“great”)
‘’’
output is:
3
4
5
6
true

Can anyone explain why is there “great” at the end of the output in the first case and “true” at the end of the output in the second case? I am unable to understand the code. How does && and || effect the above code?

Still not clear. Let me explain why:
In the second case
‘’’
any(i → ( println(i);i > 5),3:8) || println(“great”)
‘’’
when the value of i will be 3,4 or 5 then i>5 will be false and hence the other condition println (“great”) should be evaluated. So hypothetically the output should be
"great
great
great
6
‘’’
What are your thoughts on this? The above hypothetical output satisfies the short circuit evaluation logic argument whose link you have sent me.

For your code to work in the way you described then you should write:

``````any(i -> (i > 5 ? println(i) : println("great"); i > 5), 3:8)
``````

The `||` after the `any` call will only evaluate a single time, and it will depend on the return of `any`, not the `true` or `false` value for each element. Also, you need to repeat the expression at the end of the lambda expression because the ternary operator will return the value returned by the evaluated `println` otherwise (which is nothing).

Also, it seems like you are using simple quotes three times, instead of triple backticks to quote your code. You should be using ` one time for inline code, and three times for block code, for my non-USA keyboard this is done by clicking the acute accent and then space.

2 Likes

You seem to assume that `any` is evaluated like a loop once for each element in the collection `3:8`, this is not correct. The function `any` is a short-circuiting construct as @lmiq pointed out, which means it will exit at the first occurrence of `true`. In your example, `any` will exit with the value `true` when `i` becomes `6`, at this point the RHS of `||` needn’t be executed because `||` is also a short-circuiting operator, and you only see what was returned by `any` after printing `6` (that last `true`).

1 Like