Skip to content

Commit 0a86bbb

Browse files
committed
Rely less on inventing values to determine eltype. Fixes #7.
1 parent 6ecb545 commit 0a86bbb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/MappedArrays.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
creates a view of the array `A` that applies `f` to every element of
2626
`A`. The view is read-only (you can get values but not set them).
2727
"""
28-
mappedarray{T,N}(f, data::AbstractArray{T,N}) = ReadonlyMappedArray{typeof(f(one(T))),N,typeof(data),typeof(f)}(f, data)
28+
mappedarray{T,N}(f, data::AbstractArray{T,N}) = ReadonlyMappedArray{typeof(f(testvalue(data))),N,typeof(data),typeof(f)}(f, data)
2929

3030
"""
3131
mappedarray((f, finv), A)
@@ -36,7 +36,7 @@ the view and, correspondingly, the values in `A`.
3636
"""
3737
function mappedarray{T,N}(f_finv::Tuple{Any,Any}, data::AbstractArray{T,N})
3838
f, finv = f_finv
39-
MappedArray{typeof(f(one(T))),N,typeof(data),typeof(f),typeof(finv)}(f, finv, data)
39+
MappedArray{typeof(f(testvalue(data))),N,typeof(data),typeof(f),typeof(finv)}(f, finv, data)
4040
end
4141

4242
"""
@@ -60,4 +60,11 @@ parenttype{T,N,A,F,Finv}(::Type{MappedArray{T,N,A,F,Finv}}) = A
6060
@propagate_inbounds Base.setindex!{T}(A::MappedArray{T}, val::T, i::Int...) = A.data[i...] = A.finv(val)
6161
@inline Base.setindex!{T}(A::MappedArray{T}, val, i::Int...) = setindex!(A, convert(T, val), i...)
6262

63+
function testvalue(data)
64+
if !isempty(data)
65+
return first(data)
66+
end
67+
zero(eltype(data))
68+
end
69+
6370
end # module

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ aabs = mappedarray(abs, a)
6565
for i = -2:2
6666
@test aabs[i] == abs(a[i])
6767
end
68+
69+
# issue #7
70+
astr = mappedarray(length, ["abc", "onetwothree"])
71+
@test eltype(astr) == Int
72+
@test astr == [3, 11]
73+
a = mappedarray(x->x+0.5, Int[])
74+
@test eltype(a) == Float64

0 commit comments

Comments
 (0)