因此,在另一个问题的评论中,我只是看到这个例子来计算字符串中 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...
发布评论
评论(2)
您开始使用的“完全扩展”版本实际上是向后的,它应该是:
但是,Scala 语言并不认为
==
是特殊的,它只是任何
。因此,要进一步扩展:但是请坚持...
TraversableOnce
中的 >count 方法需要一个带有签名(A) ⇒ Boolean
的函数作为参数。Any.==()
的签名恰好是Any ⇒ Boolean
,因此无需将其包装在另一个匿名函数中即可完美契合。因此,我们可以直接说:或者,省略点:
为什么我们不也省略
==
运算符呢?好吧,我想你可以将countEqual
定义为count(a => a ==)
(或者,更简洁地,count(_ ==)
code>) 如果你真的想...但是你也可以定义countLowerCase
或其他什么。这里的要点是==
并不特殊。The "fully expanded" version you started out with is actually backwards, it should be:
However,
==
isn't considered special by the Scala language, it's just another method ofAny
. So to expand even further:But hang on... The
count
method fromTraversableOnce
is expecting a function with the signature(A) ⇒ Boolean
as an argument. The signature ofAny.==()
happens to beAny ⇒ Boolean
, so that fits neatly without needing to be wrapped in another anonymous function. So instead we can just say:or, omitting the dots:
Why don't we omit the
==
operator too? Well, I guess you could definecountEqual
ascount(a => a ==)
(or, more succinctly,count(_ ==)
) if you really wanted to... But you could just as well definecountLowerCase
or whatever. The main point here is that==
isn't special.首先,让我们考虑一下
count
的含义:这里的
count
是一个采用函数Char =>; 的方法。布尔值
,所以这就是它所期望的'l'==
。现在,让我们考虑一下:这是
Char
上的==
方法。该方法已重载,但此签名看起来很有趣:这意味着您有一个
object.method
,其类型为(x: Char): Boolean
正在传递,其中(x: 字符) =>需要布尔值
。真的还有什么可说的吗?First, let's consider what
count
means below:count
, here, is a method that takes a functionChar => Boolean
, so that's what it expects'l'==
to be. Now, let's consider it:That's the
==
method onChar
. That method is overloaded, but this signature looks interesting: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?