Skip to content

Commit 563adeb

Browse files
authored
Specialize map!(f, array) (#1307)
Julia v1.12 introduces the 2-term `map!`, where the source and the destination are identical. Currently, this package errors without a third term, as it can't find the source array. This PR ensures that the method works for `StaticArrays`s. After this, ```julia julia> M = MVector(1,3,4) 3-element MVector{3, Int64} with indices SOneTo(3): 1 3 4 julia> map!(x->x^2, M) 3-element MVector{3, Int64} with indices SOneTo(3): 1 9 16 julia> M 3-element MVector{3, Int64} with indices SOneTo(3): 1 9 16 ```
1 parent 15a2672 commit 563adeb

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/mapreduce.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ enumerate_static(a::StaticArray) = StaticEnumerate(a)
110110
end
111111
end
112112

113+
if VERSION >= v"1.12.0-beta3"
114+
@inline function map!(f, dest::StaticArray)
115+
_map!(f, dest, Size(dest), dest)
116+
end
117+
end
118+
113119
@inline function map!(f, dest::StaticArray, a::StaticArray...)
114120
_map!(f, dest, same_size(dest, a...), a...)
115121
end

test/mapreduce.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ using Statistics: mean
3131
map!(+, mv3, v1, v2, v3)
3232
@test mv3 == @MVector [7, 9, 11, 13]
3333

34+
if VERSION >= v"1.12.0-beta3"
35+
@testset "map!(function, array)" begin
36+
local mv = MVector(1,2,3)
37+
map!(x->x^2, mv)
38+
@test mv == SA[1,4,9]
39+
end
40+
end
41+
3442
# Output eltype for empty cases #528
3543
@test @inferred(map(/, SVector{0,Int}(), SVector{0,Int}())) === SVector{0,Float64}()
3644
@test @inferred(map(+, SVector{0,Int}(), SVector{0,Float32}())) === SVector{0,Float32}()

0 commit comments

Comments
 (0)