在 Perl 6 中使用字符串值从数组中创建连接 [英] Making a junction out of an array with string values in Perl 6

查看:41
本文介绍了在 Perl 6 中使用字符串值从数组中创建连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的.应该很简单,但我不知道如何正确执行.

Here's what I'm trying to do. It should be very simple, but I can't figure out how to do it correctly.

> my @search_keys = <bb cc dd>
[bb cc dd]
> my $search_junc = @search_keys.join('|')
bb|cc|dd
> "bb" eq $search_junc
False

推荐答案

my @search_keys = <bb cc dd>;
say "bb" eq any(@search_keys);     # any(True, False, False)
say so "bb" eq any(@search_keys);  # True

| 语法只是调用 any() 函数的糖.就像 &all() 函数的语法糖一样.它们都返回 Junctions,你可以用例如折叠它so 函数.当然,如果你打算在条件中使用它,你不需要自己折叠它,条件的Bool化会为你做到这一点:

The | syntax is merely sugar for calling the any() function. Just like & is syntactic sugar for the all() function. They both return Junctions, which you can collapse with e.g. the so function. Of course, if you're going to use it in a conditional, you don't need to collapse it yourself, the Boolification of the condition will do that for you:

say "found" if "bb" eq any(@search_keys);

另见:https://docs.raku.org/type/Junction

编辑(两年多后):

如果你对列表(<bb cc dd>)中给定对象("bb")的简单等价感兴趣,你也可以使用为此设置运算符:

If you are interested in the simple equivalence of the given object ("bb") in the list (<bb cc dd>), you can also use set operators for that:

say "found" if "bb" (elem) @search_keys;  # found

从技术上讲,这将对给定字符串的 .WHICH 进行比较.更重要的是,一旦找到匹配项,这个习语就会走捷径.因此,由于在您的示例中 "bb" 是数组中的第一个元素,因此它只会检查该元素.它不需要构建任何额外的对象,比如 Junction(在第一个解决方案中)或 Set(在第二个解决方案中).

Technically, this will do the comparison on the .WHICH of the given strings. More importantly, this idiom will short-cut as soon as a match is found. So since in your example "bb" is the first element in the array, it will only check that element. And it won't need to build any additional objects, like a Junction (in the first solution) or a Set (in the second solution).

这篇关于在 Perl 6 中使用字符串值从数组中创建连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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