You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The REPL documentation shows the following for the function map!
help?> map!
search: map! asyncmap! mapcols! map mapcols mapfoldr mapfoldl mapslices mapreduce
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.04.06.0
To my understanding the appended ! implies that the function modifies the input variable, but in this case it is stored in a separate destination argument while the collection remains unchanged. For now a simple map!(function, collection) is not supported. I can imagine in some cases the output will have a different size/structure to the input collection and then the map!(function, destination, collection) form is convenient.
Maybe the following example can highlight the current inconvenience.
julia> a = [1, 2, 3]
3-element Array{Int64,1}:123
julia>map!(x -> x *2, a)
ERROR: BoundsError: attempt to access ()
at index [1]
Stacktrace:
[1] getindex(::Tuple, ::Int64) at .\tuple.jl:24
[2] map_n!(::var"#41#42", ::Array{Int64,1}, ::Tuple{}) at .\abstractarray.jl:2122
[3] map!(::var"#41#42", ::Array{Int64,1}) at .\abstractarray.jl:2151
[4] top-level scope at REPL[46]:1
julia>map!(x -> x *2, a, a)
3-element Array{Int64,1}:246
The text was updated successfully, but these errors were encountered:
help?> broadcast!
search: broadcast! broadcast Broadcast
broadcast!(f, dest, As...)
Like broadcast, but store the result of broadcast(f, As...) in the dest array.
Note that dest is only used to store the result, and does not supply arguments
to f unless it is also listed in the As, as inbroadcast!(f, A, A, B) to perform
A[:] =broadcast(f, A, B).
Examples
≡≡≡≡≡≡≡≡≡≡
julia> A = [1.0; 0.0]; B = [0.0; 0.0];
julia>broadcast!(+, B, A, (0, -2.0));
julia> B
2-element Array{Float64,1}:1.0-2.0
julia> A
2-element Array{Float64,1}:1.00.0
julia>broadcast!(+, A, A, (0, -2.0));
julia> A
2-element Array{Float64,1}:1.0-2.0
The REPL documentation shows the following for the function
map!
To my understanding the appended
!
implies that the function modifies the input variable, but in this case it is stored in a separatedestination
argument while thecollection
remains unchanged. For now a simplemap!(function, collection)
is not supported. I can imagine in some cases the output will have a different size/structure to the inputcollection
and then themap!(function, destination, collection)
form is convenient.Maybe the following example can highlight the current inconvenience.
The text was updated successfully, but these errors were encountered: