我有一个包含操作的枚举列表,成员为 None 和 All。如何创建过滤掉
None
和 All
的枚举序列?
文件中的测试程序
filter-enum.raku
:
#!/usr/bin/env raku
use v6.d;
enum A <All None one two three four>;
my SetHash of A $actions;
$actions .= new( A.enums.grep( *.key.match(/ All | None /).not) );
say $actions.raku;
输出
$ raku -v
Welcome to Rakudo™ v2024.02-93-gc49e05d84.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2024.02.
$ raku filter-enum.raku
Type check failed in binding; expected A but got Pair (:one(2))
in block <unit> at filter-enum.raku line 6
一个解决方法,或者可能是更好的方法,是有两个枚举:
enum actions <one two three four>;
enum full-set (actions.keys, <None All>).flat;
编辑:此解决方法不起作用!所以下面的答案是最好的。 但是可以通过过滤来创建枚举序列吗?
你可以使用
enum A <All None one two three four>;
dd A::.values.grep: none(All, None); #or
dd (All,*.succ...four).grep: none(All, None)