函子什么时候应该使用他们自己什么用途 [英] Functors when should I use them whats their intended use

查看:167
本文介绍了函子什么时候应该使用他们自己什么用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是似乎无法换我周围的头。

I Just can't seem to wrap my head around them.

据我了解它的动态添加逻辑的一类。为此做好了准备的框架内是类?

As I understand it's dynamically adding logic to a class. Are classes within the framework prepared for this?

为什么我应该只是扩展了类,并在扩展添加的功能吧。我会是全球可访问和AFAIK更容易维护。

Why should I just extend the class and add the functionality to it in the extension. I would be globally accessible and afaik much easier to maintain.

我读过有4个类型的仿函数:

I've Read there are 4 functor types:

的Comparer结果
封闭结果
谓结果
变压器

Comparer
Closure
Predicate
Transformer

我们也许应该处理它们中的每一个

We should probably Handle each one of them.

PS有没有类似的东西在动?

p.s. is there something like it in vb?

所以,我可以说出我认为lambda表达式是仿函数。这澄清了事情我有点:)(呵呵)

So I can state I think that lambda expressions are functors. This clears up things for me a bit :) (hehe)


  • Lambda表达式是仿函数?

  • 匿名函数是仿函数

不过,我问这个问题,因为我遇到了另一种类型的fucntors即这些的?

But I asked this question because I ran into another type of fucntors namely these ones:

delegate void FunctorDelegate(int value);
class Addition {
    FunctorDelegate _delegate;

    public Addition AddDelegate(FunctorDelegate deleg) {
        _delegate += deleg;
        return this;
    }
    public int AddAllElements(IList< int> list) {
        int runningTotal = 0;
        foreach( int value in list) {
            runningTotal += value;
            _delegate(value);
        }
        return runningTotal;
    }
}

和则与此调用它:

 int runningTotal = new Addition()
     .AddDelegate(new FunctorDelegate(
                     delegate(int value) {
                         if ((value % 2) == 1) {
                             runningOddTotal += value;
                         }
                     }))
    .AddDelegate(new FunctorDelegate(
                     delegate(int value) {
                         if ((value % 2) == 0) {
                             runningEvenTotal += value;
                         }
                     }))
    .AddAllElements(list);



因此​​,没有花哨的lambda风格的东西。

So no fancy lambda style things.

现在我有这样的例子,但它并不清楚为什么这是一个好的解决方案。

Now I have this example but it isn't at all clear why this is a "good" solution.

作为lambda表达式代表(函子)或匿名方法,在大多数情况下,就在那里,作为程序员的快捷方式?还有,据我可以看到只有少数情况下,它们实际上是一个问题的首选选择。

推荐答案

我想你混淆了不同语言的条款。你好像在C ++或Java感,例如要使用仿函数 看到维基百科页面。在C ++中,它是重载函数调用操作者,所以它可被用作一个功能,但与状态的类的对象

I think you're confusing terms from different languages. You seem to be using "Functor" in the C++ or Java sense, e.g. see the wikipedia page. In C++, it's an object of a class that overloads the function-call operator, so it can be used as a function but with state.

这在逻辑上是相同的事情如绑定到C#实例方法(或任何.NET语言)的委托。

This is logically the same thing as a delegate bound to an instance method in C# (or any .NET language).

有三种方式来写这样的事情。首先,你可以写一个普通的方法,然后分配方法将委托变量的名称。

There are three ways to write such a thing. First, you can write an ordinary method, and then assign the name of the method to a delegate variable.

void MyMethod() { Console.WriteLine("Hi!"); }

void Foo()
{
    Action a = MyMethod;
    a();
}



第二,你可以使用匿名方法的语法,在介绍了C#2.0:

Second, you can use anonymous method syntax, introduced in C# 2.0:

void Foo()
{
    Action a = delegate { Console.WriteLine("Hi!"); }
    a();
}



第三,你可以使用lambda语法,介绍了C#3.0:

Thirdly, you can use lambda syntax, introduced in C# 3.0:

void Foo()
{
    Action a = () => Console.WriteLine("Hi!");
    a();
}



最后两个的优点是,该方法的主体可以读取和写在任何一种方法的局部变量。

The advantage of the last two is that the body of the method can read and write local variables in the containing method.

在不久的方法lambda语法的优点是,它更简洁,它键入参数推断。

The advantage of lambda syntax over anon-methods are that it is more succinct and it does type inference on parameters.

更新:过lambda表达式匿名的方法(委托关键字)的好处是,你可以,如果你不完全忽略参数需要他们:

Update: The advantage of anon-methods (delegate keyword) over lambdas is that you can omit the parameters altogether if you don't need them:

// correct way using lambda
button.Click += (sender, eventArgs) => MessageBox.Show("Clicked!");

// compile error - wrong number of arguments
button.Click += () => MessageBox.Show("Clicked!");

// anon method, omitting arguments, works fine
button.Click += delegate { MessageBox.Show("Clicked!"); };



我只晓得一个情况下,这是值得了解的,这是初始化一个事件,这样,当你没有发射前检查

event EventHandler Birthday = delegate { };



避免了很多其他的废话。

Avoids a lot of nonsense elsewhere.

最后,​​你提到有四种仿函数。事实上,有可能的委托类型无穷大,虽然有些作者可能有他们的最爱,有明显的会出现一些常见的模式。一个动作命令不带任何参数,并返回无效,和一个谓语需要一些类型和返回的实例真正

Finally, you mention that there are four kinds of functor. In fact there are an infinity of possibly delegate types, although some authors may have their favourites and there obviously will be some common patterns. An Action or Command takes no parameters and returns void, and a predicate takes an instance of some type and returns true or false.

在C#3.0中,你可以掀起一个委托与任何你喜欢的类型多达四个参数:

In C# 3.0, you can whip up a delegate with up to four parameters of any types you like:

Func<string, int, double> f;  // takes a string and an in, returns a double



回复:更新问题

您问(我认为)如果有多个用例lambda表达式。有超过都不可能上市!

You ask (I think) if there are many use cases for lambdas. There are more than can possibly be listed!

您最经常看到他们在上序列(计算上的即时列表)操作更大的表达式的中间。假设我有一个人名单,我想人正是四十多岁的列表:

You most often see them in the middle of larger expressions that operate on sequences (lists computed on-the-fly). Suppose I have a list of people, and I want a list of people exactly forty years old:

var exactlyForty = people.Where(person => person.Age == 40);



其中,方法是一个扩展方法上在的IEnumerable< T> 接口,其中 T 在这种情况下是某种人物类。

The Where method is an extension method on the IEnumerable<T> interface, where T in this case is some kind of Person class.

这是在.NET中被称为LINQ到对象,但在其他地方被称为上的序列或流或纯函数编程偷懒列表(同一件事的所有不同的名称)。

This is known in .NET as "Linq to Objects", but known elsewhere as pure functional programming on sequences or streams or "lazy" lists (all different names for the same thing).

这篇关于函子什么时候应该使用他们自己什么用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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