# Iterable collections any()

**URL:** <https://discourse.julialang.org/t/iterable-collections-any/73631>\
**Category:** New to Julia\
**Created:** [December 26, 2021, 2:07am UTC](https://discourse.julialang.org/t/iterable-collections-any/73631 "2021-12-26T02:07:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 26, 2021, 2:07am UTC](https://discourse.julialang.org/t/iterable-collections-any/73631/1 "2021-12-26T02:07:35Z")

</div>

‘’’  
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?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 26, 2021, 2:11am UTC](https://discourse.julialang.org/t/iterable-collections-any/73631/2 "2021-12-26T02:11:39Z")

</div>

[https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation)

---

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 26, 2021, 2:43am UTC](https://discourse.julialang.org/t/iterable-collections-any/73631/3 "2021-12-26T02:43:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 26, 2021, 3:08am UTC](https://discourse.julialang.org/t/iterable-collections-any/73631/4 "2021-12-26T03:08:55Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [December 27, 2021, 2:36pm UTC](https://discourse.julialang.org/t/iterable-collections-any/73631/5 "2021-12-27T14:36:40Z")

</div>

> [@apoorvvyas52](#):
>
> when the value of i will be 3,4 or 5 then i\>5 will be false and hence the other condition println (“great”)

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`).
