Skip to content

Commit

Permalink
Add @take and @drop macros
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff committed Dec 19, 2017
1 parent 7868be0 commit 68cc983
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Query.jl v0.9.0 Release Notes
* Add @take and @drop standalone macros
* Fix some test bugs

# Query.jl v0.8.0 Release Notes
* Add @groupjoin, @join and @mapmany standalone macros
* Move backend code to QueryOperators.jl
Expand Down
10 changes: 9 additions & 1 deletion docs/src/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Query.jl. At the same time any feedback on these features would be
especially welcome.

The `@map`, `@filter`, `@groupby`, `@orderby` (and various variants),
`@groupjoin`, `@join` and `@mapmany` commands can be used in standalone
`@groupjoin`, `@join`, `@mapmany`, `@take` and `@drop` commands can be used in standalone
versions. Those standalone versions are especially convenient in
combination with the pipe syntax in julia. Here is an example that
demonstrates their use:
Expand Down Expand Up @@ -124,6 +124,14 @@ be an anonymous function that takes one argument and returns a collection.
`result_selector` must be an anonymous function that takes two arguments.
It will be applied to each element of the intermediate collection.

### The `@take` command

The `@take` command has the form `@take(source, n)`. `source` can be any source that can be queried. `n` must be an integer, and it specifies how many elements from the beginning of the source should be kept.

### The `@drop` command

The `@drop` command has the form `@drop(source, n)`. `source` can be any source that can be queried. `n` must be an integer, and it specifies how many elements from the beginning of the source should be dropped from the results.

## The `..` syntax

The syntax `a..b` is translated into `map(i->i.b, a)` in any query
Expand Down
2 changes: 1 addition & 1 deletion src/Query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import QueryOperators: QueryProvider
export @from, @query, @count, Grouping, @NT

export @map, @filter, @groupby, @orderby, @orderby_descending,
@thenby, @thenby_descending, @groupjoin, @join, @mapmany
@thenby, @thenby_descending, @groupjoin, @join, @mapmany, @take, @drop

include("query_utils.jl")
include("query_translation.jl")
Expand Down
16 changes: 16 additions & 0 deletions src/standalone_query_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,19 @@ macro filter(f)
helper_namedtuples_replacement |>
helper_replace_field_extraction_syntax
end

macro take(source, n)
return :(QueryOperators.take(QueryOperators.query($(esc(source))), $(esc(n))))
end

macro take(n)
return :( i -> QueryOperators.take(QueryOperators.query(i), $(esc(n))))
end

macro drop(source, n)
return :(QueryOperators.drop(QueryOperators.query($(esc(source))), $(esc(n))))
end

macro drop(n)
return :( i -> QueryOperators.drop(QueryOperators.query(i), $(esc(n))))
end
47 changes: 47 additions & 0 deletions test/test_standalone.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Query
using DataFrames
using Base.Test

@testset "Standalone Syntax" begin

@testset "@take operator" begin
df = DataFrame(a=[1,2,3], b=[3.,2.,1.], c=["a", "b", "c"])

df2 = df |> @take(2) |> DataFrame

@test df2 isa DataFrame
@test size(df2) == (2,3)
@test df2[:a] == [1,2]
@test df2[:b] == [3.,2.]
@test df2[:c] == ["a", "b"]

df2 = DataFrame(@take(df, 2))

@test df2 isa DataFrame
@test size(df2) == (2,3)
@test df2[:a] == [1,2]
@test df2[:b] == [3.,2.]
@test df2[:c] == ["a", "b"]
end

@testset "@drop operator" begin
df = DataFrame(a=[1,2,3], b=[3.,2.,1.], c=["a", "b", "c"])

df2 = df |> @drop(1) |> DataFrame

@test df2 isa DataFrame
@test size(df2) == (2,3)
@test df2[:a] == [2,3]
@test df2[:b] == [2.,1.]
@test df2[:c] == ["b","c"]

df2 = DataFrame(@drop(df, 1))

@test df2 isa DataFrame
@test size(df2) == (2,3)
@test df2[:a] == [2,3]
@test df2[:b] == [2.,1.]
@test df2[:c] == ["b","c"]
end

end

0 comments on commit 68cc983

Please sign in to comment.