I am trying to make my own package based on the code from this Question. The following is the code from that page:
BeginPackage["SelectData`"];
select::usage = "select[data,where,condition]";
Begin["`Private`"]
ClearAll[select, where];
SetAttributes[where, HoldAll];
select[table : {colNames_List, rows__List}, w : (where[condition_] | None) : None,
cols : (columns[varNames__] | All) : All] :=
With[{namingRules = Dispatch[Thread[colNames -> Thread[Slot[Range[Length[colNames]]]]]]},
With[{cl = {varNames} /. namingRules /. Verbatim[Slot][n_] :> n},
If[cols === All, #, #[[All, cl]]] &@
If[w === None,
{rows},
(* else *)
With[{selF = Apply[Function, Hold[condition] /. namingRules]},
Select[{rows}, selF @@ # &]
]
]
]
];
End[]
EndPackage[]
Then, on my notebook, use the following data as input:
table = List[{"ID", "Variable 1", "Variable 2"}, {"Alpha", 1,
0}, {"Beta", 1, 1}, {"Alpha", 0, 0}]
Call the package, Needs["SelectData`"]
Evaluating, select[table, columns["Variable 1"]]
the output is as follows:
select[{{"ID", "Variable 1", "Variable 2"}, {"Alpha", 1, 0}, {"Beta",
1, 1}, {"Alpha", 0, 0}}, columns["Variable 1"]]
I think I have done something wrong when creating my package, but I can't find it. Could anyone give me suggestions?
where
andcolumns
also as exported symbols, likewhere::usage = ...; columns::usage = ...
. Otherwise,where
andcolumns
are created inPrivate`
sub-context, and the pattern does not match. $\endgroup$