如何解决“此Seq的迭代器已在使用中”?

问题描述

此代码:

sub MAIN(Int $N = 128)
{
    my @pascal = ((1,),{ (0,|@^a) Z+ (|@^a,0) } ... *);

    my @top = @pascal[^$N];
    say "Percentage of odd numbers in the first $N rows: ",(100 × @top».grep(* % 2).sum / @top.sum).fmt('%.12f%%');
}

给我一​​个错误:

The iterator of this Seq is already in use/consumed by another Seq
(you might solve this by adding .cache on usages of the Seq,or
by assigning the Seq into an array)
  in sub MAIN at ./pascal1 line 8
  in block <unit> at ./pascal1 line 1

任何想法如何解决?我曾尝试在多个地方添加.cache,但是没有运气。

解决方法

作为序列一部分的块正在创建Seq。您应该能够像这样缓存它:

{ ( (0,|@^a) Z+ (|@^a,0) ).cache }

,

func signInGmail() { let redirectURI = URL(string: gmail_kRedirectURI)! let appDelegate = UIApplication.shared.delegate as! AppDelegate let config = GTMAppAuthFetcherAuthorization.configurationForGoogle() let scopes = [OIDScopeOpenID,OIDScopeProfile,OIDScopeEmail,kGTLRAuthScopeGmailReadonly] let request = OIDAuthorizationRequest.init(configuration: config,clientId: gmail_kClientID,scopes: scopes,redirectURL: redirectURI,responseType: OIDResponseTypeCode,additionalParameters: nil) appDelegate.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request,presenting: self,callback: { (authState,error) in //handleError if let authState = authState { let authorizer = GTMAppAuthFetcherAuthorization(authState: authState) self.pickGmailLabels(authorizer: authorizer) } }) } 返回一个Z

Seq产生它的下一个值时,它会将前一个值丢弃。

因此,您通常只能从Seq获取一次值。

您拥有的区块Seq通过查看它生成的上一个{…}来工作。所以有一个问题。您要么查看那些Seq中的内容,要么Seq运算符查看...中的内容


问题是,您可能不希望Seq的结果为Z,而是希望它为Seq

毕竟,您需要使用List ...List序列生成器开始。

(1,)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...