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

Define vectorized get? #87

Open
quinnj opened this issue Oct 15, 2015 · 5 comments
Open

Define vectorized get? #87

quinnj opened this issue Oct 15, 2015 · 5 comments

Comments

@quinnj
Copy link
Member

quinnj commented Oct 15, 2015

I think it'd be really useful to have a NullableArray equivalent of get(A, default).

Something along the lines of:

function Base.get{T}(A::NullableArrays.NullableArray{T}, y)
    v = Array(T, size(A))
    for i in eachindex(v)
        if isbits(T)
            v[i] = ifelse(A.isnull[i], convert(T, y), A.values[i])
        else
            v[i] = A.isnull[i] ? convert(T, y) : A.values[i]
        end
    end
    return v
end
@johnmyleswhite
Copy link
Member

Seems like that's equivalent to convert(Array, NullableArray, default)

Is the goal less verbose sugar?

@quinnj
Copy link
Member Author

quinnj commented Oct 15, 2015

aha, didn't know about that convert method. Yeah, it might be nice to just have get call the convert method. In my mind, I tend to project how I work with Nullables onto NullableArrays, so when I needed all the "values" of a NullableArray, I immediately thought of the get idiom. I think it would pay off to provide this kind of consistency.

@nalimilan
Copy link
Member

Maybe get should replace convert(Array, NullableArray, default)? Looking at the convert methods, I can't find any which accepts three arguments.

@mbauman
Copy link
Contributor

mbauman commented Oct 15, 2015

julia> methods(convert, Tuple{Any,Any,Any,Vararg{Any}})
7-element Array{Any,1}:
 convert(::Type{ASCIIString}, a::Array{UInt8,1}, invalids_as::ASCIIString) at ascii.jl:115
 convert(::Type{ASCIIString}, a::Array{UInt8,1}, invalids_as::AbstractString) at ascii.jl:132
 convert(::Type{UTF8String}, a::Array{UInt8,1}, invalids_as::AbstractString) at unicode/utf8.jl:293
 convert{Tv<:Union{Complex{Float64},Float64}}(::Type{Base.SparseArrays.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}}, m::Integer, n::Integer, colptr::Array{Int64,1}, rowval::Array{Int64,1}, nzval::Array{Tv<:Union{Complex{Float64},Float64},1}, stype) at sparse/cholmod.jl:811
 convert{Tv<:Union{Complex{Float64},Float64}}(::Type{Base.SparseArrays.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}}, m::Integer, n::Integer, colptr::Array{Int64,1}, rowval::Array{Int64,1}, nzval::Array{Tv<:Union{Complex{Float64},Float64},1}) at sparse/cholmod.jl:832
 convert{Tv<:Union{Complex{Float64},Float64}}(::Type{Base.SparseArrays.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}}, A::SparseMatrixCSC{Tv<:Union{Complex{Float64},Float64},Int64}, stype::Integer) at sparse/cholmod.jl:842
 convert{T}(::Type{Base.SparseArrays.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}}, A::Union{Hermitian{T,SparseMatrixCSC{T,Int64}},SparseMatrixCSC{T,Int64},Symmetric{T,SparseMatrixCSC{T,Int64}}}, args...) at sparse/cholmod.jl:875

So there is precedence with the string methods. But I agree, I favor using the same vocabulary as the scalar case.

@johnmyleswhite
Copy link
Member

It's a little more complicated than that. There are three things you need to be able to do: fail on NULL, replace NULL and remove NULL. So you're not really using the same vocabulary as the scalar case, except for this specific variant. In particular, the best idiom anyone's proposed so far for the remove NULL variant is map, which we can't use for the array case.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants