Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map! to store result directly in collection instead of destination #36467

Closed
martincornejo opened this issue Jun 28, 2020 · 2 comments
Closed

Comments

@martincornejo
Copy link
Contributor

martincornejo commented Jun 28, 2020

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.0
   4.0
   6.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}:
 1
 2
 3

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}:
 2
 4
 6
@martincornejo
Copy link
Contributor Author

The same might apply to broadcast!

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 in broadcast!(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.0
   0.0
  
  julia> broadcast!(+, A, A, (0, -2.0));
  
  julia> A
  2-element Array{Float64,1}:
    1.0
   -2.0

@tkf
Copy link
Member

tkf commented Jun 29, 2020

It seems like this is a dup of #31677.

@tkf tkf closed this as completed Jun 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants