转换 Func到 Func

发布于 2024-11-30 10:16:48 字数 538 浏览 1 评论 0原文

我想我的思想正在爆炸,试图找出 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 技术交流群。

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

发布评论

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

评论(4

慢慢从新开始 2024-12-07 10:16:49

对于第一部分,您甚至可以创建一些“更高”阶函数:



Func<A,C> MapFun<A,B,C>(Func<A,B> input, Func<B,C> transf)
{
   return a => transf(input(a));
}

使用 with



Func <T,string> test = ...
var result = MapFun(test, String.IsNullOrWhiteSpace);

(我希望 C# 类型类型推断在这里起作用)

如果您将其定义为 Func 上的扩展,它会变得更加容易:


public static class FuncExtension
{
    public static Func<A,C> ComposeWith<A,B,C>(this Func<A,B> input, Func<B,C> f)
    {
         return a => f(input(a));
    }
}


Func<int, string> test = i => i.ToString();
var result = test.ComposeWith(string.IsNullOrEmpty);

这是一个非常简单的测试 第二个:我认为你可以将表达式编译成“真正的”Func,然后使用上面的代码。 请参阅有关 Expression.Compile 的 MSDN 文档

PS:重命名了该函数以更好地匹配它的意图(它的功能组合)

for the first part you can even make some "higher"-order function:



Func<A,C> MapFun<A,B,C>(Func<A,B> input, Func<B,C> transf)
{
   return a => transf(input(a));
}

use with



Func <T,string> test = ...
var result = MapFun(test, String.IsNullOrWhiteSpace);

(I hope C# type type inference is working here)

If you define this as extension on Func it gets even easier:


public static class FuncExtension
{
    public static Func<A,C> ComposeWith<A,B,C>(this Func<A,B> input, Func<B,C> f)
    {
         return a => f(input(a));
    }
}

here is a very simple test:


Func<int, string> test = i => i.ToString();
var result = test.ComposeWith(string.IsNullOrEmpty);

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)

从此见与不见 2024-12-07 10:16:49

您不能将其定义为单独的委托吗:

Func<T, string> func1 = t => t.ToString();
Func<T, bool> func2 = t => string.IsNullOrEmpty(func1(t));

Could you not define it as a separate delegate:

Func<T, string> func1 = t => t.ToString();
Func<T, bool> func2 = t => string.IsNullOrEmpty(func1(t));
有深☉意 2024-12-07 10:16:49

对于第一部分,该技术称为函数组合,即组合 2 个函数来创建一个新函数。
在您的情况下,您有一个函数 Func 和另一个类型为 Func 的函数(如字符串空或 null),使用 function组合 您可以组合这两个函数来创建 Func 类型的新函数

大多数函数式编程语言都已在其标准库或语言本身中定义了这种函数组合。但是,如果该语言支持函数作为第一类值,那么为您的语言创建一个函数并不困难。

在 C# 中,您可以使用以下函数来组合函数:

public static Func<X,Z> Compose<X,Y,Z>(Func<X,Y> a, Func<Y,Z> b)
{
    return (v) => b(a(v));
}

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 type Func<string,bool>, using function composition you can compose these two functions to create a new function of type Func<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:

public static Func<X,Z> Compose<X,Y,Z>(Func<X,Y> a, Func<Y,Z> b)
{
    return (v) => b(a(v));
}
苯莒 2024-12-07 10:16:49

To 1:是(您也可以参数化 bool 和 string):

Func<T, bool> Compose<T>(Func<T, string> source, Func<string, bool>map)
{
    return x => map(source(x));
}

To 2:是,但您需要先编译表达式:

Func<T, bool> Compose<T>(Expression<Func<T, string>> source, Func<string, bool> map)
{
    return x => compose(source.Compile(), map)
}

.Compile 会将表达式编译为您可以调用的动态 CLR 方法与返回的代表。

您可以像这样使用此代码:

Func<int, string> ts = i => i.ToString();
var result = Compose(ts, string.IsNullOrEmpty);

顺便说一句,在这种情况下,您确实应该编写一个高阶函数。你在这里所做的(代数上的)是组成幺半群。还记得函数组合吗? <代码>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):

Func<T, bool> Compose<T>(Func<T, string> source, Func<string, bool>map)
{
    return x => map(source(x));
}

To 2: Yes, but you need to compile the expression first:

Func<T, bool> Compose<T>(Expression<Func<T, string>> source, Func<string, bool> map)
{
    return x => compose(source.Compile(), map)
}

.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:

Func<int, string> ts = i => i.ToString();
var result = Compose(ts, string.IsNullOrEmpty);

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 as f:B->C (where A,B and C are sets) so the result of f . g is h:A->C. By the way, the . operator is often build into functional programming languages, such as Haskell and achieves the same thing as your compose function (but with cleaner syntax).

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