否定func< int,bool>返回对面的布尔
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试编写一个功能并返回另一个功能的lambda。返回的功能可以写入另一个lambda:
f
是输入到不是
的函数,然后您返回函数x =&gt; !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:
f
is the function that is inputted tonot
, you then return the functionx => !f(x)
.This can also be written as a generic method: