Created
June 30, 2015 15:51
-
-
Save johnmyleswhite/8ace4451357a9764ecfa to your computer and use it in GitHub Desktop.
lift_v2.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implement the unary operators: +, -, !, and ~ | |
using NullableArrays | |
@noinline throw_error() = error() | |
for f in ( | |
:(Base.(:+)), | |
:(Base.(:-)), | |
:(Base.(:!)), | |
:(Base.(:~)), | |
) | |
@eval begin | |
@inline function $(f){S}(x::Nullable{S}) | |
if isbits(S) | |
Nullable($(f)(x.value), x.isnull) | |
else | |
throw_error() | |
end | |
end | |
end | |
end | |
# Implement the binary operators: +, -, *, /, %, &, |, ^, <<, and >> | |
for f in ( | |
:(Base.(:+)), | |
:(Base.(:-)), | |
:(Base.(:*)), | |
:(Base.(:/)), | |
:(Base.(:%)), | |
:(Base.(:&)), | |
:(Base.(:|)), | |
:(Base.(:^)), | |
:(Base.(:<<)), | |
:(Base.(:>>)), | |
) | |
@eval begin | |
@inline function $(f){S1, S2}(x::Nullable{S1}, y::Nullable{S2}) | |
if isbits(S1) & isbits(S2) | |
Nullable($(f)(x.value, y.value), x.isnull | y.isnull) | |
else | |
throw_error() | |
end | |
end | |
end | |
end | |
# Split out & and | | |
function foo() | |
x = randn(10_000_000) | |
y = NullableArray(x) | |
z = Nullable{Float64}[Nullable(x_i) for x_i in x] | |
@time sum(x) | |
@time sum(y) | |
@time sum(z) | |
end | |
foo() | |
# Implement the binary operators: == and != | |
for f in ( | |
:(Base.(:==)), | |
:(Base.(:!=)), | |
) | |
@eval begin | |
function $(f){S1, S2}(x::Nullable{S1}, y::Nullable{S2}) | |
if isbits(S1) & isbits(S2) | |
Nullable{Bool}($(f)(x.value, y.value), x.isnull | y.isnull) | |
else | |
error() | |
end | |
end | |
end | |
end | |
# Implement the binary operators: <, >, <=, and >= | |
for f in ( | |
:(Base.(:<)), | |
:(Base.(:>)), | |
:(Base.(:<=)), | |
:(Base.(:>=)), | |
) | |
@eval begin | |
function $(f){S1, S2}(x::Nullable{S1}, y::Nullable{S2}) | |
if isbits(S1) & isbits(S2) | |
Nullable{Bool}($(f)(x.value, y.value), x.isnull | y.isnull) | |
else | |
error() | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment