From 56ca4d24036195455a2b44b8b04df53f509c14e9 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Tue, 27 Apr 2021 10:23:28 -0400 Subject: [PATCH 1/4] one-array in-place map --- base/abstractarray.jl | 22 ++++++++++++++++++++-- test/abstractarray.jl | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index a5782b99e581c..5a7d8e05b0dbc 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -2452,11 +2452,29 @@ julia> map!(+, zeros(Int, 5), 100:999, 1:3) ``` """ function map!(f::F, dest::AbstractArray, As::AbstractArray...) where {F} - isempty(As) && throw(ArgumentError( - """map! requires at least one "source" argument""")) + @assert !isempty(As) # should dispatch to map!(f, A) map_n!(f, dest, As) end +""" + map!(function, array) + +Like [`map`](@ref), but stores the result in the same array. + +# Examples +```jldoctest +julia> a = [1 2 3; 4 5 6]; + +julia> map!(x -> x^3, a); + +julia> a +2×3 Matrix{$Int}: + 1 8 27 + 64 125 216 +``` +""" +map!(f::F, inout::AbstractArray) where F = map!(f, inout, inout) + map(f) = f() map(f, iters...) = collect(Generator(f, iters...)) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index b75869f3ed9d6..a8748da11e759 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -776,7 +776,7 @@ test_ind2sub(TestAbstractArray) include("generic_map_tests.jl") generic_map_tests(map, map!) -@test_throws ArgumentError map!(-, [1]) +@test map!(-, [1]) == [-1] test_UInt_indexing(TestAbstractArray) test_13315(TestAbstractArray) From a803cda021d662df5b3121b495fe152a13e6c4ae Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Fri, 9 Feb 2024 16:52:09 +0000 Subject: [PATCH 2/4] Fix whitespace --- base/abstractarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 6596e3bd426c5..26fb5627437b5 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3396,7 +3396,7 @@ end """ map!(function, array) -Like [`map`](@ref), but stores the result in the same array. +Like [`map`](@ref), but stores the result in the same array. # Examples ```jldoctest From b7857b62c1012c29f213bde5aae684157e4b87b7 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Fri, 9 Feb 2024 13:34:29 -0500 Subject: [PATCH 3/4] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 6db22fa1f8be7..46734bd9b5cc3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -107,6 +107,7 @@ New library features automatically. * `@timed` now additionally returns the elapsed compilation and recompilation time ([#52889]) * `filter` can now act on a `NamedTuple` ([#50795]). +* `map!(f, A)` now stores the results in `A`, like `map!(f, A, A)`. or `A .= f.(A)` ([#40632]). Standard library changes ------------------------ From 5e9c04573867e004ca25d27a0d907358709452df Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Tue, 7 Jan 2025 07:27:16 -0600 Subject: [PATCH 4/4] Add compat annotation Co-authored-by: Michael Abbott <32575566+mcabbott@users.noreply.github.com> --- base/abstractarray.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index ba212a86e6762..a370b79976ab0 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3511,6 +3511,9 @@ end map!(function, array) Like [`map`](@ref), but stores the result in the same array. +!!! compat "Julia 1.12" + This method requires Julia 1.12 or later. To support previous versions too, + use the equivalent `map!(function, array, array)`. # Examples ```jldoctest