处理多个可选参数

发布于 2025-01-31 09:03:58 字数 682 浏览 3 评论 0 原文

我有一个

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾旎 2025-02-07 09:03:58

使用关键字参数。

f:=proc(x,{op1::integer:=0,op2::{list,integer}:={}},$)

   if has([_passed],'op1') then
      print("op1 =",op1);
   else
      print("op1 not passed");
   fi;

   if has([_passed],'op2') then
      print("op2 =",op2);
   else
      print("op2 not passed");
   fi;

    #rest of code
end proc;

您可以做到

f(x,'op2'=[1,2,3])    
                        "op1 not passed"    
                       "op2 =", [1, 2, 3]

f(x,'op1'=99)
                          "op1 =", 99
                        "op2 not passed"

f(x)
                        "op1 not passed"    
                        "op2 not passed"

确保

f(x,'op1'=99,'op2'=[1,2,3])
                          "op1 =", 99
                       "op2 =", [1, 2, 3]

现在,

f(x,'op1'=99,'op2'=19827)
                          "op1 =", 99
                         "op2 =", 19827

通话时使用'op1'= value ,而不是 op1 = value

Use keyword arguments.

f:=proc(x,{op1::integer:=0,op2::{list,integer}:={}},$)

   if has([_passed],'op1') then
      print("op1 =",op1);
   else
      print("op1 not passed");
   fi;

   if has([_passed],'op2') then
      print("op2 =",op2);
   else
      print("op2 not passed");
   fi;

    #rest of code
end proc;

Now you can do

f(x,'op2'=[1,2,3])    
                        "op1 not passed"    
                       "op2 =", [1, 2, 3]

And

f(x,'op1'=99)
                          "op1 =", 99
                        "op2 not passed"

And

f(x)
                        "op1 not passed"    
                        "op2 not passed"

And

f(x,'op1'=99,'op2'=[1,2,3])
                          "op1 =", 99
                       "op2 =", [1, 2, 3]

And

f(x,'op1'=99,'op2'=19827)
                          "op1 =", 99
                         "op2 =", 19827

Make sure to use 'op1'=value when calling, and not op1=value

此生挚爱伱 2025-02-07 09:03:58

正如其他人已经提到的那样,一种解决方案是使用“ kwarg ”(关键字参数)。您也可以使用 _经过 _REST 。您可以在Maple帮助页面或Maple编程指南中阅读更多信息()。

这只是一个示例,您可以如何使用它们。 _通过是为了说出已将其传递给您的过程的任何内容,而 _REST 是针对已将其传递给您的过程的任何内容到 proc 前面的括号内提到的参数。假设我们要定义一个具有1个必要参数的过程,并可能有2个可选的参数。如果给出了两个可选参数,我们假设第一个始终是 option1 ,第二个是 option2 ,但是如果给出一个可选参数,则根据是否它是类型 Integer 是否分别为 option1 option2 。

要询问传递的数量或通过的其余参数,您可以使用 _npassed _NREST 。命令分配()检查是否分配了一个值。您可以通过类型( - , - ) - :: - 检查是否是特定类型的东西。因此,这是简单的代码。

test := proc( x )
    local option1, option2:
    if _nrest = 2 then
        option1 := _rest[1]:
        option2 := _rest[2]:
    elif _nrest = 1 then
        if _rest[1] :: integer then
            option1 := _rest[1]:
        else
            option2 := _rest[1]:
        end if:
    end if:
    printf( "necessary argument is %a\n", x ):
    if assigned( option1 ) then
        printf( "option 1 is given and is %d\n", option1 ):
    end if:
    if assigned( option2 ) then
        printf( "option 2 is given and is %a\n", option2 ):
    end if:
end proc:

这是针对不同输入的上述过程的输出的屏幕截图。

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 of proc. 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 always option1 and the second one is option2, but if only one optional argument is given, then depending on if it is of type integer or not it will be option1 or option2 respectively.

To ask the number of passed or the rest of passed arguments you can use _npassed and _nrest. And the command assigned() checks if something is assigned a value or not. You can check if something is of a specific type, by type(-,-) or - :: -. So here is the simple code.

test := proc( x )
    local option1, option2:
    if _nrest = 2 then
        option1 := _rest[1]:
        option2 := _rest[2]:
    elif _nrest = 1 then
        if _rest[1] :: integer then
            option1 := _rest[1]:
        else
            option2 := _rest[1]:
        end if:
    end if:
    printf( "necessary argument is %a\n", x ):
    if assigned( option1 ) then
        printf( "option 1 is given and is %d\n", option1 ):
    end if:
    if assigned( option2 ) then
        printf( "option 2 is given and is %a\n", option2 ):
    end if:
end proc:

Here is a screenshot of the output of the above procedure for different inputs.

enter image description here

陌生 2025-02-07 09:03:58

大多数绘图命令都使用Maple更现代的管理过程选项。

特别是绘制命令的大多数选项都提供为SO-CALLED 关键字选项。这会自动提供(此类选项)的位置无关紧要的功能。

例如,

f:=proc(v, 
        {ord::{integer,NoUserValue}:=':-NoUserValue'},
        {special::{integer,list,NoUserValue}:=':-NoUserValue'});

  print(':-ord'=ord, ':-special'=special);

end proc:

f(x);

    ord = NoUserValue, special = NoUserValue

f(x,ord=3);

         ord = 3, special = NoUserValue

f(x,special=5);

         ord = NoUserValue, special = 5

f(x,special=5,ord=3);

               ord = 3, special = 5

f(x,ord=3,special=5);

               ord = 3, special = 5

正如您注意到的那样,[从逻辑上]您无法使用多个 *位置/有序的“如果两者都有相同的类型,并且丢失了一些类型

。 href =“ https://www.maplesoft.com/support/help/help/maple/view.aspx?path = argument_processing#positional” rel =“ nofollow noreferrer”> positional promotal参数,尽管自然会失去其灵活性例如,任意

restart;
f2:=proc(v, 
        ord::{integer,NoUserValue}:=':-NoUserValue',
        {special::{integer,list,NoUserValue}:=':-NoUserValue'});

  print(':-ord'=ord, ':-special'=special);

end proc:
f2(x);
f2(x,3);
f2(x,special=5);
f2(x,special=5,3);
f2(x,3,special=5);

restart;
f3:=proc(v, 
         special::{integer,list,NoUserValue}:=':-NoUserValue',
         {ord::{integer,NoUserValue}:=':-NoUserValue'});

  print(':-ord'=ord, ':-special'=special);

end proc:
f3(x);
f3(x,5);
f3(x,ord=3);
f3(x,ord=3,5);
f3(x,5,ord=3);

位置

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,

f:=proc(v, 
        {ord::{integer,NoUserValue}:=':-NoUserValue'},
        {special::{integer,list,NoUserValue}:=':-NoUserValue'});

  print(':-ord'=ord, ':-special'=special);

end proc:

f(x);

    ord = NoUserValue, special = NoUserValue

f(x,ord=3);

         ord = 3, special = NoUserValue

f(x,special=5);

         ord = NoUserValue, special = 5

f(x,special=5,ord=3);

               ord = 3, special = 5

f(x,ord=3,special=5);

               ord = 3, special = 5

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,

restart;
f2:=proc(v, 
        ord::{integer,NoUserValue}:=':-NoUserValue',
        {special::{integer,list,NoUserValue}:=':-NoUserValue'});

  print(':-ord'=ord, ':-special'=special);

end proc:
f2(x);
f2(x,3);
f2(x,special=5);
f2(x,special=5,3);
f2(x,3,special=5);

restart;
f3:=proc(v, 
         special::{integer,list,NoUserValue}:=':-NoUserValue',
         {ord::{integer,NoUserValue}:=':-NoUserValue'});

  print(':-ord'=ord, ':-special'=special);

end proc:
f3(x);
f3(x,5);
f3(x,ord=3);
f3(x,ord=3,5);
f3(x,5,ord=3);

There are too many variants to show them all here, sensibly.

You don't have to use the name "NoUserValue" as the default values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文