In-place map

In-place map! seems to work:

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.

1 Like

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

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.

2 Likes

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.

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...)?