22# Licensed under the MIT License. See LICENSE in the project root.
33# ------------------------------------------------------------------
44
5- const TargetName = Union{Symbol,AbstractString}
6- const PairWithTarget = Pair{<: Any ,<: Pair{<:Function,<:TargetName} }
7- const PairWithoutTarget = Pair{<: Any ,<: Function }
8- const MapPair = Union{PairWithTarget,PairWithoutTarget}
5+ # supported argument types
6+ const Callable = Union{Function,Type}
7+ const Target = Union{Symbol,AbstractString}
8+ const ColsCallableTarget = Pair{<: Any ,<: Pair{<:Callable,<:Target} }
9+ const ColsCallable = Pair{<: Any ,<: Callable }
10+ const CallableTarget = Pair{<: Callable ,<: Target }
11+ const MapArg = Union{ColsCallableTarget,ColsCallable,CallableTarget,Callable}
912
1013"""
1114 Map(cols₁ => fun₁ => target₁, cols₂ => fun₂, ..., colsₙ => funₙ => targetₙ)
1215
1316Applies the `funᵢ` function to the columns selected by `colsᵢ` using
1417the `map` function and saves the result in a new column named `targetᵢ`.
18+ Types are also allowed in place of functions to construct objects with
19+ arguments from the columns.
1520
1621The column selection can be a single column identifier (index or name),
17- a collection of identifiers or a regular expression (regex).
22+ a collection of identifiers or a regular expression (regex). It can also
23+ be ommited to apply the function to all columns.
1824
1925Passing a target column name is optional and when omitted a new name
2026is generated by joining the function name with the selected column names.
@@ -28,11 +34,14 @@ Map([2, 3] => ((b, c) -> 2b + c))
2834Map([:a, :c] => ((a, c) -> 2a * 3c) => :col1)
2935Map(["c", "a"] => ((c, a) -> 3c / a) => :col1, "c" => tan)
3036Map(r"[abc]" => ((a, b, c) -> a^2 - 2b + c) => "col1")
37+ Map(sin => "seno")
38+ Map(cos)
3139```
3240
3341## Notes
3442
35- * Anonymous functions must be passed with parentheses as in the examples above;
43+ * Anonymous functions must be passed with parentheses as in the examples above
44+
3645* Some function names are treated in a special way, they are:
3746 * Anonymous functions: `#1` -> `f1`;
3847 * Composed functions: `outer ∘ inner` -> `outer_inner`;
@@ -41,20 +50,20 @@ Map(r"[abc]" => ((a, b, c) -> a^2 - 2b + c) => "col1")
4150"""
4251struct Map <: StatelessFeatureTransform
4352 selectors:: Vector{ColumnSelector}
44- funs:: Vector{Function }
53+ funs:: Vector{Callable }
4554 targets:: Vector{Union{Nothing,Symbol}}
4655end
4756
48- Map () = throw (ArgumentError (" cannot create Map transform without arguments" ))
49-
50- function Map (pairs:: MapPair... )
51- tuples = map (_extract, pairs)
52- selectors = [t[1 ] for t in tuples]
53- funs = [t[2 ] for t in tuples]
54- targets = [t[3 ] for t in tuples]
55- Map (selectors, funs, targets)
57+ function Map (args:: MapArg... )
58+ tups = map (_extract, args)
59+ sels = [t[1 ] for t in tups]
60+ funs = [t[2 ] for t in tups]
61+ tars = [t[3 ] for t in tups]
62+ Map (sels, funs, tars)
5663end
5764
65+ Map () = throw (ArgumentError (" cannot create Map transform without arguments" ))
66+
5867function applyfeat (transform:: Map , feat, prep)
5968 cols = Tables. columns (feat)
6069 names = Tables. columnnames (cols)
@@ -76,13 +85,10 @@ function applyfeat(transform::Map, feat, prep)
7685 newfeat, nothing
7786end
7887
79- _extract (p:: PairWithTarget ) = selector (first (p)), first (last (p)), Symbol (last (last (p)))
80- _extract (p:: PairWithoutTarget ) = selector (first (p)), last (p), nothing
81-
82- _funname (fun:: Base.Fix1 ) = " fix1_" * _funname (fun. f)
83- _funname (fun:: Base.Fix2 ) = " fix2_" * _funname (fun. f)
84- _funname (fun:: ComposedFunction ) = _funname (fun. outer) * " _" * _funname (fun. inner)
85- _funname (fun) = string (fun)
88+ _extract (arg:: ColsCallableTarget ) = selector (first (arg)), first (last (arg)), Symbol (last (last (arg)))
89+ _extract (arg:: ColsCallable ) = selector (first (arg)), last (arg), nothing
90+ _extract (arg:: CallableTarget ) = AllSelector (), first (arg), Symbol (last (arg))
91+ _extract (arg:: Callable ) = AllSelector (), arg, nothing
8692
8793function _makename (snames, fun)
8894 funname = _funname (fun)
@@ -91,3 +97,8 @@ function _makename(snames, fun)
9197 end
9298 Symbol (funname, :_ , join (snames, " _" ))
9399end
100+
101+ _funname (fun:: Base.Fix1 ) = " fix1_" * _funname (fun. f)
102+ _funname (fun:: Base.Fix2 ) = " fix2_" * _funname (fun. f)
103+ _funname (fun:: ComposedFunction ) = _funname (fun. outer) * " _" * _funname (fun. inner)
104+ _funname (fun) = string (fun)
0 commit comments