Skip to content

Commit

Permalink
Add @unique macro
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff committed Jan 19, 2019
1 parent ded19c5 commit 55a0d8b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions docs/src/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
20 changes: 20 additions & 0 deletions docs/src/standalonequerycommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/standalone_query_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/test_standalone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 55a0d8b

Please sign in to comment.