Skip to content

Commit 9fbda81

Browse files
cscherrerMasonProtterjohnnychen94
authored
Use type inference instead of test_value (cont. #34) (#43)
Co-authored-by: MasonProtter <[email protected]> Co-authored-by: Johnny Chen <[email protected]>
1 parent ca07b29 commit 9fbda81

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "MappedArrays"
22
uuid = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900"
3-
version = "0.3.0"
3+
version = "0.4.0"
44

55
[compat]
66
FixedPointNumbers = "0.6.1, 0.7, 0.8"

src/MappedArrays.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ not set them).
5858
When multiple input arrays are supplied, `M[i] = f(A[i], B[i], C[i]...)`.
5959
"""
6060
function mappedarray(f, data::AbstractArray)
61-
T = typeof(f(testvalue(data)))
61+
T = Base._return_type(f, eltypes(data))
6262
ReadonlyMappedArray{T,ndims(data),typeof(data),typeof(f)}(f, data)
6363
end
6464

@@ -67,7 +67,7 @@ function mappedarray(::Type{T}, data::AbstractArray) where T
6767
end
6868

6969
function mappedarray(f, data::AbstractArray...)
70-
T = typeof(f(map(testvalue, data)...))
70+
T = Base._return_type(f, eltypes(data))
7171
ReadonlyMultiMappedArray{T,ndims(first(data)),typeof(data),typeof(f)}(f, data)
7272
end
7373

@@ -86,20 +86,20 @@ the view and, correspondingly, the values in `A`.
8686
When multiple input arrays are supplied, `M[i] = f(A[i], B[i], C[i]...)`.
8787
"""
8888
function mappedarray(f, finv, data::AbstractArray)
89-
T = typeof(f(testvalue(data)))
89+
T = Base._return_type(f, eltypes(data))
9090
MappedArray{T,ndims(data),typeof(data),typeof(f),typeof(finv)}(f, finv, data)
9191
end
9292

9393
function mappedarray(f, finv, data::AbstractArray...)
94-
T = typeof(f(map(testvalue, data)...))
94+
T = Base._return_type(f, eltypes(data))
9595
MultiMappedArray{T,ndims(first(data)),typeof(data),typeof(f),typeof(finv)}(f, finv, data)
9696
end
9797

9898
function mappedarray(::Type{T}, finv, data::AbstractArray...) where T
9999
MultiMappedArray{T,ndims(first(data)),typeof(data),Type{T},typeof(finv)}(T, finv, data)
100100
end
101101
function mappedarray(f, ::Type{Finv}, data::AbstractArray...) where Finv
102-
T = typeof(f(map(testvalue, data)...))
102+
T = Base._return_type(f, eltypes(data))
103103
MultiMappedArray{T,ndims(first(data)),typeof(data),typeof(f),Type{Finv}}(f, Finv, data)
104104
end
105105

test/runtests.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,40 @@ end
177177
str = String(take!(io))
178178
@test occursin("x1 + x2", str)
179179
end
180+
181+
@testset "eltype (issue #32)" begin
182+
# Tests fix for
183+
# https://github.com/JuliaArrays/MappedArrays.jl/issues/32#issuecomment-682985419
184+
T = Union{Missing, Float32}
185+
@test eltype(of_eltype(T, [missing, 3])) == T
186+
@test eltype(of_eltype(T, [3, missing])) == T
187+
@test eltype(of_eltype(Union{Missing, Float64}, [1, 2])) == Float64
188+
189+
@test eltype(mappedarray(identity, [1, missing])) == Union{Missing, Int}
190+
@test eltype(mappedarray(identity, [missing, 1])) == Union{Missing, Int}
191+
192+
# ReadonlyMappedArray and MappedArray
193+
_zero(x) = x > 0 ? x : 0
194+
@test eltype(mappedarray(_zero, [1, 1.0])) == Union{Float64,Int}
195+
@test eltype(mappedarray(_zero, [1.0, 1])) == Union{Float64,Int}
196+
@test eltype(mappedarray(_zero, [1, 1])) == Int
197+
198+
@test eltype(mappedarray(_zero, identity, [1, 1.0])) == Union{Float64,Int}
199+
@test eltype(mappedarray(_zero, identity, [1.0, 1])) == Union{Float64,Int}
200+
@test eltype(mappedarray(_zero, identity, [1, 1])) == Int
201+
202+
# MultiMappedArray and ReadonlyMultiMappedArray
203+
_sum(x, y) = _zero(x) + _zero(y)
204+
inferred_type = VERSION >= v"1.6.0-RC1" ? Union{Missing, Float64, Int64} : Any
205+
@test eltype(mappedarray(_sum, [1, 1.0], [1.0, missing])) == inferred_type
206+
@test eltype(mappedarray(_sum, [1, 1], [2, 2])) == Int
207+
@test eltype(mappedarray(_sum, identity, [1, 1.0], [1.0, missing])) == inferred_type
208+
@test eltype(mappedarray(_sum, identity, [1, 1], [2, 2])) == Int
209+
210+
_maybe_int(x) = x > 0 ? x : Int(x)
211+
@test eltype(mappedarray(_maybe_int, Float64, [1.0, 1, -1, -1.0])) == Union{Float64, Int64}
212+
@test eltype(mappedarray(_maybe_int, Float64, [1.0, -1.0])) == Union{Float64, Int64}
213+
@test eltype(mappedarray(_maybe_int, Float64, [1, -1])) == Int64
214+
@test eltype(mappedarray(Float64, _maybe_int, [1.0, 1, -1, -1.0])) == Float64
215+
@test eltype(mappedarray(Float64, _maybe_int, [1, -1])) == Float64
216+
end

0 commit comments

Comments
 (0)