转换 Func到 Func
我想我的思想正在爆炸,试图找出 Funcs...如果这没有意义,我道歉,现在它对我来说是有意义的,但已经度过了漫长的一天....
1)假设给你一个 func接受 T 并输出一个字符串:
Func<T, string>
您能否将其转换为一个接受 T 并根据某种逻辑返回 bool 的函数(在本例中,如果返回的字符串为空 (String.IsNullOrWhiteSpace)?
Func<T, bool>
2)您可以执行以下操作吗同样的事情,如果你是给定 an
Expression<Func<T, string>>
并需要
Func<T, bool>
根据返回的字符串是否为空 (String.IsNullOrWhiteSpace) 将其转换为返回 true/false 的 a?
谢谢
I think my mind is exploding trying to figure out Funcs... If this makes no sense, I apologize, right now it make sense to me but its been a long day already ....
1) Assuming you are given a func which takes in T and outputs a string:
Func<T, string>
Can you transform that into a func that take in a T and returns a bool based on some logic (in this case if the returned string is empty (String.IsNullOrWhiteSpace)?
Func<T, bool>
2) Can you do the same thing if you are given an
Expression<Func<T, string>>
and need to convert it to a
Func<T, bool>
that returns true/false based on if the returned string is empty (String.IsNullOrWhiteSpace)?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于第一部分,您甚至可以创建一些“更高”阶函数:
使用 with
(我希望 C# 类型类型推断在这里起作用)
如果您将其定义为 Func 上的扩展,它会变得更加容易:
:
这是一个非常简单的测试 第二个:我认为你可以将表达式编译成“真正的”Func,然后使用上面的代码。 请参阅有关 Expression.Compile 的 MSDN 文档
PS:重命名了该函数以更好地匹配它的意图(它的功能组合)
for the first part you can even make some "higher"-order function:
use with
(I hope C# type type inference is working here)
If you define this as extension on Func it gets even easier:
here is a very simple test:
For the second one: I think you can compile the expression into a "real" Func and then use the above code. see MSDN Docs on Expression.Compile
PS: renamed the function to better match it's intend (it's function composition)
您不能将其定义为单独的委托吗:
Could you not define it as a separate delegate:
对于第一部分,该技术称为函数组合,即组合 2 个函数来创建一个新函数。
在您的情况下,您有一个函数
Func
和另一个类型为Func
的函数(如字符串空或 null),使用 function组合 您可以组合这两个函数来创建Func
类型的新函数大多数函数式编程语言都已在其标准库或语言本身中定义了这种函数组合。但是,如果该语言支持函数作为第一类值,那么为您的语言创建一个函数并不困难。
在 C# 中,您可以使用以下函数来组合函数:
For the first part the technique is known as function composition i.e you compose 2 functions to create a new function.
In your case you have a function
Func<T,String>
and another function (like string empty or null) which is of typeFunc<string,bool>
, using function composition you can compose these two functions to create a new function of typeFunc<T,Bool>
Most functional programming language have this composition of function already defined in their standard library or in the language itself. But it is no tough to create one for your language if the language supports functions as first class values.
In C# you can use the below function which will allow you to compose functions:
To 1:是(您也可以参数化 bool 和 string):
To 2:是,但您需要先编译表达式:
.Compile
会将表达式编译为您可以调用的动态 CLR 方法与返回的代表。您可以像这样使用此代码:
顺便说一句,在这种情况下,您确实应该编写一个高阶函数。你在这里所做的(代数上的)是组成幺半群。还记得函数组合吗? <代码>f . g := f(g(x)) 就是您在这里所做的。
将源视为
g:A->B
并将映射视为f:B->C
(其中 A、B 和 C 是集合),因此 <代码>f。 g 是h:A->C
。顺便说一句,.
运算符通常内置于函数式编程语言(例如 Haskell)中,并实现与compose
函数相同的功能(但语法更简洁)。To 1: Yes (You can also parametrize bool and string):
To 2: Yes, but you need to compile the expression first:
.Compile
will compile the expression into a dynamic CLR method that you can invoke with the returned delegate.You can use this code like this:
By the way, in this case you should really write a higher-order function. What you are doing here (algebraically) is composing monoids. Remember function composition?
f . g := f(g(x))
is what you are doing here.Think of source as
g:A->B
and map asf:B->C
(where A,B and C are sets) so the result off . g
ish:A->C
. By the way, the.
operator is often build into functional programming languages, such as Haskell and achieves the same thing as yourcompose
function (but with cleaner syntax).