# Fastest approach to returning a vector of elements choose 2

**URL:** <https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 21, 2024, 3:31am UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494 "2024-02-21T03:31:07Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [February 21, 2024, 10:29pm UTC](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494/16 "2024-02-21T22:29:01Z")

</div>

> [@narnold0](#):
>
> but how can one do such a thing in a single line if statement without calling something twice? Sorry if thats a dumb question lol

it is a good question, and one that has been discussed in the past

> [@Proposal to extend the syntax of list comprehensions with a \`let\` clause](https://discourse.julialang.org/t/proposal-to-extend-the-syntax-of-list-comprehensions-with-a-let-clause/87256):
>
> I would like to propose a new syntactic sugar to the list comprehension syntax. Take the following, somewhat forced example: [2abs(x) for x in -20:20 if abs(x) \> 10] Here, the abs function is invoked twice. Of course, in this case, it is not a big deal, but it could be a more costly computation, or just a longer expression, which I don’t want to invoke or write down twice. So I can apply the following trick: [2abs\_x for x in -20:20 for abs\_x in [abs(x)] if abs\_x \> 10] Which works fine,…

I don’t think such a feature was ever ruled out, but it has also not yet been implemented

If you have this need, it may be difficult to do it as a snazzy comprehension one-liner.

You may notice that `(i for i in 1:10 if iseven(i))` internally uses `Iterators.Filter`, but you are free to call `Iterators.Filter` directly

- `(foo(x) for x in itr if cond(foo(x)))`

becomes

- `Iterators.Filter(cond, (foo(x) for x in itr))`

---

_[View the full topic](https://discourse.julialang.org/t/fastest-approach-to-returning-a-vector-of-elements-choose-2/110494)._
