# @foreach item in vector

**URL:** <https://discourse.julialang.org/t/foreach-item-in-vector/69024>\
**Category:** General Usage\
**Tags:** macros, unrolling\
**Created:** [October 1, 2021, 4:01am UTC](https://discourse.julialang.org/t/foreach-item-in-vector/69024 "2021-10-01T04:01:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [October 1, 2021, 4:01am UTC](https://discourse.julialang.org/t/foreach-item-in-vector/69024/1 "2021-10-01T04:01:34Z")

</div>

I want to run a bit of code for each item in a vector, as if I’d typed it out multiple types. Not using a function or run-time loop. How can I do that?

Similar to this but with no function:

```julia
foreach(1:5) do x
    println(x)
end

```

---

<div class="post-metadata">

**Author:** ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)\
**Post date:** [October 1, 2021, 4:48am UTC](https://discourse.julialang.org/t/foreach-item-in-vector/69024/2 "2021-10-01T04:48:23Z")

</div>

Use the map command.

```julia
v = [1, 2, 3, 4, 5] # example vector with 5 elements
map(x -> x^3, v)

```

The will cube each element of vector v with the result.

```julia
5-element Vector{Int64}:
   1
   8
  27
  64
 125

```

---

<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:** [October 1, 2021, 5:16am UTC](https://discourse.julialang.org/t/foreach-item-in-vector/69024/3 "2021-10-01T05:16:00Z")

</div>

This is usually referred to as “unrolling”. You can’t do this in general for vectors in julia because a vector can hold a variable number of items (under some restrictions maybe you can for your use case). It is possible for StaticArrays and Tuples though.  
There is [https://github.com/cstjean/Unrolled.jl](https://github.com/cstjean/Unrolled.jl) and [https://github.com/StephenVavasis/Unroll.jl](https://github.com/StephenVavasis/Unroll.jl) and maybe other packages as well.
