如何简化scala的函数文字?

我是scala的新手,并试图编写一个函数文字来检查给定的整数是否为奇数。 我的第一次尝试是:
val isOdd = (x:Int) => (x & 1) == 1
它工作得很好,而且,由于参数x只在这个函数文字中出现一次,我很想用“_”表示法进一步简化它,如下所示:
val isOdd = ((_:Int) & 1 ) == 1
但这次编译器抱怨: 警告:使用`=='比较新对象将始终产生错误 val isOdd =((_:Int)&1)== 1 这个警告意味着什么?为什么编译器将
((_ :Int) & 1)
识别为新对象而不是导致值的按位运算?有没有办法用“_”表示法编写这个函数文字?     
已邀请:
问题基本上是Scala需要区分它们
val isOdd = ((_:Int) & 1 ) == 1
你希望等号右边的所有东西都是lambda,而且
val result = collection.map( _ + 1 )
你只希望括号内的东西是lambda Scala已经决定,当你使用下划线创建一个lambda时,它会选择最里面的一组括号作为lambda的边界。有一个例外:
(_:Int)
不算作最里面的括号,因为它的目的只是用
_
占位符对它们进行类型声明分组。 因此:
val isOdd = ((_:Int) & 1 ) == 1
            ^^^^^^^^^^^^^^
            this is the lambda

val result = collection.map( _ + 1 )
                            ^^^^^^^
                            this is the lambda

val result = collection.map(( _ + 1) / 2)
                            ^^^^^^^^
                            this is the lambda
                            and the compiler can't infer the type of the _

val result = somemap.map(( _ + 1) / 2 * _)
                         ^^^^^^^^
                         this is an inner lambda with one parameter
                         and the compiler can't infer the type of the _
                         ^^^^^^^^^^^^^^^^^
                         this is an outer lambda with one parameter
最后一种情况可以让你做的事情
_.map(_ + 1)
并将其翻译成
x => x.map( y=> y + 1 )
    
只是轻微作弊:
val isOdd = (_: Int) % 2 == 1
:-)     
你去:
val isOdd = ((_: Int) & 1) andThen (1 ==)
    
Scala正在做的是: 它看到
((_:Int) & 1 )
并创建一个类型为
(Int) => Int
的对象,即一个函数。 然后应用比较运算符
==
将此函数与值1进行比较 函数不等于值1.因此结果为
false
,因此您的代码相当于:
val isOdd = false
你可以做的是创建另一个匿名函数,它可以完成计算的“17”部分。这很难看:
val isOdd = ((_: Int) & 1)(_: Int) == 1
这相当于更详细(也许更容易理解):
val isOdd = (x: Int) => 1 == ((_: Int) & 1)(x)
    
一种不同的方法
val isOdd = (_:Int).&(1) == 1
    

要回复问题请先登录注册