Re: Function in a package
- To: mathgroup at smc.vnet.net
- Subject: [mg42526] Re: [mg42493] Function in a package
- From: Dr Bob <drbob at bigfoot.com>
- Date: Sat, 12 Jul 2003 05:19:34 -0400 (EDT)
- References: <200307110657.CAA17172@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
First of all, you've written the test function as if, at all costs, you don't want anybody to understand what it does. Here's a simpler function that MAY do the same thing. test[dat_, opts___?OptionQ] := Module[{n = norm /. Flatten@{opts, norm -> fst}}, Which[ n === fst, First[dat], n === max, Max[dat] ] ] I say MAY, because a lot depends on whether you intended Global values of norm, fst, and max to have an effect on the outcome. Probably one of those had a value when you ran your tests, and that accounts for the difference in outcomes. It's generally bad form (counter to functional programming principles) to have a function depend on global variables. It's not always a bad idea, but it's certainly a bad idea to do it without thinking about it. Also, as you wrote the function (and me too), the 'norm' that you use in a call to the function is global, but the norm mentioned twice inside the function is a Private version of norm, so the function can't operate as you probably intend, even if norm doesn't have a Global value. This could also explain the behavior you're seeing. I have to leave you at this point, as I haven't learned all the mysteries of options in package functions myself! I'll be interested to see other answers to this. Bobby On Fri, 11 Jul 2003 02:57:54 -0400 (EDT), Roger Mason <rmason at sparky2.esd.mun.ca> wrote: > Hello, > > I have written and tested successfully a function in a notebook. On > putting the function in a package it ceases to work. > > Here is the package: > > BeginPackage["FredNoEDA`"] > > TestPkgNoEDA::usage = "a test function."; > > Begin["`Private`"] > > TestPkgNoEDA[dat_, opts___?OptionQ] := Module[ > {n, Tow, Tow0}, > n = norm /. Flatten[{opts}] /. {norm -> fst}; > Tow = dat; > If[ > n == fst, Tow0 = First[Tow] > ]; > If[ > n == max, Tow0 = Tow[[ > Position[Tow, Max[Tow] ][[1, 1]] > ]] > ]; > Tow0 > ] > > End[] > > Protect[TestPkgNoEDA] > > EndPackage[]; > > Here is the identical function (under a different name) from a notebook: > > TestPkg[dat_, opts___?OptionQ] := Module[ > {n, Tow, Tow0}, > n = norm /. Flatten[{opts}] /. {norm -> fst}; > Tow = dat; > If[ > n == fst, Tow0 = First[Tow] > ]; > If[ > n == max, Tow0 = Tow[[ > Position[Tow, Max[Tow] ][[1, 1]] > ]] > ]; > Tow0 > ] > > Here are some test data: > > In[2]:= > tst = Table[x, {x, 1, 10}] > > Out[2]= > {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} > > Here are the results of a test: > > Needs["FredNoEDA`"] > > In[6]:= > TestPkgNoEDA[tst, norm -> max] > > Out[6]= > 1 > > In[11]:= > TestPkg[tst, norm -> max] > > Out[11]= > 10 > > Can anyone explain why the version of the function in the package returns > an incorrect result? > > Thanks, > Roger Mason > > -- majort at cox-internet.com Bobby R. Treat
- References:
- Function in a package
- From: Roger Mason <rmason@sparky2.esd.mun.ca>
- Function in a package