Skip to content

Instantly share code, notes, and snippets.

@HarlanH
Created February 25, 2012 18:57
Show Gist options
  • Save HarlanH/1910073 to your computer and use it in GitHub Desktop.
Save HarlanH/1910073 to your computer and use it in GitHub Desktop.
playing with NA for Julia
type _NA
end
macro NA() # for easier typing!
_NA()
end
NumberData = Union(_NA, Number)
StringData = Union(_NA, String)
# is there a way to avoid having to write down rules like this for every
# basic data type?
promote_rule(::Type{_NA}, ::Type{Int64}) = NumberData
promote_rule(::Type{_NA}, ::Type{Float64}) = NumberData
promote_rule(::Type{_NA}, ::Type{ASCIIString}) = StringData
# and likewise?
promote_rule(::Type{NumberData}, ::Type{Int64}) = NumberData
promote_rule(::Type{NumberData}, ::Type{Float64}) = NumberData
promote_rule(::Type{StringData}, ::Type{ASCIIString}) = StringData
# This works, but will we need to do it for EVERY operator?
+(x::_NA, y::Number) = @NA
show(na::_NA) = print("NA")
is_na(x) = typeof(x) == _NA
# try it out:
qq = ["asdf", "qwerty", @NA, "biff"]
qq[!map(is_na, qq)]
map(uc, qq) # fails -- will we have to change EVERY core function?
zz = [1:3, @NA, 5:6]
zz + 1
sqrt(zz) # fails -- same issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment