8

Does c#'s type system have the ability to specify a function that takes an enumerable number of functions which commute to form a pipeline?

The effect would be similar to chaining, but instead of

var pipeline = a.Chain(b).Chain(c)

one could write

var pipeline = CreatePipeline(a,b,c)

where a, b and c are functions? I have included a bit of sample code to illustrate, thanks.

void Main()
{
    Func<int, string>       a = i => i.ToString();
    Func<string, DateTime>  b = s => new DateTime(2000,1,1).AddDays(s.Length);
    Func<DateTime, bool>    c = d => d.DayOfWeek == DayOfWeek.Wednesday;

    //var myPipeline = CreatePipeline(a, b, c);

    Func<int, bool> similarTo =  i => c(b(a(i))) ;

    Func<int, bool> isThisTheBestWeCanDo = a.Chain(b).Chain(c);

}

public static class Ext{

    //public static Func<X, Z> CreatePipeline<X,Z>(params MagicFunc<X..Y>[] fns) {
    //  return 
    //}

    public static Func<X, Z> Chain<X,Y,Z>(this Func<X,Y> a, Func<Y,Z> b)
    {
        return x => b(a(x));
    }
}
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
William
  • 410
  • 3
  • 10

1 Answers1

3

You can consider this solution:

public class Pipeline
{
    public Func<object, object> CreatePipeline(params Func<object, object>[] funcs)
    {
        if (funcs.Count() == 1)
            return funcs[0];

        Func<object, object> temp = funcs[0];            
        foreach (var func in funcs.Skip(1))
        {
            var t = temp;
            var f = func;               
            temp = x => f(t(x));
        }                
        return temp;
    }
}

Usage:

Func<int, string> a = x => (x * 3).ToString();
Func<string, bool> b = x => int.Parse(x.ToString()) / 10 > 0;
Func<bool, bool> c = x => !x;

var pipeline = new Pipeline();
var func = pipeline.CreatePipeline(x => a((int)x), x => b((string)x), x => c((bool)x));

Console.WriteLine(func(3));//True
Console.WriteLine(func(4));//False
Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26