top | item 22367251

(no title)

SuddsMcDuff | 6 years ago

C# can only go so far down the road towards F# without introducing currying and partial application. I can't see those ever being introduced into C#.

discuss

order

LandR|6 years ago

You can sorta get partial application using lambdas, or whatever they called in C#

Like a poor mans hacky partial application, one argument at a time.

     namespace Bored
     {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using static System.Console;
        internal static class Program
        {
           private static void Main()
           {
              var addFive = Add.Partial(5);
              addFive(6).Print(); // Prints 11
              Enumerable.Range(0, 5).Select(addFive).ForEach(WriteLine); // Prints 5 to 9
           }
           private static Func<int, int, int> Add = (int x, int y) => x + y;
        }

        internal static class Extensions
        {
             public static Func<T2, TR> Partial<T1, T2, TR>(this Func<T1, T2, TR> func, T1 t1) => t2 => func(t1, t2);
             public static void Print<T>(this T t) => WriteLine(t);
             public static void ForEach<T>(this IEnumerable<T> xs, Action<T> f) => xs.ToList().ForEach(f);
         }
     }
Define your partial extension method for each arrity to Func. I think it maxes at 16?