假设我们有以下组成角色Iterable
的类:
class Word-Char does Iterable {
has @.words;
method !pairize($item) {
return $item => $item.chars;
}
method iterator( Word-Char:D: ) {
@!words.map({self!pairize($_)}).rotor(1).iterator
}
}
我可以在对象构造过程中将对象分配给Positional
变量,然后遍历该变量:
my @words = Word-Char.new: words => <the sky is blue>;
.say for @words;
输出:
(the => 3)
(sky => 3)
(is => 2)
(blue => 4)
但是,如果要传递对象该怎么办?我如何确定它仍然是可迭代?:
my $w = Word-Char.new: words => <the sky is blue>;
sub f( $w ) {
.say for $w
}
f($w);
输出:
Word-Char.new(words => ["the", "sky", "is", "blue"])
目标:
通过使用Iterable
,Iterator
或两者,如果可能的话,我希望能够遍历实现这些角色的类的实例对象。现在我知道,通过在对象构造过程中将实例对象分配给Positional
变量,我可以获得类提供的 iterable 项,但这不是我想要的。相反,我想传递对象本身,并在我认为有需要的地方/地方对其进行遍历。
答案 0 :(得分:3)
好的,在这里不清楚您要实现什么,但是让我们尝试一下。
第二个示例中的主要问题是您已使用标量更改了Positional(带有w
)。只需再次使用@w
,您就可以设置
my @w = Word-Char.new: words => <the sky is blue>;
sub f( @w ) {
.say for @w
}
f(@w);
这将以完全相同的方式工作,因为@w仍然是Positional,因此是Iterable的。当您调用$w
时,标量仅返回其唯一的项目,即对象,即所打印的内容。如果要在该对象上使用标量符号并且对其进行迭代,则还需要使其成为Iterator
。
答案 1 :(得分:2)
在#perl6上,jnthn provided several approaches。他们中有些人的表现不如我预期。
我按照如下方式更新了课程 jjmerelo's suggestion:
class Word-Char does Iterable does Iterator {
has @.words;
has Int $!index = 0;
method !pairize($item) {
return $item => $item.chars;
}
method iterator() {self}
method pull-one( --> Mu ) {
if $!index < @!words.elems {
my $item = @!words[$!index];
$!index += 1;
return self!pairize($item);
}
else {
return IterationEnd;
}
}
}
# Binding to a Positional
my @w01 := Word-Char.new: words => <the sky is blue>;
这会产生以下错误:
Type check failed in binding; expected Positional but got Word-Char...
|
my $w = Word-Char.new: words => <the sky is blue>;
for |$w {
.say
}
=begin comment
Word-Char.new(words => ["the", "sky", "is", "blue"])
=end comment
|
对似乎保持其标量性质的对象没有影响,因此for
不会对其进行迭代。
my \w = Word-Char.new: words => <the sky is blue>;
for w {
.say
}
=begin comment
he => 3
sky => 3
is => 2
blue => 4
=end comment
到目前为止,这是我所期望的最干净的方法。
实际上,这是我的第一种方法,但是我发现它并不是太
p6y 。无论如何,为了使它起作用,我们需要更新我们的类并添加一个返回可迭代内容的方法。我选择的方法的名称是LOOP-OVER
,只是为了使其在其他所有方面脱颖而出。
class Word-Char {
has @.words;
method !pairize($item) {
return $item => $item.chars;
}
method LOOP-OVER {
gather for @!words -> $word {
take self!pairize($word)
}
}
}
my $w = Word-Char.new: words => <the sky is blue>;
for $w.LOOP-OVER {
.say
}
=begin comment
he => 3
sky => 3
is => 2
blue => 4
=end comment
但是,如果我们依赖于反复运行的几个类怎么办?我们如何确保它们实现相同的方法?最直接的方法
在这种情况下,将组成一个实现存根Iterationable
方法的角色(例如LOOP-OVER
)。
role Iterationable {
method LOOP-OVER { ... }
}
class Word-Char does Iterationable {
has @.words;
method !pairize($item) {
return $item => $item.chars;
}
method LOOP-OVER {
gather for @!words -> $word {
take self!pairize($word)
}
}
}
class Names does Iterationable {
has @.names;
method LOOP-OVER {
gather for @!names -> $name {
take $name.split(/\s+/)».tc.join(' ')
}
}
}
class NotIterable {
has @.items
}
my @objs =
Word-Char.new(words => <the sky is blue>),
Names.new(names => ['Jose arat', 'elva delorean', 'alphonse romer']),
NotIterable.new(items => [5, 'five', 'cinco', 'cinq'])
;
for @objs -> $obj {
if $obj.can('LOOP-OVER') {
put "» From {$obj.^name}: ";
for $obj.LOOP-OVER {
.say
}
}
else {
put "» From {$obj.^name}: Cannot iterate over it";
}
}
=begin comment
» From Word-Char:
the => 3
sky => 3
is => 2
blue => 4
» From Names:
Jose Arat
Elva Delorean
Alphonse Romer
» From NotIterable: Cannot iterate over it
=end comment
如jnthn所述,使用哪种方法(至少从工作中使用)几乎不会取决于眼前的问题。
答案 2 :(得分:2)
在处理具有迭代器角色的标量值时,完成您要尝试的操作的最简单方法是告诉perl6您的标量值是可迭代的。您可以通过使用[]
后缀来实现。您的示例如下所示:
my $w = Word-Char.new: words => <the sky is blue>;
.say for $w[]
另一件事......
您的迭代代码存在一个错误,即在返回IterationEnd
之前不会重置自身。快速修复如下所示:
class Word-Char does Iterable does Iterator {
has @.words;
has Int $!index = 0;
method !pairize($item) {
return $item => $item.chars;
}
method iterator() {self}
method pull-one( --> Mu ) {
if $!index < @!words.elems {
my $item = @!words[$!index];
$!index += 1;
return self!pairize($item);
}
else {
$!index = 0;
return IterationEnd;
}
}
}
但是,这意味着您必须将所有迭代逻辑(及其属性)保留在主类中。另一种方法是使用匿名类,而不是使用self
:
class Word-Char does Iterable {
has @.words;
method !pairize($item) {
return $item => $item.chars;
}
method iterator() {
my @words = @!words;
class :: does Iterator {
has $.index is rw = 0;
method pull-one {
return IterationEnd if $!index >= @words.elems;
@words[$!index++];
}
}.new;
}
method pull-one( --> Mu ) {
if $!index < @!words.elems {
my $item = @!words[$!index];
$!index += 1;
return self!pairize($item);
}
else {
return IterationEnd;
}
}
}
上述优点是可以使迭代逻辑更整洁并与对象的其余部分隔离。您也不必担心重置状态。
答案 3 :(得分:0)
另一个(有点混乱)的解决方案是:
class Word-Char does Iterator {
has @.words;
has Int $.index is rw = 0;
method pull-one() {
LEAVE { $!index++ }
return $!index < @!words.elems
?? (@!words[$!index] => @!words[$!index].chars)
!! IterationEnd;
}
}
my $w = Word-Char.new: words => <the sky is blue>;
my $seq = Seq.new($w).cache;
sub f( $w ) {
.say for $w[]
}
f($seq);
$w.index = 0;
f($seq);