       Re: Function as return value, how to document it

• To: mathgroup at smc.vnet.net
• Subject: [mg97520] Re: [mg97488] Function as return value, how to document it
• From: Bob Hanlon <hanlonr at cox.net>
• Date: Sat, 14 Mar 2009 18:13:36 -0500 (EST)
• Reply-to: hanlonr at cox.net

```SetAttributes[funcBuilder, HoldFirst]

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

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

empty

{2,4,7,11,16}

myf[x] my help text here.

Bob Hanlon

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

=============
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 = 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.

```

• 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)