Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two Sets of strings in Perl 6?

Tags:

raku

Is there an idiomatic way or a built-in method to concatenate two Sets of strings?

Here's what I want:

> 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)

Or, if there are more than two of them:

> 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)
like image 953
Eugene Barsky Avatar asked Nov 19 '17 20:11

Eugene Barsky


People also ask

How to concatenate strings Perl?

Perl strings are concatenated with a Dot(.) symbol. The Dot(.) sign is used instead of (+) sign in Perl.

How to concatenate 2 variables in Perl?

In general, the string concatenation in Perl is very simple by using the string operator such as dot operator (.) To perform the concatenation of two operands which are declared as variables which means joining the two variables containing string to one single string using this concatenation string dot operator (.)


1 Answers

You can use the reduce function to go from an arbitrary number of sets to a single set with everything concatenated in it:

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")

That seems clean to me.

like image 177
timotimo Avatar answered Oct 17 '22 12:10

timotimo