# How to create a @ntuple-like macro but can hundle arbitrary number of nested loops

**URL:** <https://discourse.julialang.org/t/how-to-create-a-ntuple-like-macro-but-can-hundle-arbitrary-number-of-nested-loops/38180>\
**Category:** New to Julia\
**Created:** [April 25, 2020, 7:29am UTC](https://discourse.julialang.org/t/how-to-create-a-ntuple-like-macro-but-can-hundle-arbitrary-number-of-nested-loops/38180 "2020-04-25T07:29:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 25, 2020, 7:29am UTC](https://discourse.julialang.org/t/how-to-create-a-ntuple-like-macro-but-can-hundle-arbitrary-number-of-nested-loops/38180/1 "2020-04-25T07:29:32Z")

</div>

I would like to create a macro that can hundle arbitrary number of nested loops:

```julia
julia> @ntuple 3 i -> 2i
(2, 4, 6)

julia> FEM.@ntuple 2 3 (i,j) -> (2i,j)
((2, 1), (4, 1), (2, 2), (4, 2), (2, 3), (4, 3))

```

Is there anyway to to this?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 25, 2020, 8:01am UTC](https://discourse.julialang.org/t/how-to-create-a-ntuple-like-macro-but-can-hundle-arbitrary-number-of-nested-loops/38180/2 "2020-04-25T08:01:46Z")

</div>

There are macros that do that in Base Julia, but you can probably avoid them by reading this blog post: [Multidimensional algorithms and iteration](https://julialang.org/blog/2016/02/iteration/)

---

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 25, 2020, 8:25am UTC](https://discourse.julialang.org/t/how-to-create-a-ntuple-like-macro-but-can-hundle-arbitrary-number-of-nested-loops/38180/3 "2020-04-25T08:25:18Z")

</div>

Thank you for your reply! But Base Julia cannot do the later example but only the first one.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [April 25, 2020, 9:16pm UTC](https://discourse.julialang.org/t/how-to-create-a-ntuple-like-macro-but-can-hundle-arbitrary-number-of-nested-loops/38180/4 "2020-04-25T21:16:07Z")

</div>

It can actually, and depending on what _exactly_ you’re trying to do… it still probably can.

Using `CartesianIndices`, which I think is the main takeaway of that blog post, is extremely powerful. I’ve gotten used to using it in the context of combinatorics (often things “look” like multidimensional array indices even when they’re not), and it’s always amazed me how far that can take you.

If that doesn’t do the trick, a macro for N-loops already exists. `Base.@nloops` will no doubt do what you want, it just might be the wrong tool for most cases.
