Making a junction out of an array with string values in Perl 6

你说的曾经没有我的故事 提交于 2019-12-12 13:07:37

问题


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

回答1:


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

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);

See also: https://docs.raku.org/type/Junction

EDIT (more than 2 years later):

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

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).



来源:https://stackoverflow.com/questions/46927870/making-a-junction-out-of-an-array-with-string-values-in-perl-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!