在 Perl 6 中过滤匹配两个正则表达式的元素 [英] Filtering elements matching two regexes in Perl 6

查看:31
本文介绍了在 Perl 6 中过滤匹配两个正则表达式的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里发生了一些我不太明白的事情.

Something is going on here that I don't quite understand.

> my @arr = <ac bc abc>
> @arr.grep: (( * ~~ /a/ ) && ( * ~~ /b/ ))
(bc abc)

但是

> @arr.grep(* ~~ /a/).grep(* ~~ /b/)
(abc)

什么原因?

推荐答案

您想出了完美的解决方案.

You've come up with perfectly cromulent solutions.

另一个是:

my @arr = <ac bc abc>
@arr.grep: { $_ ~~ /a/ && $_ ~~ /b/ }
(abc)

这个答案的其余部分只是解释了这个问题.这个问题中的问题是 WhateverStar && 任何明星.

The rest of this answer just explains the problem. The problem in this question is a more complicated version of the problem covered at WhateverStar && WhateverStar.

如果它们是代码,逻辑操作不会执行它们的参数.

The logical ops don't execute their arguments if they're code.

所以 { $_ ~~/a/} &&{ $_ ~~/b/} 返回 { $_ ~~/b/}.

* ~~/a/&&* ~~/b/ 返回 * ~~/b/.

同时,grep 确实执行它的匹配器,如果它是代码或者它是一个正则表达式,所以这些都是一样的:

At the same time, grep does execute its matcher if it's code or if it's a regex, so these are all the same:

foo.grep: { $_ ~~ /.../ }
foo.grep:    * ~~ /.../;
foo.grep:         /.../;

Junctions

的神奇之处

您的 Junction 解决方案看起来很自然.如果有人可以解释我在以下内容中缺少的内容,我会很高兴.虽然我期望它可以工作,但我想弄清楚它实际上是如何工作的,但我认为它类似于(但不完全是):

The magic of Junctions

Your Junction solution seems natural. I'd love it if someone could explain what I'm missing in the following. While I expected it to work, it's bending my head to figure out how it actually works but I think it's something like (but not quite):

  • foo &bar 成为 foo barJunction.

尝试使用 Junction 作为参数调用 grep.

An attempt is made to invoke grep with a Junction as an argument.

因为 Junction 在正常的 Any 值层次结构之外,大多数例程没有匹配的签名.grep 没有.

Because Junction is outside the normal Any value hierarchy, most routines don't have a matching signature. grep doesn't.

当你调用一个例程并且没有相应的签名时,初始调度失败,我们遇到了调度回退处理程序.这可能是一种方法.

When you invoke a routine and there is no corresponding signature then the initial dispatch fails and we hit a dispatch fallback handler. This may be the method one.

如果调度回退处理程序看到有 Junction 参数,那么它会提取 Junction 中的各个值并启动逻辑上并行的线程"(目前从不实际线程,但如果编译器认为这是一个好主意,则允许编译器使它们成为真正的线程)对应于这些值.因此,它对调用者的每个元素调用 grep 两次,并将结果输出回与传入的 Junction 类型相同的新 Junction.

If the dispatch fallback handler sees there are Junction arguments then it extracts the individual values in the Junction and fires up logically parallel "threads" (currently never actual threads, but the compiler is allowed to make them real threads if it thinks that is a good idea) corresponding to those values. Thus it invokes grep twice per element of the invocant and outputs the results back into a new Junction of the same type as the incoming Junction.

在布尔上下文中,Junction 会折叠为单个结果.

In boolean context, a Junction collapses to a single result.

(如果 Junction 在一个整体布尔上下文中,那么它可以(并行)基于结果短路.如果它是一个 anyany 结果出现在 True 中,然后它可以取消其余的 - 或者如果它实际上是按顺序执行操作,则首先不执行它们,就像目前的情况一样.如果是all 和任何结果出现在 False 它可以取消其余的.等等)

(If a Junction is in an overall boolean context then it can (parallel) short-circuits based on results. If it's an any and any result comes in True then it can cancel the rest -- or not do them in the first place if it's actually doing things sequentially, as is currently always the case. If it's an all and any result comes in False it can cancel the rest. Etc.)

这篇关于在 Perl 6 中过滤匹配两个正则表达式的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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