Scala 中的参数省略/推断

发布于 2024-12-12 06:11:55 字数 723 浏览 0 评论 0 原文

因此,在另一个问题的评论中,我只是看到这个例子来计算字符串中 L 的数量:

"hello".count('l'==)

而且足够疯狂......它有效。

从完全扩展的版本开始,我们有:

"hello".count(ch => ch == 'l')

然后可以简化为:

"hello".count(_ == 'l')

但然后我们可以这样做:

"hello".count('l'==)

我会像...... ??? ...

大概 Scala 推断我们一定是想在比较末尾添加一个 _ 。 IMO,这就是事情开始变得超级奇怪的地方;这对我来说似乎太过分了。谁能解释允许这种语法背后的想法,或者进一步阐明这一点?

如果您认为这很酷,那么我们如何证明不假设人们也可能需要 == 运算符,以便可以省略它?然后我们可以:

"hello".count('l')

我想我正在回想起在 Perl 中有 10^99999 种可能的做事方式的噩梦......

So in a comment from another question, I just saw this example for counting the number of L's in a string:

"hello".count('l'==)

And crazy enough..it works.

Starting with the fully expanded version, we have:

"hello".count(ch => ch == 'l')

Which can then be reduced to:

"hello".count(_ == 'l')

But then we can do:

"hello".count('l'==)

And I'm going like ... ??? ...

Presumably Scala is inferring that we must have meant to add an _ at the end of the comparison. IMO, this is where things start getting super weird; this seems overboard to me. Can anyone explain the thinking behind allowing this syntax, or shed further light on this?

If you think this is cool, then how do we justify not assuming that people may want the == operator too, so that could be omitted? Then we could have:

"hello".count('l')

I think I'm having flashbacks to the nightmare of having 10^99999 possible ways of doing things in perl...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-12-19 06:11:55

您开始使用的“完全扩展”版本实际上是向后的,它应该是:

"hello".count(ch => 'l' == ch)

但是,Scala 语言并不认为 == 是特殊的,它只是 任何。因此,要进一步扩展:

"hello".count(ch => 'l'.==(ch))

但是请坚持... TraversableOnce 中的 >count 方法需要一个带有签名 (A) ⇒ Boolean 的函数作为参数。 Any.==() 的签名恰好是 Any ⇒ Boolean,因此无需将其包装在另一个匿名函数中即可完美契合。因此,我们可以直接说:

"hello".count('l'.==)

或者,省略点:

"hello" count('l' ==)

为什么我们不也省略 == 运算符呢?好吧,我想你可以将 countEqual 定义为 count(a => a ==) (或者,更简洁地,count(_ ==) code>) 如果你真的想...但是你也可以定义 countLowerCase 或其他什么。这里的要点是 == 并不特殊。

The "fully expanded" version you started out with is actually backwards, it should be:

"hello".count(ch => 'l' == ch)

However, == isn't considered special by the Scala language, it's just another method of Any. So to expand even further:

"hello".count(ch => 'l'.==(ch))

But hang on... The count method from TraversableOnce is expecting a function with the signature (A) ⇒ Boolean as an argument. The signature of Any.==() happens to be Any ⇒ Boolean, so that fits neatly without needing to be wrapped in another anonymous function. So instead we can just say:

"hello".count('l'.==)

or, omitting the dots:

"hello" count('l' ==)

Why don't we omit the == operator too? Well, I guess you could define countEqual as count(a => a ==) (or, more succinctly, count(_ ==)) if you really wanted to... But you could just as well define countLowerCase or whatever. The main point here is that == isn't special.

神爱温柔 2024-12-19 06:11:55

首先,让我们考虑一下 count 的含义:

"hello".count('l'==)

这里的 count 是一个采用函数 Char =>; 的方法。布尔值,所以这就是它所期望的'l'==。现在,让我们考虑一下:

'l' ==

这是 Char 上的 == 方法。该方法已重载,但此签名看起来很有趣:

def == (x: Char): Boolean

这意味着您有一个 object.method ,其类型为 (x: Char): Boolean 正在传递,其中 (x: 字符) =>需要布尔值。真的还有什么可说的吗?

First, let's consider what count means below:

"hello".count('l'==)

count, here, is a method that takes a function Char => Boolean, so that's what it expects 'l'== to be. Now, let's consider it:

'l' ==

That's the == method on Char. That method is overloaded, but this signature looks interesting:

def == (x: Char): Boolean

That means you have an object.method whose type is (x: Char): Boolean being passed where a (x: Char) => Boolean is expected. Is there really anything else to say?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文