5
$\begingroup$

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?

$\endgroup$
11
  • $\begingroup$ @b.gatessucks It was typo. I fixed it $\endgroup$
    – newbie
    Feb 12, 2014 at 10:43
  • $\begingroup$ I'd suggest ; after Begin, End and EndPackage. After package is loaded what does "?select" give? $\endgroup$
    – Ymareth
    Feb 12, 2014 at 10:53
  • $\begingroup$ Does your function work if its not in the package? $\endgroup$
    – Ymareth
    Feb 12, 2014 at 10:56
  • $\begingroup$ @Ymareth Using "?select" just returns me the code, not what I define in usage. And Yes, it works when it is not the package. $\endgroup$
    – newbie
    Feb 12, 2014 at 10:56
  • 2
    $\begingroup$ You have to declare where and columns also as exported symbols, like where::usage = ...; columns::usage = .... Otherwise, where and columns are created in Private` sub-context, and the pattern does not match. $\endgroup$ Feb 12, 2014 at 11:16

1 Answer 1

5
$\begingroup$

There are two issues:

  1. The function columns needs to be a public function, instead a private one. This is because you have tried to access it from outside. Thus you should also add a line before begin of Private

    columns::usage = "something"

    Similarly, as @LeonidShifrin and @rasher have mentioned, if you also want to access where from outside the package, you should also add

    where::usage = "something"

  2. (Minor, but related to a comment) You shouldn't ClearAll[select] after the usage information is defined. Otherwise, the usage is no longer kept. That's why ?select gives the code directly. If you want to clear select, move it to before select::usage. Also, you don't have to clear where, because it is a private variable. There shouldn't be naming conflict unless by purpose.

$\endgroup$
13
  • $\begingroup$ ……You mean the only necessary thing here is to add column::usage = "" and remove the ClearAll[select, where]; ? $\endgroup$
    – xzczd
    Feb 12, 2014 at 11:35
  • $\begingroup$ Yes. After that the example given by the OP returns some nontrivial results (I haven't read the code and thus don't know the meaning of the return value though.) $\endgroup$
    – Yi Wang
    Feb 12, 2014 at 11:38
  • $\begingroup$ ……Are you sure? I can't fix OP's package by these steps, and even with the ClearAll existed, ?select still gives the desired result. I'm using v8.0.4 $\endgroup$
    – xzczd
    Feb 12, 2014 at 11:41
  • $\begingroup$ Did you quit the kernel or restart Mathematica after the modifications? Note that the OP used Needs to load package. In this method if the package is loaded previously, it is not to be reloaded. You can either use Get (i.e. <<) to load package. In this case you don't need to restart Mathematica when modifications are made in the package. $\endgroup$
    – Yi Wang
    Feb 12, 2014 at 11:46
  • $\begingroup$ +1 - though I think where needs the same treatment... $\endgroup$
    – ciao
    Feb 12, 2014 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.