Function as return value, how to document it (::usage)
- To: mathgroup at smc.vnet.net
- Subject: [mg97488] Function as return value, how to document it (::usage)
- From: "Karsten W." <Karsten.G.Weinert at googlemail.com>
- Date: Sat, 14 Mar 2009 05:38:12 -0500 (EST)
Hello,
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},
localfunc[0] = start;
localfunc[x_Integer] := localfunc[x - 1] + factor*x;
localfunc[] := "empty";
localfunc::usage = "my help text here.";
localfunc
];
myf = funcBuilder[1, 1];
myf[]
Table[myf[i], {i, 1, 5}]
?myf
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
key).
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,
Length[firstRow]}]}];
datafunc[id_] := First[Select[data, Part[#, pk /. colNameRules] ==
id &]];
datafunc[id_, colName_String] := Part[datafunc[id], colName /.
colNameRules];
datafunc[id_, colNames_List] := Part[datafunc[id], colNames /.
colNameRules];
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,
Karsten.