Skip to content

Commit

Permalink
Stop relying on inference (#18)
Browse files Browse the repository at this point in the history
* removed returntype

* no returntype

* fixed tuple comprehension

* fix size
  • Loading branch information
Pietro Vertechi committed Dec 2, 2017
1 parent 0c40e76 commit 44da4ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
56 changes: 13 additions & 43 deletions src/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ struct Table2Process{T}
kw::Dict{Symbol, Any}
end

Table2Process(table) = Table2Process(table, Dict{Symbol, Any}())
Table2Process(df) = Table2Process(df, Dict{Symbol, Any}())

tuplify(x::Tuple, args...) = (x..., args...)
tuplify(x, args...) = (x, args...)

columnify(t::IndexedTables.AbstractIndexedTable) = columns(dropna(t))
columnify(t) = columnify(table(convert(Columns, t)))

function Table2Process(s::Selector)
enumerable = TableTraits.getiterator(s.table)
T = eltype(enumerable)
splitter = Base._return_type(s.splitby, Tuple{T,})<:Tuple ?
t -> s.splitby(t) : t -> (s.splitby(t),)
if s.kw[:compare]
select_func = t -> (splitter(t)..., s.compare(t), s.across(t), s.x(t), s.y(t))
select_func = t -> tuplify(s.splitby(t), s.compare(t), s.across(t), s.x(t), s.y(t))
else
select_func = t -> (splitter(t)..., s.across(t), s.x(t), s.y(t))
select_func = t -> tuplify(s.splitby(t), s.across(t), s.x(t), s.y(t))
end
column_types = Base._return_type(select_func, Tuple{T,}).parameters
columns = Tuple(_type(S)[] for S in column_types)
fill_cols!(columns, enumerable, select_func)
Table2Process(columns, s.kw)
tuple_vec = [select_func(i) for i in enumerable]
Table2Process(columnify(tuple_vec), s.kw)
end

getcolumn(t::IndexedTables.AbstractIndexedTable, s) = columns(t, s)
Expand All @@ -59,7 +61,7 @@ getlength(t) = size(t, 1)

function Table2Process(s::ColumnSelector)
if s.splitby == Symbol[]
splitter = [fill("y1", size(s.table, 1))]
splitter = [fill("y1", getlength(s.table))]
elseif isa(s.splitby, Symbol)
splitter = [getcolumn(s.table, s.splitby)]
else
Expand Down Expand Up @@ -89,36 +91,4 @@ function Base.filter(f::Function, p::ProcessedTable)
end

nsplits(t::Union{Table2Process, ProcessedTable}) = count(t -> startswith(t, "s"), string.(colnames(t.table)))
listsplits(t::Union{Table2Process, ProcessedTable}) = [Symbol(:s, i) for i = 1:nsplits(t)]

_isnull(x) = false
_isnull(x::DataValue) = isnull(x)

_get(x) = x
_get(x::DataValue) = get(x)

_type(x) = x
_type(x::Type{DataValue{T}}) where T = T


@generated function fill_cols!(columns, enumerable, select_func)
push_exprs = Expr(:block)
for i in find(collect(columns.types) .!= Void)
ex = quote
push!(columns[$i], _get(j[$i]))
end
push!(push_exprs.args, ex)
end
cond_push_exprs = quote
j = select_func(i)
if !any(_isnull, j)
$push_exprs
end
end

quote
for i in enumerable
$cond_push_exprs
end
end
end
listsplits(t::Union{Table2Process, ProcessedTable}) = [Symbol(:s, i) for i = 1:nsplits(t)]
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ for i in 1:length(tables)
check_equality(stored_table, test_table, atol)
end

split_vars = [:Minrty, :Sx]

processed_table = @> school begin
@splitby(_.Minrty)
@splitby(Tuple(getfield(_, s) for s in split_vars))
@x(_.MAch, :continuous)
@y(_.SSS)
ProcessedTable
end

processed_table_col = @> GroupedErrors.ColumnSelector(school) begin
GroupedErrors._splitby(:Minrty)
GroupedErrors._splitby(split_vars)
GroupedErrors._x(:MAch, :continuous)
GroupedErrors._y(:locreg, :SSS)
ProcessedTable
Expand Down

0 comments on commit 44da4ff

Please sign in to comment.