当应用于潜艇时,最初和返回是什么?
我在某个地方找到了操作员 and then
,当我在我发现了这一点:
#!/bin/env raku
sub load-data
{
rand > .5 or return; # simulated load data failure; return Nil
(rand > .3 ?? 'error' !! 'good data') xx 10 # our loaded data
}
load-data.first: /good/ andthen say "$_ is good";
# OUTPUT: «(good data is good)»
load-data() andthen .return; # return loaded data, if it's defined
die "Failed to load data!!";
那么, .first
和。返回
在应用于Subs时工作?
我以前从未见过这样的事情。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是。您没有调用
.first
或.trunt
在sub
对象上。当Raku中的子例程没有参数出现时,就称为。因此,简单地将
load-data
用自己的呼叫load-data
,就好像您要放置括号一样。 (注意:如果您想要获得子例程对象本身,则使用&
sigil,所以& load-data
是子例程对象)因此,这与更熟悉的类似python的语法中的类似内容相当。
首先,
在列表上调用。
load-data
被调用。它返回nil
或数据列表。现在您已将其称为
load-data.first:/good/
,因此它将返回正则表达式/good/good/
匹配的列表的第一个元素,或nil
否则。andthen
就像&&&
,除了&&
检查真实性(通过布尔上下文)检查,和Thenen
通过多方法定义
。nil
和故障之类的对象是未定义的,而大多数“普通”对象(例如数字和字符串)是定义的。特别是,仍然定义了常规“虚假”(例如零或空字符串)的事物。像&&
,和thenen
短路,因此,如果左侧不确定,则右侧永远不会评估。最后,还有另一个怪癖使这条线可以工作。
和Thens
旨在处理此确切的用例:做事情1,如果那件事成功,请执行操作2。通常,事物2取决于事物1的结果。 /code>在评估右侧时,将左侧的结果分配给特殊变量$ _
。那是一口气的,所以让我们把它写出来。X和Then Y
执行以下操作。X
。X
,请停止。我们完成了。返回x
。$ _
设置为x
的结果。y
(使用$ _
设置)并将其返回。因此,将所有内容全部放入...
加载数据,找到与正则匹配的加载数据的第一个元素,如果存在,则将其打印出来。
现在让我们看看您的第二个示例。您需要了解另一个有趣的小糖糖。通常,语句开头的点带有隐含的
$ _
。因此,.say
等于$_。Say
,它等效于say($ _:)
,该方法在方法分辨率为<<<<<等效。代码>说($ _)。你相信与否,
return 是子例程。严重地。这不是关键字;这是一个子。返回
&amp; return.s repl中的
。它会说(sub)
。load-data
被调用,如果返回定义的任何内容,则定义的值将分配给$ _
。然后。返回
发生,这等效于$_。返回
哪个(最终)变为返回($ _)
。如果定义了值,则它将从函数返回,第二行永远不会运行。如果没有,我们die
带有适当的错误消息。请注意,在这种情况下,实际上需要括号。如果我们编写
,则Raku试图将
和Then.Return
作为参数load> load-data
,并抱怨没有subroutine称为and
and < /代码>存在。它认为我们的意思是
总而言之,Raku是一个大包,里面装满了语法糖,该糖包裹在地狱第六个地狱中的解析器中,这是一个敏感的,其结果是一种真正美丽的语言。
It doesn't. You're not calling
.first
or.return
on aSub
object.When a subroutine in Raku appears with no arguments, it's called. So simply writing
load-data
on its own callsload-data
, as though you'd put parentheses. (Note: If you want to get the subroutine object itself, you use an&
sigil, so&load-data
is the subroutine object) So thisis equivalent to something like this in a more familiar Python-like syntax.
First,
load-data
is called. It returns eitherNil
or a list of data. Nowfirst
is called on the list.You've called it as
load-data.first: /good/
, so it will return the first element of the list for which the regular expression/good/
matches, orNil
otherwise.andthen
is like&&
, except that while&&
checks for truthiness (via a Boolean context),andthen
checks for definedness via the multi methoddefined
. Objects likeNil
and failures are undefined, while most "ordinary" objects like numbers and strings are defined. In particular, things that are conventionally "falsy" like zero or the empty string are still defined. Like&&
,andthen
short-circuits, so if the left-hand side is undefined, then the right-hand side never evaluates.Finally, there's one other quirk that enables this line to work.
andthen
is designed to work with this exact use case: Do thing 1 and, if that thing succeeds, do thing 2. Frequently, thing 2 depends on the result of thing 1. Soandthen
assigns the result of the left-hand side to the special variable$_
during evaluation of the right-hand side. That's a mouthful, so let's write it out.x andthen y
does the following.x
.x
is not defined, then stop. We're done. Returnx
.$_
to the result ofx
.y
(with$_
set) and return it.So, taking it all in...
Load the data, find the first element of the loaded data which matches a regex, and if one exists, then print it out.
Now let's take a look at your second example. There's another fun little piece of syntax sugar you need to know about for this one. In general, a dot at the beginning of a statement carries an implied
$_
. So.say
is equivalent to$_.say
, which is equivalent tosay($_:)
, which evaluates after method resolution tosay($_)
.And believe it or not,
return
is a subroutine. Seriously. It's not a keyword; it's a sub. Go evaluate&return.WHAT
in your REPL right now. It'll say(Sub)
.load-data
gets called, and if it returns anything defined, that defined value gets assigned to$_
. Then.return
happens, which is equivalent to$_.return
which (eventually) becomesreturn($_)
. If the value was defined, it gets returned from the function and the second line never runs. If not, wedie
with an appropriate error message.Note that the parentheses are actually required in this case. If we write
Then Raku tries to interpret
andthen .return
as an argument toload-data
and complains that no subroutine calledandthen
exists. It thinks we meantIn summary, Raku is a big bag full of syntax sugar wrapped in a parser from the sixth circle of Hell that's whitespace- and context- sensitive, and the result is a genuinely beautiful language.