# In-place map

**URL:** <https://discourse.julialang.org/t/in-place-map/47464>\
**Category:** General Usage\
**Created:** [September 29, 2020, 4:02am UTC](https://discourse.julialang.org/t/in-place-map/47464 "2020-09-29T04:02:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [September 29, 2020, 4:02am UTC](https://discourse.julialang.org/t/in-place-map/47464/1 "2020-09-29T04:02:24Z")

</div>

In-place `map!` seems to work:

```julia
a = [collect(1:3)..., -Inf, Inf, NaN]
map!(x -> 0 <= x <= 10 ? x : 0.0, a, a)
.....
julia> a
6-element Array{Float64,1}:
 1.0
 2.0
 3.0
 0.0
 0.0
 0.0

```

Is it allowed?

Assuming that it is, I made a pull request: [https://github.com/JuliaLang/julia/pull/37792](https://github.com/JuliaLang/julia/pull/37792).

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [September 29, 2020, 6:29am UTC](https://discourse.julialang.org/t/in-place-map/47464/2 "2020-09-29T06:29:13Z")

</div>

I would have been surprised if it _didn’t_ work. `map!(f, dest, src)` is just:

```julia
for i in eachindex(src)
    dest[i] = f(src[i])
end

```

AFAIK. An example is always nice, but IMHO there’s a missing method `map!(f, iter) = map!(f, iter, iter)` which would make it obvious that it does work.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [September 29, 2020, 7:05am UTC](https://discourse.julialang.org/t/in-place-map/47464/3 "2020-09-29T07:05:49Z")

</div>

> [@cstjean](#):
>
> there’s a missing method `map!(f, iter) = map!(f, iter, iter)`

Agree. I was specifically looking for it in the documentation. In fact, I wrote a piece of code with it and I was surprised that it didn’t work.

---

<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:** [September 29, 2020, 12:50pm UTC](https://discourse.julialang.org/t/in-place-map/47464/4 "2020-09-29T12:50:40Z")

</div>

> [@cstjean](#):
>
> a missing method `map!(f, iter)`

That’s a great idea, but given that the semantics is different from the current `map!`, perhaps a different name would be preferable. Also, there is no reason it can’t take multiple arguments, and just write into the first one.

So maybe `mapinto!(f, dst_and_first_arg, args...)`?
