Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Created July 1, 2012 18:13
Show Gist options
  • Save kakkun61/3029152 to your computer and use it in GitHub Desktop.
Save kakkun61/3029152 to your computer and use it in GitHub Desktop.
Functional Composition in C# ver. 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionalComposition
{
class FunctionalComposition
{
static void Main(string[] args)
{
Func<double, double> f = Math.Exp;
Func<double, double> g = x => Math.Pow(x, 2);
Func<double, double> h = x => -x + 1;
var composed = Function.Compose(Function.Compose(f, g), h);
Console.WriteLine(composed(10));
}
}
class Function
{
public static Func<T, V> Compose<T, U, V>(Func<U, V> f, Func<T, U> g)
{
return x => f(g(x));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment