# Unexpected 'map' behaviour

**URL:** <https://discourse.julialang.org/t/unexpected-map-behaviour/9354>\
**Category:** New to Julia\
**Created:** [February 27, 2018, 12:25am UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/9354 "2018-02-27T00:25:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rje](https://avatars.discourse-cdn.com/v4/letter/r/ee7513/32.png) [@rje](https://discourse.julialang.org/u/rje)\
**Post date:** [February 27, 2018, 12:25am UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/9354/1 "2018-02-27T00:25:24Z")

</div>

I was playing around with `map` and encountered some unexpected behaviour when changing the definition of the argument function.

Define a function

```julia
julia> f(x) = x + 2
f (generic function with 1 method)

```

Apply `map` to the function and a collection.

```julia
julia> map(f,1:3)
3-element Array{Int64,1}:
 3
 4
 5

```

Redefine `f`

```julia
f(x) = x + 3
WARNING: Method definition f(Any) in module Main at REPL[1]:1 overwritten at REPL[3]:1.
f (generic function with 1 method)

```

Apply `map` to redefined `f` and the same collection.

```julia
julia> map(f,1:3)
3-element Array{Int64,1}:
 3
 4
 5

```

Hmmm. Looks like `f` never changed. `Collect` the collection.

```julia
map(f,collect(1:3))
3-element Array{Int64,1}:
 4
 5
 6

```

Oh, now it changed. Change `f` one more time.

```julia
f(x) = x + 4
WARNING: Method definition f(Any) in module Main at REPL[3]:1 overwritten at REPL[13]:1.
f (generic function with 1 method)

```

Apply `map` to `f` and the collected collection.

```julia
map(f,collect(1:3))
3-element Array{Int64,1}:
 4
 5
 6

```

It’s still using the previous definition of `f`. Well then.

I expected `map` to use the current definition of `f`, so if I change `f` and call `map` again same arguments as before, `map` would use the new definition of `f`, much like changing the second argument to `map` changes the collection that `f` gets applied to. But instead, it looks like `map` is using the definition of `f` from when `map` was first called.

This is not the kind of behaviour I would infer from the documentation which states simply that `map(f,c...)` “transform[s] collection c by applying f to each element.” I spent a while reading other documentation on functions to find the answer, but didn’t see anything describing or explaining this kind of behaviour.

I can get around this by writing `map(x->f(x),r)`, but that seems rather roundabout.

Is this behaviour on purpose? Is there a better way to work around it?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 27, 2018, 12:32am UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/9354/2 "2018-02-27T00:32:01Z")

</div>

It looks like you’re still on Julia 0.5 or older. Update to Julia 0.6 to fix it.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 27, 2018, 1:40am UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/9354/3 "2018-02-27T01:40:39Z")

</div>

In case you’re curious, you’ve run into the dreaded issue 265: [https://github.com/julialang/julia/issues/265](https://github.com/julialang/julia/issues/265) which was finally resolved in Julia 0.6, as mbauman mentioned.

---

<div class="post-metadata">

**Author:** ![rje](https://avatars.discourse-cdn.com/v4/letter/r/ee7513/32.png) [@rje](https://discourse.julialang.org/u/rje)\
**Post date:** [February 27, 2018, 2:55am UTC](https://discourse.julialang.org/t/unexpected-map-behaviour/9354/4 "2018-02-27T02:55:56Z")

</div>

Ah, there we go! Thanks!
