using System; public class CompositionDemo { // A function from S to T public delegate T Function (S d); // The generic function for function composition // from T to S via U public static Function Compose (Function f, Function g){ return delegate(T d){return f(g(d));}; } // A generic PrintTable function public static void PrintTableOfFunction (Function f, string fname, S[] inputValues){ foreach(S s in inputValues) Console.WriteLine("{0,35}({1,-4:F3}) = {2}", fname, s, f(s)); Console.WriteLine(); } // DieFromInt: int -> Die public static Die DieFromInt(int i){ return new Die(i); } // Round: double -> int public static int Round(double d){ return (int)(Math.Round(d)); } public static void Main(){ double[] input = new double[25]; for(int i = 0; i < 25; i++) input[i] = (double) (i*2); // Compose(DieFromInt, Round): double -> Die // (via int) PrintTableOfFunction(Compose(DieFromInt, Round), "Die of double", input); } }