Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functors when should I use them whats their intended use [closed]

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?

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.

I've Read there are 4 functor types:

Comparer
Closure
Predicate
Transformer

We should probably Handle each one of them.

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

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

  • Lambda expressions are functors?
  • Anonymous functions are functors?

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;
    }
}

And then calling it with this:

 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);

So no fancy lambda style things.

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

Are delegates (functors) used as lambda expressions or anonymous methods "in most cases" just there as a shortcut for the programmer? There are as far as I can see only a few cases where they're actually the prefered choice for a problem.

like image 448
albertjan Avatar asked Jun 10 '09 07:06

albertjan


2 Answers

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.

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();
}

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

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

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.

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

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!"); };

I know of only one situation where this is worth knowing, which is when initializing an event so that you don't have to check for null before firing it:

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.

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

Re: Updated Question

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);

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

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

like image 60
Daniel Earwicker Avatar answered Oct 07 '22 09:10

Daniel Earwicker


In .NET terms, I think what you are describing is the Delegate - and it exists in all of .NET, not just C#.

I'm not sure that a "closure" would a "type" in the same was as a comparer/predicate/transformer, since in C# terms a closure is simply an implementation detail but can be any of those three.

In .NET, delegates are used in two main ways:

  • as the eventing mechanism
  • to provide functional-style programming

The first is important, but it sounds like you are more interested in the second. In reality, they operate much like single-method interfaces... consider:

List<int> vals = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<int> evenVals = vals.FindAll(i => i % 2 == 0); // predicate
List<string> valsAsStrings = vals.ConvertAll(i => i.ToString()); // transformer
// sort descending
vals.Sort((x, y) => y.CompareTo(x)); // comparer

A closure is more where we bring additional scope from outside the delegate into the delegate:

int max = int.Parse(Console.ReadLine()); // perhaps 6
List<int> limited = vals.FindAll(i => i <= max);

here the max is captured into the delegate as a closure.

Re "Are classes within the framework prepaired for this?" - many are, and LINQ goes a long way to allowing this even wider. LINQ provides extension methods over (for example) all of IEnumerable<T> - meaning that collections without delegate-based access aquire them for free:

int[] data = { 1,2,3,4,5,6,7,8,9 };
var oddData = data.Where( i => i % 2 == 1 );
var descending = data.OrderBy(i => -i);
var asStrings = data.Select(i => i.ToString());

Here the Where and OrderBy methods are LINQ extension methods that take delegates.

like image 41
Marc Gravell Avatar answered Oct 07 '22 07:10

Marc Gravell