I'm new to scala and trying to write a function literal that check whether a given integer is odd or not. my first attempt is:
val isOdd = (x:Int) => (x & 1) == 1
it works great, and, since the parameter x only appears once within this function literal, I'm tempted to use the "_" notation to simplify it further, like this:
val isOdd = ((_:Int) & 1 ) == 1
however this time the compiler complains :
warning: comparing a fresh object using `==' will always yield false val isOdd = ((_:Int) & 1 ) == 1
what does this warning mean? why does the compiler recognize ((_ :Int) & 1)
as fresh object rather than a bitwise operation that results in a value? is there any way to write this function literal using the "_" notation?
val odd = ! even (_:Int)
– Selfdetermination