Leaving values for options unevaluated in Mathematica -
i'm having problems writing function takes options. 1 of option values function. 1 @ value keep unevaluated. tried every single thing possibly think of nothing worked far.
basically, illustrate tried:
setattributes[foo, holdrest]; options[foo] = {blah -> none} foo[x_, optionspattern[]] := module[{blah}, blah = optionvalue[automatic, automatic, blah, hold]; . . .
then when have:
func[a_, b_, c_] := + b + c;
i'd able call foo with:
foo[2, blah -> func[1, 2, 3]]
and have "blah" variable (inside foo) unevaluated, i.e. blah = func[1, 2, 3].
thanks in advance!
edit:
for reasons long elaborate, cannot use ruledelayed (:>). i'm trying write function in package, used other people don't know mathematica, have no clue :> is. using rules (->) specifying options , values standard way , familiar that.
so further illustrate, let's i'm trying write number generator function takes function generates actual number 1 of it's options:
options[generatenumbers] = {generatorfunction -> none}; generatenumbers[n_, optionspattern[]] := module[{func}, func = optionvalue[generatorfunction]; table[func, {n}] ] ]
now, if called function values follows:
generatenumbers[5, generatorfunction -> randomreal[10]]
it return list of 5 numbers same, since randomreal[10] gets evaluated once , not @ every iteration of table. want prevent this. problem more complicated it's along these lines.
thanks!
use name optionspattern
, wrap captured sequence object list
, unevaluated
. minimal way of capturing right-hand side blah
is:
setattributes[foo, holdrest]; options[foo] = {blah -> none}; foo[x_, opts : optionspattern[]] := module[{blah}, blah = optionvalue[foo, unevaluated[{opts}], blah, hold]; blah]
testing out:
in[2]:= foo[x, blah -> (1 + 1)] out[2]= hold[1 + 1]
Comments
Post a Comment