MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Function as return value, how to document it

SetAttributes[funcBuilder, HoldFirst]

funcBuilder[func_Symbol, start_, factor_] :=
  func[0] = start;
  func[x_Integer] := func[x - 1] + factor*x;
  func[] = "empty";
  func::usage = ToString[func] <>
    "[x] my help text here."]

funcBuilder[myf, 1, 1];
Table[myf[i], {i, 1, 5}]



myf[x] my help text here.

Bob Hanlon

---- "Karsten W." <Karsten.G.Weinert at> wrote: 


my function is a function builder, when it is called, it returns a
function. Everything works fine, except for the help text.
Here is a minimal example:

funcBuilder[start_, factor_] := Module[
   localfunc[0] = start;
   localfunc[x_Integer] := localfunc[x - 1] + factor*x;
   localfunc[] := "empty";
   localfunc::usage = "my help text here.";
myf = funcBuilder[1, 1];
Table[myf[i], {i, 1, 5}]

 It displays "localfunc$7151" instead of "my help text here". Do you
have a hint on how to improve this?

To put this problem into a larger perspective, here is what I really
want to do with this function builder. I need to read several Excel
sheets and want a nice interface. I have some assumptions on the Excel
sheets: data is rectangular, first row is column names,
there is one column that has unique values (something like a primary

Now I have defined a wrapper for the Import function as follows:

ReadExcelSheet[file_, sheet_, pk_] :=
  Module[{firstRow, colNameRules, data, datafunc},
   data = Import[file, {"Sheets", sheet}];
   firstRow = First[data];
   data = Rest[data];
   colNameRules = MapThread[#1 -> #2 &, {firstRow, Table[i, {i, 1,
   datafunc[id_] := First[Select[data, Part[#, pk /. colNameRules] ==
id &]];
   datafunc[id_, colName_String] := Part[datafunc[id], colName /.
   datafunc[id_, colNames_List] := Part[datafunc[id], colNames /.
   datafunc[] := colNameRules;
   datafunc[Null] := data;
   datafunc[Null, colName_String] := Map[ Part[#, colName /.
colNameRules] &, data];
   datafunc[Null, colNames_List] := Map[ Part[#, colNames /.
colNameRules] &, data];
   datafunc (* return value *)

The function returns a function which allows data access like this
(assuming we have a sheet with columns id and each month):

ProductionData = ReadExcelSheet["myfile.xls", "mysheet", "id"]
ProductionData["id1", "Jan"] (* returns one number *)
ProductionData["id1", {"Jan", "Feb"}] (* returns two values *)
ProductionData[Null, {"id", "Jan"}] (* returns id and january values
for all plants *)
ProductionData[Null] (*returns all values, but not the headers*)
ProductionData[] (* returns transformation rules column names -> list
index *)

I hope that this approach does not have negative side effects ?!?
Also, I would be interested in different approaches to this data
access problem. And, I would like to add some information of the excel
sheet (file and sheet name, timestamp, some usage information).

Kind regards,


  • Prev by Date: BarChart - Extendng the Y-axis and labeling the endpoints
  • Next by Date: Re: font size
  • Previous by thread: Re: BarChart - Extendng the Y-axis and labeling the endpoints
  • Next by thread: how to keep multiplication sign "*" ( Please)