# Iterators: take filter countfrom

**URL:** <https://discourse.julialang.org/t/iterators-take-filter-countfrom/20352>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 1, 2019, 12:33pm UTC](https://discourse.julialang.org/t/iterators-take-filter-countfrom/20352 "2019-02-01T12:33:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [February 1, 2019, 12:33pm UTC](https://discourse.julialang.org/t/iterators-take-filter-countfrom/20352/1 "2019-02-01T12:33:30Z")

</div>

Is there a more compact form to write

```
n -> Iterators.take(Iterators.filter(predicate, Iterators.countfrom(1)), n) ?

```

_Edit:_ The wording of my question has obviously given the impression that I am only concerned with syntactic sugar. Firstly I wanted to make sure that I had not overlooked an existing iterator. But secondly, to me this iterator seems to describe a very common task. Therefore I wonder whether it is worth building it explicitly, and if so, look if someone already has such a solution in the drawer.

If not, I will give bennedich’s solution the preference.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 1, 2019, 1:32pm UTC](https://discourse.julialang.org/t/iterators-take-filter-countfrom/20352/2 "2019-02-01T13:32:56Z")

</div>

That looks pretty legible to me. I guess you could import the functions from Iterators if you wanted to:

```julia
import Base.Iterators: take, filter, countfrom

n -> take(filter(predicate, countfrom(1)), n)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 1, 2019, 2:10pm UTC](https://discourse.julialang.org/t/iterators-take-filter-countfrom/20352/3 "2019-02-01T14:10:48Z")

</div>

In addition to what @bennedich wrote, I often find it more readable to give a name to interim values, eg

```julia
function first_filtered_n(predicate, n)
    filtered = Iterators.filter(predicate, Iterators.countfrom(1))
    Iterators.take(filtered, n)
end

```

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [February 1, 2019, 8:40pm UTC](https://discourse.julialang.org/t/iterators-take-filter-countfrom/20352/4 "2019-02-01T20:40:47Z")

</div>

You can use [Query.jl](https://github.com/queryverse/Query.jl)’s piping syntax. Which some people love, and some hate 🙂

```julia
using Query

n -> Iterators.countfrom(1) |> @filter(predicate(_)) |> @take(n)

```
