Skip to content

Commit 4225f1b

Browse files
authored
map: build output type based on first static argument. (#996)
1 parent 1858207 commit 4225f1b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/mapreduce.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ end
7373
@_inline_meta
7474
S = same_size(a...)
7575
@inbounds elements = tuple($(exprs...))
76-
@inbounds return similar_type(typeof(_first(a...)), eltype(elements), S)(elements)
76+
@inbounds return similar_type(typeof(a[$first_staticarray]), eltype(elements), S)(elements)
7777
end
7878
end
7979

test/mapreduce.jl

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ using Statistics: mean
55
@testset "map and map!" begin
66
v1 = @SVector [2,4,6,8]
77
v2 = @SVector [4,3,2,1]
8+
mv1 = @MVector [2,4,6,8]
9+
mv2 = @MVector [4,3,2,1]
810
mv = MVector{4, Int}(undef)
911

1012
normal_v1 = [2,4,6,8]
1113
normal_v2 = [4,3,2,1]
1214

1315
@test @inferred(map(-, v1)) === @SVector [-2, -4, -6, -8]
1416
@test @inferred(map(+, v1, v2)) === @SVector [6, 7, 8, 9]
15-
# @test @inferred(map(+, normal_v1, v2)) === @SVector [6, 7, 8, 9] # Maybe could fix this up
17+
@test @inferred(map(+, normal_v1, v2)) === @SVector [6, 7, 8, 9]
1618
@test @inferred(map(+, v1, normal_v2)) === @SVector [6, 7, 8, 9]
1719

20+
# Make sure similar_type is based on first <: StaticArray
21+
@test @inferred(map(+, normal_v1, mv2))::MVector{4,Int} == @MVector [6, 7, 8, 9]
22+
@test @inferred(map(+, mv1, normal_v2))::MVector{4,Int} == @MVector [6, 7, 8, 9]
23+
1824
map!(+, mv, v1, v2)
1925
@test mv == @MVector [6, 7, 8, 9]
2026
mv2 = MVector{4, Int}(undef)
@@ -29,6 +35,9 @@ using Statistics: mean
2935
@test @inferred(map(/, SVector{0,Int}(), SVector{0,Int}())) === SVector{0,Float64}()
3036
@test @inferred(map(+, SVector{0,Int}(), SVector{0,Float32}())) === SVector{0,Float32}()
3137
@test @inferred(map(length, SVector{0,String}())) === SVector{0,Int}()
38+
# similar_type based on first <: StaticArray
39+
@test @inferred(map(+, MVector{0,Int}(), Int[]))::MVector{0,Int} == MVector{0,Int}()
40+
@test @inferred(map(+, Int[], MVector{0,Int}()))::MVector{0,Int} == MVector{0,Int}()
3241
end
3342

3443
@testset "[map]reduce and [map]reducedim" begin

0 commit comments

Comments
 (0)