Binding Extensions

Developer
Jun 3, 2008 at 5:09 AM
I'm a big fan of the C++ STL.  One of the nice things it has is a couple of methods like bind1st and bind2nd.  This allow you to adapt binary functions into unary functions.  Boost (http://boost.org) has extended this quite a bit with their positional parameters (_1, _2, etc.).  Now we can't get positional parameters (I've tried.  But generics just aren't as powerful as templates), but we can have simple bindings.  I'm thinking of adding the Func and Action bindings that I have to the library.  Here are the func bindings...

using System;

namespace nVentive.Umbrella.Extensions
{
    public static class FuncBindingExtensions
    {
        public static Func<TResult>
            Bind<T, TResult>(this Func<T, TResult> func, T value)
        {
            return () => func(value);
        }

        public static Func<T2, TResult>
            BindFirst<T1, T2, TResult>(this Func<T1, T2, TResult> func, T1 first)
        {
            return (T2 last) => func(first, last);
        }

        public static Func<T1, TResult>
            BindLast<T1, T2, TResult>(this Func<T1, T2, TResult> func, T2 last)
        {
            return (T1 first) => func(first, last);
        }

        public static Func<T2, T3, TResult>
            BindFirst<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> func, T1 first)
        {
            return (T2 second, T3 last) => func(first, second, last);
        }

        public static Func<T1, T2, TResult>
            BindLast<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> func, T3 last)
        {
            return (T1 first, T2 second) => func(first, second, last);
        }

        public static Func<T2, T3, T4, TResult>
            BindFirst<T1, T2, T3, T4, TResult>(this Func<T1, T2, T3, T4, TResult> func, T1 first)
        {
            return (T2 second, T3 third, T4 last) => func(first, second, third, last);
        }

        public static Func<T1, T2, T3, TResult>
            BindLast<T1, T2, T3, T4, TResult>(this Func<T1, T2, T3, T4, TResult> func, T4 last)
        {
            return (T1 first, T2 second, T3 third) => func(first, second, third, last);
        }
    }
}

I won't post the Action ones because they are pretty much the same but with no TResult type parameter.  Anyone have any problems with these being in the library?

Justin
Jun 3, 2008 at 2:20 PM
"Anyone have any problems with these being in the library?"

No problems at all, except that they should be renamed to Curry.
Coordinator
Jun 4, 2008 at 7:19 PM
Sorry for the long delay in the response. Was Out of town (whan an excuse!)

I totally agree it should be renamed to Curry as it is exactly that.

@Justin: You should send a message to TripleZ (on the People page) as I thought he was to integrate his currying functions in the project. He did a talk at DevTeach Toronto on Functionnal C# and had plenty of examples of "F# like" helpers.

Developer
Jun 4, 2008 at 8:19 PM
I'm not attached to any names, so I'll rename them.

And I'll ping TripleZ to find out what he's up to as well.