如何在 Scala 中使用/引用布尔函数的否定? [英] How to use / refer to the negation of a boolean function in Scala?

查看:47
本文介绍了如何在 Scala 中使用/引用布尔函数的否定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Scala 中使用布尔函数的否定,例如:

I'm trying to use the negation of a boolean function in Scala, such as:

def someFunction(x: Set, p: Int => Boolean): Boolean = 
    someOtherFunction(x, !p)

但我收到错误:

 value unary_! is not a member of Int => Boolean

如何引用 p 的否定?

How can I refer to the negation of p?

推荐答案

p 的否定是一个将 p 应用于其参数并否定结果的函数.

The negation of p is a function that applies p to its argument and negates the result.

x => !p(x)

如果您希望能够编写 !pp &&q 你可以使用这个库,它使用各种逻辑运算符返回布尔值的函数.

If you want to be able to write !p or p && q you can use this library, which pimps functions that return a bool with various logical operators.

这篇关于如何在 Scala 中使用/引用布尔函数的否定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆