# How to prevent using global arrays in iteration in the module

**URL:** <https://discourse.julialang.org/t/how-to-prevent-using-global-arrays-in-iteration-in-the-module/65857>\
**Category:** General Usage\
**Created:** [August 5, 2021, 4:53am UTC](https://discourse.julialang.org/t/how-to-prevent-using-global-arrays-in-iteration-in-the-module/65857 "2021-08-05T04:53:43Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 5, 2021, 9:44pm UTC](https://discourse.julialang.org/t/how-to-prevent-using-global-arrays-in-iteration-in-the-module/65857/12 "2021-08-05T21:44:40Z")

</div>

> [@CRquantum](#):
>
> You example seems does not have a loop.

No.

The function `LifeGame.gif!(board, niters; ...)` in my example contains the for loop updating `niters` times the global variables initialized by `boardrandom = LifeGame.randboard()` and `board5x5 = LifeGame.fivexfive()` outside the module `LifeGame` and plotting each step to gif animations.

In the module LifeGame:

> [@genkuroki](#):
>
> ```julia
> function gif!(board::Board, niters, nskips=1; gifname="life.gif", fps=20, size=(240, 240))
> prog = Progress(niters, 0)
> anim = @animate for t in 1:niters
> heatmap(board.state; size, colorbar=false, ticks=false, axis=false, frame=false)
> update!(board, nskips)
> next!(prog)
> end
> gif(anim, gifname; fps)
> end
> 
> ```

Outside the module LifeGame:

> [@genkuroki](#):
>
> ```julia
> boardrandom = LifeGame.randboard()
> LifeGame.gif!(boardrandom, 500; gifname="liferandom.gif")
> 
> ```

> [@genkuroki](#):
>
> ```julia
> board5x5 = LifeGame.fivexfive()
> LifeGame.gif!(board5x5, 2000; gifname="life5x5.gif", fps=60)
> 
> ```

`LifeGame.update!(board, niters)` is not used in the example, but can update a `LifeGame.Board` type variable without plotting.

In the module LifeGame:

> [@genkuroki](#):
>
> ```julia
> function update!(board::Board, niters=1)
> state, tmp = board.state, board.tmp
> for _ in 1:niters÷2
> _update!(tmp, state)
> _update!(state, tmp)
> end
> if isodd(niters)
> _update!(tmp, state)
> state .= tmp
> end
> end
> 
> ```

The array `tmp`, which is used as a temporary workspace, is created by the constructor of `LifeGame.Board` objects.

> [@genkuroki](#):
>
> ```julia
> struct Board{T}
> state::T
> tmp::T
> end
> Board(state) = Board(state, similar(state))
> 
> ```

**Example of `LifeGame.update!`:** Glider

Updating a global variable with a for loop can be done by passing it as an argument to a function containing a for loop.

```julia
state = Int8[
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 1 1 0 0 0 0
    1 0 1 0 0 0 0
    0 0 1 0 0 0 0
]
board = LifeGame.Board(state)
board.state

```

```julia
7×7 Matrix{Int8}:
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 1 1 0 0 0 0
 1 0 1 0 0 0 0
 0 0 1 0 0 0 0

```

↓  
↓ update the global variable `board` 12 times  
↓ by the for loop in the function `LifeGame.update!`  
↓

```julia
LifeGame.update!(board, 12)
board.state

```

```julia
7×7 Matrix{Int8}:
 0 0 0 0 0 0 0
 0 0 0 0 1 1 0
 0 0 0 1 0 1 0
 0 0 0 0 0 1 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0
 0 0 0 0 0 0 0

```

↓  
↓ update and animate the global variable `board`  
↓

```julia
LifeGame.gif!(board, 27; gifname="lifeglider.gif", fps=5)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/a/c/ace63cbf80e94795a5fcd3ec23947a601578f895.gif)

Have fun!

Jupyter notebook: [https://github.com/genkuroki/public/blob/main/0014/Game%20of%20Life.ipynb](https://github.com/genkuroki/public/blob/main/0014/Game%20of%20Life.ipynb)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-prevent-using-global-arrays-in-iteration-in-the-module/65857)._
