Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind function (delegate) arguments

I'm trying to achieve the following without using lambda function:

Func<bool> test = () => RunSomething("test");  <-- This work fine but creates lambda
Func<bool> test = bind(RunSomething, "test"); <-- Bind "test" to RunSomething  

In other words I'm want to know if it is possible to somehow bind Function and arguments.
It is possible in C++ using std::bind, but is it possible in C# ?

like image 546
JobNick Avatar asked Dec 02 '14 13:12

JobNick


1 Answers

Well it's easy to build such a method, but that would use a lambda expression for the implementation:

public Func<TResult> Bind<T, TResult>(Func<T, TResult> func, T arg)
{
    return () => func(arg);
}

And likewise some overloads for functions with more arguments:

public Func<T2, TResult> Bind<T1, T2, TResult>
    (Func<T1, T2, TResult> func, T1 arg)
{
    return t2 => func(arg, t2);
}

public Func<T2, TResult> Bind<T1, T2, T3, TResult>
    (Func<T1, T2, T3, TResult> func, T1 arg)
{
    return (t2, t3) => func(arg, t2, t3);
}

Keep going as far as you want - possibly even adding methods to bind more than one argument in a call.

You can do all this without a lambda expression, but it would just be more work. For example:

public Func<TResult> Bind<T, TResult>(Func<T, TResult> func, T arg)
{
    return new Binder<T, TResult>(func, arg).Apply;
}

private sealed class Binder<T, TResult>
{
    private readonly T arg;
    private readonly Func<T, TResult> func;

    internal Binder(Func<T, TResult> func, T arg)
    {
        this.func = func;
        this.arg = arg;
    }

    public TResult Apply()
    {
        return func(arg);
    }
}

That's basically what the compiler would do for you automatically with a lambda expression, so why do it yourself?

like image 93
Jon Skeet Avatar answered Nov 07 '22 21:11

Jon Skeet