2
2
# Licensed under the MIT License. See LICENSE in the project root.
3
3
# ------------------------------------------------------------------
4
4
5
+ const ColSpec = Union{Vector{Symbol}, Regex}
6
+
5
7
"""
6
8
Select(col₁, col₂, ..., colₙ)
7
9
Select([col₁, col₂, ..., colₙ])
8
-
10
+ Select((col₁, col₂, ..., colₙ))
11
+
9
12
The transform that selects columns `col₁`, `col₂`, ..., `colₙ`.
13
+
14
+ Select(regex)
15
+
16
+ Selects the columns that match with `regex`.
10
17
"""
11
- struct Select{N } <: Stateless
12
- cols:: NTuple{N,Symbol}
18
+ struct Select{S <: ColSpec } <: Stateless
19
+ cols:: S
13
20
end
14
21
15
- Select (cols:: NTuple{N,AbstractString} ) where {N} =
16
- Select (Symbol .( cols) )
22
+ Select (cols:: T... ) where {T <: Union{AbstractString, Symbol} } =
23
+ Select (cols)
17
24
18
- Select (cols:: AbstractVector ) = Select (Tuple (cols))
25
+ Select (cols:: NTuple{N, T} ) where {N, T<: Union{AbstractString, Symbol} } =
26
+ Select (collect (cols))
19
27
20
- Select (cols... ) = Select (cols)
28
+ Select (cols:: Vector{T} ) where {T<: AbstractString } =
29
+ Select (Symbol .(cols))
30
+
31
+ Base.:(== )(a:: Select , b:: Select ) = a. cols == b. cols
21
32
22
33
isrevertible (:: Type{<:Select} ) = true
23
34
35
+ _select (cols:: Vector{Symbol} , allcols) = cols
36
+ _select (cols:: Regex , allcols) =
37
+ filter (col -> occursin (cols, String (col)), allcols)
38
+
24
39
function apply (transform:: Select , table)
25
40
# retrieve relevant column names
26
41
allcols = collect (Tables. columnnames (table))
27
- select = collect (transform. cols)
42
+ select = _select (transform. cols, allcols )
28
43
reject = setdiff (allcols, select)
29
44
30
45
# keep track of indices to revert later
76
91
"""
77
92
Reject(col₁, col₂, ..., colₙ)
78
93
Reject([col₁, col₂, ..., colₙ])
94
+ Reject((col₁, col₂, ..., colₙ))
79
95
80
96
The transform that discards columns `col₁`, `col₂`, ..., `colₙ`.
97
+
98
+ Reject(regex)
99
+
100
+ Discards the columns that match with `regex`.
81
101
"""
82
- struct Reject{N } <: Stateless
83
- cols:: NTuple{N,Symbol}
102
+ struct Reject{S <: ColSpec } <: Stateless
103
+ cols:: S
84
104
end
85
105
86
- Reject (cols:: NTuple{N,AbstractString} ) where {N} =
87
- Reject (Symbol .( cols) )
106
+ Reject (cols:: T... ) where {T <: Union{AbstractString, Symbol} } =
107
+ Reject (cols)
88
108
89
- Reject (cols:: AbstractVector ) = Reject (Tuple (cols))
109
+ Reject (cols:: NTuple{N, T} ) where {N, T<: Union{AbstractString, Symbol} } =
110
+ Reject (collect (cols))
90
111
91
- Reject (cols... ) = Reject (cols)
112
+ Reject (cols:: Vector{T} ) where {T<: AbstractString } =
113
+ Reject (Symbol .(cols))
114
+
115
+ Base.:(== )(a:: Reject , b:: Reject ) = a. cols == b. cols
92
116
93
117
isrevertible (:: Type{<:Reject} ) = true
94
118
95
119
function apply (transform:: Reject , table)
96
120
allcols = Tables. columnnames (table)
97
- reject = collect (transform. cols)
98
- select = Tuple ( setdiff (allcols, reject) )
121
+ reject = _select (transform. cols, allcols )
122
+ select = setdiff (allcols, reject)
99
123
strans = Select (select)
100
124
newtable, scache = apply (strans, table)
101
125
newtable, (strans, scache)
104
128
function revert (:: Reject , newtable, cache)
105
129
strans, scache = cache
106
130
revert (strans, newtable, scache)
107
- end
131
+ end
0 commit comments