Hello,
The first step is always to check the help
help?> map!
search: map! asyncmap! map_coefficients_inplace! map mapcols mapfoldr mapfoldl map_rows mapslices mapreduce map_coefficients
map!(function, destination, collection...)
Like map, but stores the result in destination rather than a new collection. destination must be at least as large as the
first collection.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> a = zeros(3);
julia> map!(x -> x * 2, a, [1, 2, 3]);
julia> a
3-element Array{Float64,1}:
2.0
4.0
6.0
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
map!(f, values(dict::AbstractDict))
Modifies dict by transforming each value from val to f(val). Note that the type of dict cannot be changed: if f(val) is not
an instance of the key type of dict then it will be converted to the key type if possible and otherwise raise an error.
Examples
≡≡≡≡≡≡≡≡≡≡
```jldoctest julia> d = Dict(:a => 1, :b => 2) Dict{Symbol,Int64} with 2 entries: :a => 1 :b => 2
julia> map!(v -> v-1, values(d)) Dict{Symbol,Int64} with 2 entries: :a => 0 :b => 1 ```
Based on the above, we need a destination for map! (the ! symbol stands for in-place operations so it doesn’t allocate a new array for storing the result. It just mutates the existing array.).
What you need then is
julia> listofnumbers = [23, 45, 56, 67, 89]
5-element Array{Int64,1}:
23
45
56
67
89
julia> map!(x -> x + 1, listofnumbers, listofnumbers)
5-element Array{Int64,1}:
24
46
57
68
90
You could also create a separate destination object, like so
julia> listofnumbers = [23, 45, 56, 67, 89]
5-element Array{Int64,1}:
23
45
56
67
89
julia> a = fill(0, 5)
5-element Array{Int64,1}:
0
0
0
0
0
julia> map!(x -> x + 1, a, listofnumbers)
5-element Array{Int64,1}:
24
46
57
68
90
julia> a
5-element Array{Int64,1}:
24
46
57
68
90
Note that a = fill(0, 5)
at the beginning and a = [24 46 57 68 90]
at the end. This is because you were mutating a
in-place.