Dirty trick or legitimate style?
- To: mathgroup at smc.vnet.net
- Subject: [mg4036] Dirty trick or legitimate style?
- From: hagai at helix.nih.gov (Hagai Agmon-Snir)
- Date: Tue, 28 May 1996 01:45:51 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Hi, Mathematica experts,
I am improving a package that I wrote a while ago. One of the things I
wanted to add is a function that gets an "operator" (practically, a
Mathematica expression), and performs this operation on the internal tree
structure (that is a part of the Private context of the package). I pass
the operator as a string and use ToExpression to calculate it in the
function (any better ideas?). Problem: the operator will not be calculated
in the correct "Private" context.
I solve this problem by the following "dirty" trick, that seems to work
fine - but ignores the style of Mathematica programming (I think). I
enclose just a tiny portion of the package - but the definition of
EvaluateCommand demonstrate the trick and GeneralTransform shows an
example of actual use.
I am far from being a Mathematica expert, not speaking of package-writing.
If you have any better idea or if you think it is OK with the "Mathematica
code of style" - please let me know. You might also see "real" problems
with this trick (that are worse than the stylistic one).
BeginPackage["MET`",
{"Graphics`Legend`","Graphics`Colors`"}];
EvaluateCommand::usage = " ";
GeneralTransform::usage = " ";
Begin["`Private`"];
EvaluateCommand[stringarg_]:=Module[{tmp},(
BeginPackage["MET`",
{"Graphics`Legend`","Graphics`Colors`"}];
Begin["`Private`"];
tmp=ToExpression[stringarg];
End[];
EndPackage[];
tmp
)]
GeneralTransform[lroot_,stringarg_]:=
Module[{tmp},(
BeginPackage["MET`",
{"Graphics`Legend`","Graphics`Colors`"}];
Begin["`Private`"];
tmp=ToExpression[stringarg];
tmp=Evaluate[tmp]&;
PassTreeRecursive[lroot,
((#[Strl]=(N[tmp[#]]))&)
];
End[];
EndPackage[];
)];
PassTreeRecursive[lroot_,lfunc_] := (
Apply[lfunc,List[lroot]];
If[Length[lroot[Schildren]] >0 ,
Map[PassTreeRecursive[#,lfunc]&,lroot[Schildren]]];
);
End[];
EndPackage[];
Yours,
Hagai Agmon-Snir
>>>>>>>>>>>>>>>>>> ================================ <<<<<<<<<<<<<<<<<
Hagai Agmon-Snir Tel: (301) 496-9972
(301) 496-4325
Surface mail: Fax: (301) 402-0535
Mathematical Research Branch, NIDDK
9190 Rockville Pike - Suite 350
Bethesda, MD 20814-3800
USA
E-mail: hagai at helix.nih.gov WWW: http://mrb.niddk.nih.gov/hagai
<<<<<<<<<<<<<<<<< ================================ >>>>>>>>>>>>>>>>>>
==== [MESSAGE SEPARATOR] ====