|
| 1 | +module FunctionalNullableOperations |
| 2 | + |
| 3 | +# extends Base with operations that treat nullables as collections |
| 4 | + |
| 5 | +importall Base |
| 6 | +import Base: promote_op, LinearFast |
| 7 | + |
| 8 | +# conceptually, nullable types can be considered infinite-order tensor powers of |
| 9 | +# finite-dimensional vector spaces — we attempt to support most operations on |
| 10 | +# arrays except the linear algebra related ones; the infinite-dimensional nature |
| 11 | +# makes subtyping AbstractArray a little dangerous, which explains why |
| 12 | +# functionality is reimplemented instead of subtyping AbstractArray |
| 13 | + |
| 14 | +# size – not implemented since an infinite-dimensional tuple would be strange |
| 15 | +# length – one or zero |
| 16 | +# endof – same as length |
| 17 | +length(u::Nullable) = u.isnull ? 0 : 1 |
| 18 | +endof(u::Nullable) = length(u) |
| 19 | + |
| 20 | +# indexing is either without index, or with 1 as index |
| 21 | +# generalized linear indexing is not supported |
| 22 | +# setindex! not supported because Nullable is immutable |
| 23 | +linearindexing{T}(::Nullable{T}) = LinearFast() |
| 24 | +function getindex(u::Nullable) |
| 25 | + @boundscheck u.isnull && throw(NullException()) |
| 26 | + u.value |
| 27 | +end |
| 28 | +function getindex(u::Nullable, i::Integer) |
| 29 | + @boundscheck u.isnull | (i ≠ one(i)) && throw(BoundsError(i, u)) |
| 30 | + u.value |
| 31 | +end |
| 32 | + |
| 33 | +# iteration protocol |
| 34 | +start(u::Nullable) = 1 |
| 35 | +next(u::Nullable, i::Integer) = u.value, 0 |
| 36 | +done(u::Nullable, i::Integer) = u.isnull || i == 0 |
| 37 | + |
| 38 | +# next we have reimplementations of some higher-order functions |
| 39 | +filter{T}(p, u::Nullable{T}) = u.isnull ? u : p(u.value) ? u : Nullable{T}() |
| 40 | + |
| 41 | +# warning: not type-stable |
| 42 | +map{T}(f, u::Nullable{T}) = u.isnull ? Nullable{Union{}}() : Nullable(f(u.value)) |
| 43 | + |
| 44 | +# multi-argument map doesn't broadcast, so not very useful, but no harm having |
| 45 | +# it... |
| 46 | +function map(f, us::Nullable...) |
| 47 | + if all(isnull, us) |
| 48 | + Nullable() |
| 49 | + elseif !any(isnull, us) |
| 50 | + Nullable(map(f, map(getindex, us)...)) |
| 51 | + else |
| 52 | + throw(DimensionMismatch("expected all null or all nonnull")) |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | +# foldr and foldl are quite useful to express "do something if not null, else" |
| 57 | +# these |
| 58 | + |
| 59 | +# being infinite-dimensional, nullables are generally incompatible with |
| 60 | +# broadcast with arrays — it is probably not worth supporting the rare case |
| 61 | +# where an array has length 0 or 1 |
| 62 | + |
| 63 | +# so we reimplement broadcast, which has the same semantics but very different |
| 64 | +# implementation. This implementation is in fact much simpler than that for |
| 65 | +# arrays. Length-1 (non-nulls) are flexible and can broadcast to length-0 |
| 66 | +# (nulls), but the other way does not work. Numbers are zero-dimensional and can |
| 67 | +# broadcast to infinite-dimensional nullables, but the other direction is not |
| 68 | +# supported. |
| 69 | + |
| 70 | +# there are two shapes we are concerned about: infinite-dimensional 1×1×... |
| 71 | +# and infinite-dimensional 0×0×...; we don't care about zero-dimensional because |
| 72 | +# in that case all arguments were numbers, and broadcasting over only numbers |
| 73 | +# isn't supported by base currently |
| 74 | +function nullable_broadcast_shape(us::Union{Nullable,Number}...) |
| 75 | + for u in us |
| 76 | + if isa(us, Nullable) |
| 77 | + if u.isnull |
| 78 | + return true |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + return false |
| 83 | +end |
| 84 | + |
| 85 | +# Base's broadcast has a very loose signature so we can easily make it more |
| 86 | +# specific. Broadcast on numbers is still not supported. FIXME: remove generated |
| 87 | +# functions where unnecessary |
| 88 | + |
| 89 | +# some specialized functions |
| 90 | +broadcast{T}(f, u::Nullable{T}) = |
| 91 | + u.isnull ? Nullable{promote_op(f, T)}() : Nullable{promote_op(f, T)}(f(u.value)) |
| 92 | + |
| 93 | +@generated function broadcast(f, u::Union{Nullable,Number}, v::Union{Nullable,Number}) |
| 94 | + checkfor(s) = :($s.isnull && return Nullable{result}()) |
| 95 | + lifted = [T <: Nullable ? T.parameters[1] : T for T in (u, v)] |
| 96 | + checks = vcat(u <: Nullable ? [checkfor(:u)] : [], |
| 97 | + v <: Nullable ? [checkfor(:v)] : []) |
| 98 | + quote |
| 99 | + result = promote_op(f, $(lifted...)) |
| 100 | + $(checks...) |
| 101 | + @inbounds return Nullable{result}(f(u[], v[])) |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +# functions with three arguments or more are a bit expensive to specialize... |
| 106 | +# FIXME: why the arbitrary cutoff? justify |
| 107 | +function broadcast(f, us::Union{Nullable, Number}...) |
| 108 | + result = promote_op(f, |
| 109 | + [T <: Nullable ? T.parameters[1] : T for T in map(typeof, us)]...) |
| 110 | + for u in us |
| 111 | + if isa(u, Nullable) && u.isnull |
| 112 | + return Nullable{result}() |
| 113 | + end |
| 114 | + end |
| 115 | + @inbounds return Nullable{result}(f(map(getindex, us)...)) |
| 116 | +end |
| 117 | + |
| 118 | +# FIXME: these operations are probably not all correct |
| 119 | +# and definitely some of them are slow, needs specialization |
| 120 | +# also have to be careful to avoid ambiguities... needs testing |
| 121 | +for eop in :(.+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, .÷, .%, .<<, .>>, .^).args |
| 122 | + @eval $eop(u::Nullable, v::Union{Nullable, Number}) = broadcast($eop, u, v) |
| 123 | + @eval $eop(u::Number, v::Nullable) = broadcast($eop, u, v) |
| 124 | +end |
| 125 | + |
| 126 | +end # module |
0 commit comments