开发者

Does C# support function composition?

In the 开发者_开发问答latest version of C#, can I do something like Haskell's function composition? more...?

Function composition is the act of pipelining the result of one function, to the input of another, creating an entirely new function.

I feel like linq is the closest but that's chaining, not function composition, right?


public static class Extensions
{
    public static Func<T, TReturn2> Compose<T, TReturn1, TReturn2>(this Func<TReturn1, TReturn2> func1, Func<T, TReturn1> func2)
    {
        return x => func1(func2(x));
    }
}

Usage:

Func<int, int> makeDouble = x => x * 2;
Func<int, int> makeTriple = x => x * 3;
Func<int, string> toString = x => x.ToString();
Func<int, string> makeTimesSixString = toString.Compose(makeDouble).Compose(makeTriple);

//Prints "true"
Console.WriteLine(makeTimesSixString (3) == toString(makeDouble(makeTriple(3))));


I did not let the compiler check this, but this should be possible:

public static Func<T3,T1> my_chain<T1, T2, T3>(Func<T2,T1> f1, Func<T3,T2> f2)
{
    return x => f2(f1(x));
}


There is no specific operator / "syntax sugar" for that in C# (however, in F# you would use the >> operator).

There is a great blog post on this subject from Matthew Podwysocki. He suggests this kind of construct in C#:

public static class FuncExtensions
{
    public static Func<TSource, TResult> ForwardCompose<TSource, TIntermediate, TResult>(
        this Func<TSource, TIntermediate> func1, Func<TIntermediate, TResult> func2)
    {
        return source => func2(func1(source));
    }
}

Func<Func<int, int>, IEnumerable<int>, IEnumerable<int>> map = (f, i) => i.Select(f);
Func<Func<int, bool>, IEnumerable<int>, IEnumerable<int>> filter = (f, i) => i.Where(f);
Func<int, Func<int, int, int>, IEnumerable<int>, int> fold = (s, f, i) => i.Aggregate(s, f);

// Compose together
var mapFilterFold = map.Apply(x => x * x * x)
    .ForwardCompose(filter.Apply(x => x % 3 == 0))
    .ForwardCompose(fold.Apply(1, (acc, x) => acc * x));
Console.WriteLine(mapFilterFold(Enumerable.Range(1, 10)));


C# doesn't have first class support but it's not particularly hard to implement. You just have to write a lot of overloads.

public static class Composition
{
    public static Func<T2> Compose<T1, T2>(Func<T1> f1, Func<T1, T2> f2)
    {
        return () => f2(f1());
    }

    public static Func<T1, T3> Compose<T1, T2, T3>(Func<T1, T2> f1, Func<T2, T3> f2)
    {
        return v => f2(f1(v));
    }
}


It's not nearly as pretty, but you could do:

Func<IEnumerable<T>, IEnumerable<T>> desort = l => l.OrderBy(i => i).Reverse();

Or, if you want something more composit-y (that acts on the array in place):

Action<int[]> desort = a => Array.Reverse(Array.Sort(a));

Assuming those methods existed...but the syntax should be about right.

You could then use it in the following way (assuming you went with the first method above):

var arr = { 2,8,7,10,1,9,5,3,4,6 };
var desortedArr = desort(arr);


More than Linq, it's delegates, and lambda expressions/statements that are similar to chaining.

Func<int, string> a = p => p.ToString();
Func<string, string> b = p => "|" + p + "|";

Func<int, string> c = p => b(a(p));

The definition given in the link is:

Function composition is the act of pipelining the result of one function, to the input of another, creating an entirely new function.

And c is clearly a new function that chain call a and b.


No, not short of defining your own generic Compose<T, U...>() functions. C# has no built-in functionality to help you out with this. (It doesn't do partial application, either.)


Example for function composition in C#

f(x) ° g(x)

Func<int, int> f = x => x + 2;
Func<int, int> g = x => 3 * x;

Func<int, int> b = x => f(g(x));

int result = b(2); //Result is 8
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜