Skip to content

Commit

Permalink
Merge pull request #3 from davidagold/select
Browse files Browse the repository at this point in the history
Solution to issue #2
  • Loading branch information
davidagold authored Jul 21, 2016
2 parents 8dda21b + 0d0cc5a commit b8bfa4f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
26 changes: 23 additions & 3 deletions src/groupby.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
macro groupby(fields...)
g = _groupby(gensym(), collect(fields))
#=
@groupby experiences same issue concerning piped and non-piped data inputs as
does @select. See top of `src/select.jl` for details and current solution
=#
macro groupby(args...)
# for case where first arg is data input
input, cols = args[1], args[2:end]
_input = QuoteNode(input)
g1 = _groupby(input, collect(cols))
# for case where all args are column specifications
g2 = _groupby(gensym(), collect(args))
return quote
run($g)
try # assume first that first arg is data input
run($(esc(input)), $g1)
catch err
#= if error because first arg isn't valid name, assume it is a
column specification and return curried run. Otherwise, throw
the error =#
if err == UndefVarError($_input)
run($g2)
else
throw(err)
end
end
end
end

Expand Down
35 changes: 28 additions & 7 deletions src/select.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
#=
TO DO: figure out how to differentiate b/w piped to and non-piped to
@select calls. Currently, only the former are handled. The issue is how to
distinguish a symbol that represents a data source argument from a symbol
that represents a field name argument
OPEN QUESTION: How to differentiate b/w piped to and non-piped to
@select calls. The issue is how to distinguish a symbol that represents a
data source argument from a symbol that represents a column specification.
The following is one way to do so. However, this strategy will fail/give
incorrect results in cases such as the following:
df |> @select(fieldname)
where `df` and `fieldname` are both valid names bound to DataFrame objects.
=#
macro select(syms...)
g = _select(gensym(), collect(syms))
macro select(args...)
# for case where first arg is data input
input, cols = args[1], args[2:end]
_input = QuoteNode(input)
g1 = _select(input, collect(cols))
# for case where all args are column specifications
g2 = _select(gensym(), collect(args))
return quote
run($g)
try # assume first that first arg is data input
run($(esc(input)), $g1)
catch err
#= if error because first arg isn't valid name, assume it is a
column specification and return curried run. Otherwise, throw
the error =#
if err == UndefVarError($_input)
run($g2)
else
throw(err)
end
end
end
end

Expand Down

0 comments on commit b8bfa4f

Please sign in to comment.