我有一个
f:=proc(x,option1,option2) ... end proc;
过程中的过程 option1
始终是整数, option2
是列表或其他内容(包括整数)。这两个选项都是可选的,因此这些命令可以按预期工作:
f(x);
f(x,3);
f(x,4,[1,2]);
f(x,5,3);
f(x,6,expand);
如果未指定 option1
,那么我不知道一种简单的处理方法
f(x,,3);
f(x,,[1,2]);
但是, 让它理解,
f(x,[1,2]);
但我仍然有问题
f(x,3);
,因为尚不清楚 3
是 option1
或 option2
。我可以重写代码以以这种格式理解函数调用
f(x,[1,option1],[2,option2]);
f(x,[1,option1]);
f(x,[2,option2]);
,但是我很好奇是否有更简单的方法可以实现这一目标事情。
I have a procedure
f:=proc(x,option1,option2) ... end proc;
In my case option1
is always an integer and option2
is either a list or something else (including integer). Both options are optional, so these commands work as expected:
f(x);
f(x,3);
f(x,4,[1,2]);
f(x,5,3);
f(x,6,expand);
But if option1
isn't specified then I don't know an easy way to deal with it since Maple doesn't allow the usage like
f(x,,3);
f(x,,[1,2]);
I can make it understand
f(x,[1,2]);
but I still have a problem with
f(x,3);
since it's not clear if 3
is option1
or option2
. I can rewrite the code to understand function calls in this format
f(x,[1,option1],[2,option2]);
f(x,[1,option1]);
f(x,[2,option2]);
but I'm curious if there is a simpler way to achieve that since for some Maple functions (like plot
) the order of most options doesn't matter.
发布评论
评论(3)
使用关键字参数。
您可以做到
和
并
确保
现在,
通话时使用
'op1'= value
,而不是op1 = value
Use keyword arguments.
Now you can do
And
And
And
And
Make sure to use
'op1'=value
when calling, and notop1=value
正如其他人已经提到的那样,一种解决方案是使用“ kwarg ”(关键字参数)。您也可以使用
_经过
或_REST
。您可以在Maple帮助页面或Maple编程指南中阅读更多信息()。这只是一个示例,您可以如何使用它们。
_通过
是为了说出已将其传递给您的过程的任何内容,而_REST
是针对已将其传递给您的过程的任何内容到proc
前面的括号内提到的参数。假设我们要定义一个具有1个必要参数的过程,并可能有2个可选的参数。如果给出了两个可选参数,我们假设第一个始终是option1
,第二个是option2 ,但是如果给出一个可选参数,则根据是否它是类型
Integer
是否分别为option1
或option2 。
要询问传递的数量或通过的其余参数,您可以使用
_npassed
和_NREST
。命令分配()
检查是否分配了一个值。您可以通过类型( - , - )
或- :: -
检查是否是特定类型的东西。因此,这是简单的代码。这是针对不同输入的上述过程的输出的屏幕截图。
As others already mentioned one solution is using "kwarg" (keyword argument). You can also use
_passed
or_rest
. You can read more in Maple help pages or Maple programming guide (https://www.maplesoft.com/documentation_center/Maple2021/ProgrammingGuide.pdf).Here is just an example how you can use them.
_passed
is for when you want to say whatever that has been passed to your procedure, and_rest
is for whatever that has been passed to your procedure except the ones that are already assigned to the parameters you mentioned inside the parentheses in front ofproc
. Let's say we want to define a procedure with 1 necessary argument and possible 2 optional arguments. If there are two optional arguments given, we assume the first one is alwaysoption1
and the second one isoption2
, but if only one optional argument is given, then depending on if it is of typeinteger
or not it will beoption1
oroption2
respectively.To ask the number of passed or the rest of passed arguments you can use
_npassed
and_nrest
. And the commandassigned()
checks if something is assigned a value or not. You can check if something is of a specific type, bytype(-,-)
or- :: -
. So here is the simple code.Here is a screenshot of the output of the above procedure for different inputs.
大多数绘图命令都使用Maple更现代的管理过程选项。
特别是绘制命令的大多数选项都提供为SO-CALLED 关键字选项。这会自动提供(此类选项)的位置无关紧要的功能。
例如,
正如您注意到的那样,[从逻辑上]您无法使用多个 *位置/有序的“如果两者都有相同的类型,并且丢失了一些类型
。 href =“ https://www.maplesoft.com/support/help/help/maple/view.aspx?path = argument_processing#positional” rel =“ nofollow noreferrer”> positional promotal参数,尽管自然会失去其灵活性例如,任意
。
位置
Most of the plotting commands use Maple's more modern argument-processing to manage procedure options.
In particular most options to plotting commands are provided as so-called keyword options. That automatically provides the functionlity in which the location (of such options) doesn't matter.
For example,
As you've noticed, you [logically] cannot use multiple *positional/ordered" parameters if both have the same type and some earlier ones are missing.
If you really wanted you could make one of those options into a positional parameter, although naturally that would lose its flexibility of arbitrary placement. For example,
There are too many variants to show them all here, sensibly.
You don't have to use the name "NoUserValue" as the default values.