concatenate两个Sets
字符串是否存在惯用方法或内置方法?
这就是我想要的:
> my Set $left = <start_ begin_>.Set
set(begin_ start_)
> my Set $right = <end finish>.Set
set(end finish)
> my Set $left_right = ($left.keys X~ $right.keys).Set
set(begin_end begin_finish start_end start_finish)
或者,如果有两个以上:
> my Set $middle = <center_ base_>.Set
> my Set $all = ([X~] $left.keys, $middle.keys, $right.keys).Set
set(begin_base_end begin_base_finish begin_center_end begin_center_finish start_base_end start_base_finish start_center_end start_center_finish)
答案 0 :(得分:9)
您可以使用reduce
函数从任意数量的集合转到单个集合,其中包含所有内容:
my Set @sets = set(<start_ begin_>),
set(<center_ base_>),
set(<end finish>);
my $result = @sets.reduce({ set $^a.keys X~ $^b.keys });
say $result.perl
# =>
Set.new("start_base_end","begin_center_finish","start_center_finish",
"start_center_end","start_base_finish","begin_base_end",
"begin_center_end","begin_base_finish")
这对我来说似乎很干净。