Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Commit

Permalink
Added ability to convert from Arrays of Nullables
Browse files Browse the repository at this point in the history
  • Loading branch information
andyferris authored and nalimilan committed Apr 30, 2016
1 parent 5de3aff commit f74ae1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,30 @@ function NullableArray{T}(A::AbstractArray,
end

#----- Constructor #7 --------------------------------------------------------#

# The following method allows for the construction of zero-element
# NullableArrays by calling the parametrized type on zero arguments.
# TODO: add support for dimensions arguments?
@compat (::Type{NullableArray{T, N}}){T, N}() = NullableArray(T, ntuple(i->0, N))

#----- Conversion from Array of Nullables #1 -----------------------------------#
# Take an Array of Nullable objects and convert it into a NullableArray
function Base.convert{S, T, N}(::Type{NullableArray{S, N}}, in::AbstractArray{Nullable{T}, N})
out = NullableArray{S, N}(Array(S, size(in)), Array(Bool, size(in)))
for i = 1:length(in)
if !(out.isnull[i] = isnull(in[i]))
out.values[i] = in[i].value
end
end
out
end

#----- Conversion from Array of Nullables #2 -----------------------------------#
# Take an Array of Nullable objects and convert it into a NullableArray
# A slightly more convenient syntax to match the type of the input
Base.convert{S, T, N}(::Type{NullableArray{S}}, in::AbstractArray{Nullable{T}, N}) = Base.convert(NullableArray{S, N}, in)

#----- Constructor #8 -----------------------------------#
# Take an Array of Nullable objects and convert it into a NullableArray
# The constructor needs defining as it is a more specialized version of the
# above, and thus will not fall back to convert.
NullableArray{T, N}(in::Array{Nullable{T}, N}) = Base.convert(NullableArray{T, N}, in)
13 changes: 12 additions & 1 deletion test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,16 @@ module TestConstructors
@test isequal(NullableArray{Bool, 2}(),
NullableArray{Bool, 2}(Array(Bool, 0, 0)))


# test conversion from arrays of nullables
array_of_nulls = Nullable{Int}[Nullable(1), Nullable(2), Nullable(3), Nullable()]
@test isa(NullableArray(array_of_nulls), NullableArray{Int,1})
@test isequal(NullableArray(array_of_nulls), NullableArray{Int,1}([1,2,3,4],[false,false,false,true]))
@test isa(NullableArray{Int64}(array_of_nulls), NullableArray{Int,1})
@test isequal(NullableArray{Int64}(array_of_nulls), NullableArray{Int,1}([1,2,3,4],[false,false,false,true]))
@test isa(NullableArray{Float64}(array_of_nulls), NullableArray{Float64,1})
@test isequal(NullableArray{Float64}(array_of_nulls), NullableArray{Float64,1}([1.0,2.0,3.0,4.0],[false,false,false,true]))
@test isa(NullableArray{Int64,1}(array_of_nulls), NullableArray{Int,1})
@test isequal(NullableArray{Int64,1}(array_of_nulls), NullableArray{Int,1}([1,2,3,4],[false,false,false,true]))
@test isa(NullableArray{Float64,1}(array_of_nulls), NullableArray{Float64,1})
@test isequal(NullableArray{Float64,1}(array_of_nulls), NullableArray{Float64,1}([1.0,2.0,3.0,4.0],[false,false,false,true]))
end

0 comments on commit f74ae1f

Please sign in to comment.