组合

作者:Eric Hodges

规范

来自 http://www.perlmonks.org/?node_id=731808

给定一个 URL 前缀列表和一个产品 ID 列表,创建一个由每个 URL 前缀与每个产品 ID 连接而成的列表。

源代码:combinations-731808.pl

use v6;

my @urls = ('http://www.something.com/blah.aspx?code=',
            'http://www.somethingelse.com/stuff.aspx?thing=');

my @ids = <375035304 564564774 346464646>;

# 1. Cross then map
# We use the cross operator X to make every combination of pairs from @urls
# and @ids. We then use map to stringify each pair. $^a is a "placeholder
# argument" - in this case, it refers to the only argument to the block.
my @combined = (@urls X @ids).map: { ~$^a };

.say for @combined;


# 2. cross hyperoperator
# We use the cross hyperoperator X~
# This combines each element from list1 with each element from list2 using ~
#    You can use any infix operator.
#    Try (1,2,3) X* (1,2,3) to generate a multiplication table.

.say for @urls X~ @ids;