什么时候调用迭代器方法? [英] When does for call the iterator method?

查看:124
本文介绍了什么时候调用迭代器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是在使块可迭代的过程中与其他问题相同,但似乎揭露mixins的其他问题(或我对语法的不同误解). Iterable 的作用是使数据结构有效地迭代,也就是说,您可以创建通过在其前面加上for进行循环.

This question is in the same ballpark as this other on making blocks iterable, but seems to reveal a different problem with mixins (or a different misunderstanding of the syntax on my part). What Iterable does is to make a data structure effectively iterable, that is, you can create loops by preceding it with for.

Iterable用作可通过for构造和相关的迭代构造(例如超级运算符)进行迭代的对象的API.

Iterable serves as an API for objects that can be iterated with the for construct and related iteration constructs, like hyper operators.

因此,让我们尝试将其付诸实践:

So let's try to put this to practice:

 my &logger = -> $event  {
    state %store;
    if ( $event ) {
        %store{ DateTime.new( now ) } = $event;
    } else {
        %store;
    }
}

role Forable does Iterable {
    method iterator(&self:) {
        self( Nil );
    }
}

logger( "One" );
logger( "Two" );

&logger does Forable;

.say for &logger;

这根本行不通; say作为简单项目应用于&logger.但是,如果我们将最后一句更改为:

This simply does not work; say is applied to &logger as a simple item. However, it works if we change that last sentence to:

.say for &logger.iterator;

我猜这表明该角色实际上在起作用,并且已经混合使用.由于&logger的类型是Block+{Forable},所以如果不直接混合Iterable,则可能不起作用.实际上,从Forable声明中删除does Iterable不会以任何方式影响它.让我们尝试一下:

Which I guess that indicates that the role is actually working, and mixed in. Since the type for &logger is Block+{Forable}, maybe it does not work if Iterable is not mixed in directly. In fact, erasing does Iterable from the Forable declaration does not affect it in any way. Let's try then this:

&logger does (Iterable,Forable);

现在&logger的类型显示为Block+{Iterable,Forable},但仍然没有喜悦. iterator必须直接调用.有什么解决办法的想法吗?

Now the type of &logger is revealed as Block+{Iterable,Forable}, but still no joy. iterator has to be called directly. Any idea on how to solve this?

推荐答案

for什么时候调用迭代器方法?

When does for call the iterator method?

据我了解,如果迭代功能的(单个)参数是Scalar容器,则它使用容器中的值,并且不调用.iterator.否则,它将在其上调用.iterator,如果它是表达式或例程调用,则首先对其求值.

As I understand, if the (single) argument to an iterating feature is a Scalar container, then it uses the value in the container and does not call .iterator. Otherwise, it calls .iterator on it, evaluating it first if it's an expression or routine call.

&logger does Forable;
.say for &logger;

这根本行不通; say作为简单项目应用于&logger.

This simply does not work; say is applied to &logger as a simple item.

&是一个名词标记(sigil),用于标记Callable,它本质上是一个单一的东西,一个代码块.

The & is a noun marker (sigil) marking a Callable that is inherently a single thing, a single block of code.

更具体地说,&logger绑定到类型为CallableScalar容器,如果您编写my Callable $logger,则与$logger(带有$标记)完全相同:

More specifically, &logger is bound to a Scalar container whose type is Callable, exactly the same as $logger (with a $ sigil) would be if you wrote my Callable $logger:

say .WHAT, .VAR, .VAR.WHAT, .VAR.of
for my &logger, my Callable $logger

显示:

(Callable)Callable(Scalar)(Callable)
(Callable)Callable(Scalar)(Callable)

由于&logger的类型为Block+{Forable}

这实际上是包含在 绑定到&loggerScalar容器中的Callable的类型.

That's actually the type of the Callable that's contained in the Scalar container that's bound to &logger.

这不是容器本身的类型,它是Scalar,如上所示.

当给定变量形式的单个参数时,像for这样的迭代功能将查看变量,而不是变量中包含的值,以查看其是否为Iterable. Scalar不是Iterable.

When given a single argument in the form of a variable, iterating features like for look at the variable, not the value contained in the variable, to see if it's Iterable. A Scalar is not Iterable.

关于如何解决此问题的任何想法?

Any idea on how to solve this?

有关一种方法,请参阅lizmat的答案.

See lizmat's answer for one approach.

这篇关于什么时候调用迭代器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆