# Mutating with \`foldl\` undefined behaviour?

**URL:** https://discourse.julialang.org/t/mutating-with-foldl-undefined-behaviour/1659
**Category:** General Usage
**Tags:** question
**Created:** [January 24, 2017, 8:05am UTC](https://discourse.julialang.org/t/mutating-with-foldl-undefined-behaviour/1659 "2017-01-24T08:05:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [January 24, 2017, 8:05am UTC](https://discourse.julialang.org/t/mutating-with-foldl-undefined-behaviour/1659/1 "2017-01-24T08:05:05Z")

</div>

```julia
help?> foldl
search: foldl mapfoldl foldr mapfoldr

  foldl(op, v0, itr)

  Like reduce, but with guaranteed left associativity. v0 will be used exactly
  once.

```

Is it undefined behaviour for `op` to mutate one (or both) of its arguments in the method above?

---

<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 24, 2017, 11:04am UTC](https://discourse.julialang.org/t/mutating-with-foldl-undefined-behaviour/1659/2 "2017-01-24T11:04:22Z")

</div>

This is an interesting question — it is not documented explicitly, but given that the associativity is imposed, I would argue that

```julia
foldl(op, v0, itr)

```

should have a fixed evaluation order, and thus be equivalent in results and side effects to

```julia
let x = v0
    for elt in itr
        x = op(x, elt)
    end
    x
end

```

and deviations are bugs.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [January 25, 2017, 1:56am UTC](https://discourse.julialang.org/t/mutating-with-foldl-undefined-behaviour/1659/3 "2017-01-25T01:56:21Z")

</div>

I assume you referring to the `itr` argument? Yes, it’s undefined what happens to an iterator when you change its underlying container.

---

<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 25, 2017, 7:10am UTC](https://discourse.julialang.org/t/mutating-with-foldl-undefined-behaviour/1659/4 "2017-01-25T07:10:24Z")

</div>

Then I misunderstood the question, I was thinking about something like

```julia
julia> x = [[i] for i in 1:5]
5-element Array{Array{Int64,1},1}:
 [1]
 [2]
 [3]
 [4]
 [5]

julia> mutating_plus(acc, vec) = (acc=acc+vec[1]; vec[1] += 1; acc)

julia> foldl(mutating_plus, 0, x)
15

julia> x
5-element Array{Array{Int64,1},1}:
 [2]
 [3]
 [4]
 [5]
 [6]

```

ie `foldl` having a well-defined traversal order, which is the same as iteration.

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [January 26, 2017, 12:10am UTC](https://discourse.julialang.org/t/mutating-with-foldl-undefined-behaviour/1659/5 "2017-01-26T00:10:38Z")

</div>

This is what I meant. I encountered a strange issue recently through a definition of

```julia
append!(x, y) = foldl(push!, x, y)

```

for a custom data structure; the issue went away after I replaced the `foldl` with an explicit loop. I asked this question to help diagnose the problem.
