推断泛型类型与功能性成分 [英] Inferring generic types with functional composition

查看:147
本文介绍了推断泛型类型与功能性成分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我想要实现的功能成分,如:

Suppose I want to implement a functional composition, like this:

    public Func<T,T> Compose<T>(Func<T,T> f, Func<T,T> g)
    {
        return new Func<T,T>( x => f(g(x)));
    }

现在在实践中,我可以用这撰写()FN这样的:

Now in practice, I can use this Compose() fn like this:

    public String ToUpper(String s) { return s.ToUpper(); }        
    public String Replicate(String s) { return s+s; }

    public void Run()
    {
        var h = Compose<String>(ToUpper, Replicate);
        System.Console.WriteLine("{0}", h("fred"));
    }

和结果是 FREDFRED

有没有使用更简单的语法调用撰写的方法吗?我想是这样的:

Is there a way to use a simpler syntax to invoke Compose? I tried like this:

        var h = Compose(ToUpper, Replicate);



...但我得到一个编译错误:

...but I get a compile error:

错误CS0411:为方法的类型参数'FunctionalTest.Compose(System.Func,System.Func)'不能从使用推断。尝试显式指定类型参数。

error CS0411: The type arguments for method 'FunctionalTest.Compose(System.Func, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

可以理解的。我想知道是否有可能以不同的声明,并得到推理实际工作。

Quite understandable. I am wondering if it is possible to declare it differently and get the inference to actually work.

修改结果
问题的由来:我在看一名大学生函数式编程课程的在线讲座,加州大学伯克利分校的CS61A。 (发现它在YouTube上)。我没有对任何FP正式训练,我想我可能会学到一些东西。该教授使用计划和他谈论计划+口齿不清怎么是纯粹的函数式语言,和其他语言要少一些。他特别指出的Pascal,C,C ++和Java(而不是C#)为缺乏功能的能力,并表示这将是很难做到这些语​​言的功能性成分(没有站在你的头)。他断言一个指针到功能(如在C,C ++提供)是不一样的函数实体,一个拉姆达。我明白了。

EDIT
The origin of the problem: I was watching an online lecture of an undergrad Functional Programming course, UC Berkley's CS61A. (find it on youtube). I don't have any formal training on FP, and I thought I might learn something. The prof uses scheme and he talks about how scheme + lisp are purely functional languages, and other languages are less so. He specifically identified Pascal, C, C++, and Java (but not C#) as lacking functional capabilities, and said it would be difficult to do functional composition with these languages ("Without standing on your head"). He asserted that a pointer-to-function (as available in C, C++) is not the same as a function "entity", a lambda. I get that.

搞笑 - 他没有提到JavaScript或C#,我认为是主流语言在都具有相当不错的实用功能。 (我不知道F#)

Funny - he didn't mention Javascript or C#, which I consider to be mainstream languages that both have pretty good functional capabilities. (I don't know F#.)

我觉得很奇怪,这是从去年开始的演讲 - 14个月前 - 但他似乎不知道的主流功能方面,现代语言。

I find it curious that this is a lecture from last year - 14 months ago - and yet he seems to be unaware of the functional aspects of mainstream, modern languages.

所以我沿着以下和做练习,但不是使用方案或口齿不清,我使用C#。并且还做了一些他们在Javascript。

So I'm following along and doing exercises, but instead of using scheme or lisp, I'm using C#. And also doing some of them in Javascript.

反正感谢大家对质量的响应。

Anyway thanks to everyone for the quality responses.

推荐答案

下面的代码将工作:

Func<string, string> toUpper = ToUpper;
Func<string, string> replicate = Replicate;

// now the compiler knows that the parameters are Func<string, string>
var h = Compose(toUpper, replicate);



因此​​,也许你仍然可以通过定义仅一旦这些变量来得到你所寻求的可读性改善和他们重用他们在整个测试中(我假设这是一个测试工具...)

So maybe you can still get the readability improvement you are seeking by defining those variables only once and them reusing them throughout your tests (I'm assuming this is a test utility...)

这篇关于推断泛型类型与功能性成分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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