From 55a0d8b5a765909c4eb33a3d94ce99965b172c49 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Sat, 19 Jan 2019 12:00:23 -0800 Subject: [PATCH] Add @unique macro --- REQUIRE | 2 +- docs/src/experimental.md | 18 ++++++++++++++++++ docs/src/standalonequerycommands.md | 20 ++++++++++++++++++++ src/Query.jl | 2 +- src/standalone_query_macros.jl | 12 ++++++++++++ test/test_standalone.jl | 7 +++++++ 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/REQUIRE b/REQUIRE index 2f07b12e..27fef956 100644 --- a/REQUIRE +++ b/REQUIRE @@ -3,4 +3,4 @@ TableTraits 0.3.1 IterableTables 0.8.2 DataValues 0.4.4 MacroTools 0.4.4 -QueryOperators 0.5.2 +QueryOperators 0.6.0 diff --git a/docs/src/experimental.md b/docs/src/experimental.md index 85c4000e..fa899786 100644 --- a/docs/src/experimental.md +++ b/docs/src/experimental.md @@ -26,3 +26,21 @@ df_children = DataFrame(Name=["Bill", "Joe", "Mary"], Parent=["John", "John", "S df_parents |> @join(df_children, _.Name, _.Parent, {Parent=_.Name, Child=__.Name}) |> DataFrame ``` + +## Key selector in the `@unique` standalone command + +As an experimental feature, one can specify a key selector for the `@unique` command. In that case uniqueness is tested based on that key. + +```jldoctest +using Query + +source = [1,-1,2,2,3] + +q = source |> @unique(abs(_)) |> collect + +println(q) + +# output + +[1, 2, 3] +``` diff --git a/docs/src/standalonequerycommands.md b/docs/src/standalonequerycommands.md index 9fea8931..8f53ca43 100644 --- a/docs/src/standalonequerycommands.md +++ b/docs/src/standalonequerycommands.md @@ -262,6 +262,26 @@ println(q) [4, 5] ``` +## The `@unique` command + +The `@unique command has the form `source |> @unique()`. `source` can be any source that can be queried. The command will filter out any duplicates from the input source. Note that there is also an experimental version of this command that accepts a key selector, see the experimental section in the documentation. + +#### Exmample + +```jldoctest +using Query + +source = [1,1,2,2,3] + +q = source |> @unique() |> collect + +println(q) + +# output + +[1, 2, 3] +``` + ## The `@select` command The `@select` command has the form `source |> @select(selectors...)`. `source` can be any source that can be queried. Each selector of `selectors...` can either select elements from `source` and add them to the result set, or select elements from the result set and remove them. A selector may select or remove an element by name, by position, or using a predicate function. All `selectors...` are executed in order and may not commute. diff --git a/src/Query.jl b/src/Query.jl index 55ffe65c..780e6c10 100644 --- a/src/Query.jl +++ b/src/Query.jl @@ -8,7 +8,7 @@ using QueryOperators export @from, @query, @count, Grouping, key -export @map, @filter, @groupby, @orderby, @orderby_descending, +export @map, @filter, @groupby, @orderby, @orderby_descending, @unique, @thenby, @thenby_descending, @groupjoin, @join, @mapmany, @take, @drop export @select, @rename, @mutate diff --git a/src/standalone_query_macros.jl b/src/standalone_query_macros.jl index 57034821..0655e8fe 100644 --- a/src/standalone_query_macros.jl +++ b/src/standalone_query_macros.jl @@ -237,3 +237,15 @@ end macro drop(n) return :( i -> QueryOperators.drop(QueryOperators.query(i), $(esc(n)))) end + +macro unique() + return :( i -> QueryOperators.unique(QueryOperators.query(i), q->q, :(q->q))) |> + helper_namedtuples_replacement +end + +macro unique(f) + f_as_anonym_func = helper_replace_anon_func_syntax(f) + q = Expr(:quote, helper_replace_anon_func_syntax(f_as_anonym_func)) + return :( i -> QueryOperators.unique(QueryOperators.query(i), $(esc(f_as_anonym_func)), $(esc(q)))) |> + helper_namedtuples_replacement +end diff --git a/test/test_standalone.jl b/test/test_standalone.jl index fb7beea5..4ed987ac 100644 --- a/test/test_standalone.jl +++ b/test/test_standalone.jl @@ -44,4 +44,11 @@ end @test df2[:c] == ["b","c"] end +@testset "@unique operator" begin + df = DataFrame(a=[1,2,1], b=[3.,3.,3.]) + + @test df |> @unique() |> collect == [(a=1,b=3.), (a=2,b=3.)] + @test df |> @unique(_.b) |> collect == [(a=1,b=3.)] +end + end