Skip to content

Commit 48f6fcb

Browse files
authored
Fix inference of mapreduce (#103)
Fixes JuliaGraphics/Colors.jl#326
1 parent 3718725 commit 48f6fcb

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ColorTypes = "≥ 0.7.4"
1616
Colors = "≥ 0.7.1"
1717
FixedPointNumbers = "≥ 0.3.0"
1818
StatsBase = "≥ 0.8.2"
19-
julia = "0.7.0"
19+
julia = "0.7, 1"
2020

2121
[extras]
2222
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/ColorVectorSpace.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ _nan(::Type{T}, ::Type{C}) where {T<:AbstractFloat,C<:TransparentRGB} = (x = con
5151
## Generic algorithms
5252
mapreduce(f, op::Union{typeof(&), typeof(|)}, a::MathTypes) = f(a) # ambiguity
5353
mapreduce(f, op, a::MathTypes) = f(a)
54-
if isdefined(Base, :r_promote)
55-
Base.r_promote(::typeof(+), c::MathTypes) = mapc(x->Base.r_promote(+, x), c)
56-
elseif isdefined(Base, :promote_sys_size_add)
57-
Base.promote_sys_size_add(c::MathTypes) = mapc(Base.promote_sys_size_add, c)
58-
else
59-
Base.add_sum(c1::MathTypes,c2::MathTypes) = mapc(Base.add_sum, c1, c2)
54+
Base.add_sum(c1::MathTypes,c2::MathTypes) = mapc(Base.add_sum, c1, c2)
55+
Base.reduce_first(::typeof(Base.add_sum), c::MathTypes) = mapc(x->Base.reduce_first(Base.add_sum, x), c)
56+
function Base.reduce_empty(::typeof(Base.add_sum), ::Type{T}) where {T<:MathTypes}
57+
z = Base.reduce_empty(Base.add_sum, eltype(T))
58+
return zero(base_colorant_type(T){typeof(z)})
6059
end
6160

6261
for f in (:trunc, :floor, :round, :ceil, :eps, :bswap)

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ end
371371
@test size(A) == (1,3)
372372
end
373373
end
374+
375+
@testset "Colors issue #326" begin
376+
A = rand(RGB{N0f8}, 2, 2)
377+
@test @inferred mean(A) == mean(map(c->mapc(FixedPointNumbers.Treduce, c), A))
378+
end
374379
end
375380

376381
end

0 commit comments

Comments
 (0)