# Filter and filter! give different results

**URL:** https://discourse.julialang.org/t/filter-and-filter-give-different-results/65271
**Category:** General Usage
**Tags:** question
**Created:** [July 25, 2021, 4:40pm UTC](https://discourse.julialang.org/t/filter-and-filter-give-different-results/65271 "2021-07-25T16:40:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [July 25, 2021, 4:40pm UTC](https://discourse.julialang.org/t/filter-and-filter-give-different-results/65271/1 "2021-07-25T16:40:26Z")

</div>

I am wondering why filter and filter! give different results in the example below. Case 1 with `filter!` includes an extra element whereas Case 2 with `filter` works as expected

```julia
mutable struct M
    x::Int
    y::Int
end

```

**Case 1:**

```julia
m1 = [M(i,i+1) for i in 1:4]
filter!(x->x == m1[2], m1)

```

Results:

```julia
println(m1)
M[M(2, 3), M(3, 4)]

```

**Case 2:**

```julia
m2 = [M(i,i+1) for i in 1:4]
m3 = filter(x->x == m2[2], m2)

```

Results:

```julia
julia> println(m3)
M[M(2, 3)]

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [July 25, 2021, 4:52pm UTC](https://discourse.julialang.org/t/filter-and-filter-give-different-results/65271/2 "2021-07-25T16:52:33Z")

</div>

I would guess in Case 1 what `m1[2]` is changes as you mutate `m1` with `filter!`; in other words, each time you evaluate that anonymous function inside `filter!` you could be getting a different object because the filter is changing the array.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [July 25, 2021, 5:11pm UTC](https://discourse.julialang.org/t/filter-and-filter-give-different-results/65271/3 "2021-07-25T17:11:42Z")

</div>

Thanks. That was a silly mistake on my part. Using z = m1[2] fixes the problem.

I am trying to navigate several issues with tests of equality on mutable structs. Evidently, there is a [long standing issue](https://github.com/JuliaLang/julia/issues/4648). Is there a recommended solution for testing that the fields in two structs are equal? Should I iterate over the fields and use `getfield` to test the values?
