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

Added ability to convert from Arrays of Nullables #99

Merged
merged 2 commits into from
Apr 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -----------------------------------#
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate of the method above?

# 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]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use === rather than isequal: that way, you'll check the actual type, not the isequal method. That will allow you to remove the lines using isa.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tip, but is/===/ for mutables checks if the memory addresses are the same, which isn't true here (my constructors copy the data so they won't end up with the same references).

For example:

julia> NullableArray([1,2,3]) === NullableArray([1,2,3])
false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Yet you can use == instead of isequal since there are no NaNs.

@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