using System;
namespace Recipes.LINQ.R0102
{
public class FunctionalMethods
{
public static void Main()
{
// Functional method f(x) and g(x):
Func<int, int> f = x => x + 1;
Func<int, int> g = x => x + 2;
// Compound functional method f(g(x)):
Func<Func<int, int>, Func<int, int>, int, int> fog = (f1, g1, x) => f1.Invoke(g1.Invoke(x));
Console.WriteLine();
// Computing f(x) with x = 2:
Console.WriteLine("f(2) = {0}", f(2));
Console.WriteLine("g(3) = {0}", g(3));
Console.WriteLine("f(g(5) = {0}", fog(f, g, 5));
Console.WriteLine();
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIFJlY2lwZXMuTElOUS5SMDEwMgp7CiAgICBwdWJsaWMgY2xhc3MgRnVuY3Rpb25hbE1ldGhvZHMKICAgIHsKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCiAgICAgICAgewogICAgICAgICAgICAvLyBGdW5jdGlvbmFsIG1ldGhvZCBmKHgpIGFuZCBnKHgpOgogICAgICAgICAgICBGdW5jPGludCwgaW50PiBmID0geCA9PiB4ICsgMTsKICAgICAgICAgICAgRnVuYzxpbnQsIGludD4gZyA9IHggPT4geCArIDI7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBDb21wb3VuZCBmdW5jdGlvbmFsIG1ldGhvZCBmKGcoeCkpOgogICAgICAgICAgICBGdW5jPEZ1bmM8aW50LCBpbnQ+LCBGdW5jPGludCwgaW50PiwgaW50LCBpbnQ+IGZvZyA9IChmMSwgZzEsIHgpID0+IGYxLkludm9rZShnMS5JbnZva2UoeCkpOwogICAgICAgICAgICAKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICAgICAgLy8gQ29tcHV0aW5nIGYoeCkgd2l0aCB4ID0gMjoKICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJmKDIpID0gezB9IiwgZigyKSk7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJnKDMpID0gezB9IiwgZygzKSk7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJmKGcoNSkgPSB7MH0iLCBmb2coZiwgZywgNSkpOwogICAgICAgICAgICAKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICB9CiAgICB9Cn0=