# A for loop where the counter isn't used

**URL:** <https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [January 11, 2019, 7:56pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535 "2019-01-11T19:56:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [January 11, 2019, 7:56pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/1 "2019-01-11T19:56:34Z")

</div>

I sometimes need to repeat some code `n` times, but I don’t care what iteration I’m on. In those cases I do this:

```julia
for _ in 1:n 
    ...
end

```

Is there anything out there better than that? I’m merely curious if I’ve missed some construct/function that better suits this use-case.  
Thanks!

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [January 11, 2019, 8:09pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/2 "2019-01-11T20:09:11Z")

</div>

Not sure if it’s worth it but here is a macro 😉

```julia
macro loop(n, ex)
    return quote
        for _ ∈ 1:$n
            $ex
        end
    end
end

@loop 3 println("hello")

@loop 10 begin
    println("yep")
end

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [January 11, 2019, 8:09pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/3 "2019-01-11T20:09:33Z")

</div>

I don’t know if it is intentional or documented, but the empty tuple `()` seems to work as a “non-variable”. For example, `for () in 1:n` will work, and make it clear that you will not use the iteration index.

(Similarly, ((), x) = foo()` can be a way of expressing that you only care about the second element of a returned tuple.)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 11, 2019, 8:34pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/4 "2019-01-11T20:34:29Z")

</div>

The underscore `_` is probably a good choice, as it’s actually meant to be used in Julia as a “don’t care” variable. In fact, as of Julia 1.0 it’s illegal to read from `_` (you can only assign to it), so the compiler is free to ignore anything you assign to it, opening up additional optimization possibilities.

```julia
julia> _, x = (1, 2)
(1, 2)

julia> x
2

julia> y = _
ERROR: syntax: all-underscore identifier used as rvalue

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 11, 2019, 8:42pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/5 "2019-01-11T20:42:46Z")

</div>

> [@rdeits](#):
>
> opening up additional optimization possibilities.

The compiler doesnt need this help. It only helps the reader.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 11, 2019, 11:39pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/6 "2019-01-11T23:39:53Z")

</div>

See also the discussion at

[https://github.com/JuliaLang/julia/issues/2675](https://github.com/JuliaLang/julia/issues/2675)

---

<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:** [January 12, 2019, 10:25am UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/7 "2019-01-12T10:25:31Z")

</div>

I think just using `_` is the best solution. It nicely covers a special case, and works fine in other contexts (closures) if you restructure your code.

---

<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:** [January 12, 2019, 8:55pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/8 "2019-01-12T20:55:41Z")

</div>

From a readability and performance point of view, `for _ = 1:N` is the way to go. However, Julia is not short of alternatives. Here are some:

```julia
julia> for _=1:3 println("loop") end
loop
loop
loop

julia> foreach(_->println("loop"),1:3)
loop
loop
loop

julia> map(_->println("loop"),1:3);
loop
loop
loop

julia> (1:3).|>_->println("loop");
loop
loop
loop

julia> (_->println("loop")).(1:3);
loop
loop
loop

julia> [println("loop") for _=1:3];
loop
loop
loop

julia> eval.(fill(:(println("loop")),3));
loop
loop
loop

julia> eval(Meta.parse("println(\"loop\");"^3))
loop
loop
loop

```

---

<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:** [January 13, 2019, 12:28pm UTC](https://discourse.julialang.org/t/a-for-loop-where-the-counter-isnt-used/19535/9 "2019-01-13T12:28:45Z")

</div>

Note that strictly speaking, only the first one is a loop (so that it can be used to eg accumulate a local state).

Also, the last two ones are kind of icky, too; `eval`ing code parsed from plain text is not something one would do in practice.
