Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff committed Nov 19, 2018
2 parents ed3a7a0 + c217aa5 commit a312c00
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ julia 0.7
DataStructures 0.11.0
IteratorInterfaceExtensions 0.1.1
TableShowUtils 0.1.1
DataValues 0.4.5
1 change: 1 addition & 0 deletions src/QueryOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module QueryOperators
using DataStructures
using IteratorInterfaceExtensions
using TableShowUtils
import DataValues

export Grouping, key

Expand Down
35 changes: 25 additions & 10 deletions src/enumerable/enumerable_defaultifempty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@ end

Base.eltype(iter::Type{EnumerableDefaultIfEmpty{T,S}}) where {T,S} = T

function default_if_empty(source::S) where {S}
T = eltype(source)
_default_value_expr(::Type{T}) where {T} = :( DataValues.DataValue{$T}() )

if T<:NamedTuple
default_value = T(([fieldtype(T,i)() for i in 1:length(fieldnames(T))]...,))
else
default_value = T()
_default_value_expr(::Type{T}) where {T<:DataValues.DataValue} = :( $T() )

function _default_value_expr(::Type{T}) where {T<:NamedTuple}
return :( NamedTuple{$(fieldnames(T))}( ($( (_default_value_expr(fieldtype(T,i)) for i in 1:length(fieldnames(T)))... ),)) )
end

@generated function default_if_empty(source::S) where {S}
T_source = eltype(source)

default_value_expr = _default_value_expr(T_source)

q = quote
default_value = $default_value_expr

T = typeof(default_value)

return EnumerableDefaultIfEmpty{T,$S}(source, default_value)
end

return EnumerableDefaultIfEmpty{T,S}(source, default_value)
return q
end


Expand All @@ -32,14 +44,17 @@ function Base.iterate(iter::EnumerableDefaultIfEmpty{T,S}) where {T,S}
if s===nothing
return iter.default_value, nothing
else
return s
return convert(T,s[1]), s[2]
end
end

function Base.iterate(iter::EnumerableDefaultIfEmpty{T,S}, state) where {T,S}
if state===nothing
state===nothing && return nothing

s = iterate(iter.source, state)
if s===nothing
return nothing
else
return iterate(iter.source, state)
return convert(T, s[1]), s[2]
end
end
4 changes: 4 additions & 0 deletions src/enumerable/enumerable_mapmany.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function expr_contains_ref_to(expr::QuoteNode, var_name::Symbol)
return expr==var_name
end

function expr_contains_ref_to(expr::Function, var_name::Symbol)
return false
end

function mapmany(source::Enumerable, f_collectionSelector::Function, collectionSelector::Expr, f_resultSelector::Function, resultSelector::Expr)
TS = eltype(source)
# First detect whether the collectionSelector return value depends at all
Expand Down

0 comments on commit a312c00

Please sign in to comment.