否定func< int,bool>返回对面的布尔

发布于 2025-02-10 14:40:30 字数 500 浏览 1 评论 0原文

我有一个lambda返回一个函数,该函数否定了给定功能的结果,但是我无法使它起作用。

示例:

not(x => x % 2 == 0)(1) --> true  
not(x => x % 2 == 0)(2) --> false

我当前的代码:

Func<Func<int,bool>, Func<int, bool>> not = y => !y;

Func<int, bool> equalTo0 = x => x == 0;

var test3 = not(equalTo0)(5);

例如,这将返回false,但是使用lambda,它应该返回true

有人对如何改变功能的结果有任何想法/想法吗?

I have a lambda that returns a function that negates the result for a given function, but I just cannot get it to work.

Example:

not(x => x % 2 == 0)(1) --> true  
not(x => x % 2 == 0)(2) --> false

My current code:

Func<Func<int,bool>, Func<int, bool>> not = y => !y;

Func<int, bool> equalTo0 = x => x == 0;

var test3 = not(equalTo0)(5);

This would return false for example, but with the not lambda it should return true.

Has anyone have any idea/ideas on how to change the outcome of the function?

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

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

发布评论

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

评论(1

逆蝶 2025-02-17 14:40:30

您正在尝试编写一个功能并返回另一个功能的lambda。返回的功能可以写入另一个lambda:

// parentheses not necessary, just for clarity 
Func<Func<int,bool>, Func<int, bool>> not = f => (x => !f(x));

f是输入到不是的函数,然后您返回函数x =&gt; !f(x)

这也可以写成通用方法:

Func<T, bool> Not<T>(Func<T, bool> f) => x => !f(x);

You are trying to write a lambda that takes a function and returns another function. That returned function can be written as another lambda:

// parentheses not necessary, just for clarity 
Func<Func<int,bool>, Func<int, bool>> not = f => (x => !f(x));

f is the function that is inputted to not, you then return the function x => !f(x).

This can also be written as a generic method:

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