// Exercise 1: Func/Action composition (easy) // // Implement: // - Compose(f, g): returns a Func that computes g(f(x)) // - ApplyN(f, n): returns a Func that applies f n times // (n = 0 returns the identity function) // - MakeCounter(): returns an (Action increment, Func read) pair that // share one captured counter variable — the closure is the only state. // - Where(items, predicate): your own filter using Predicate, no LINQ. // // Run with: dotnet run (inside a console project). using System; using System.Collections.Generic; class Program { #pragma warning disable IDE0060 static Func Compose(Func f, Func g) { // TODO return x => 0; } static Func ApplyN(Func f, int n) { // TODO return x => 0; } static (Action increment, Func read) MakeCounter() { // TODO return (() => { }, () => 0); } static List Where(IEnumerable items, Predicate predicate) { // TODO return new List(); } #pragma warning restore IDE0060 static void Main() { int pass = 0, total = 0; total++; var addThenDouble = Compose(x => x + 1, x => x * 2); if (addThenDouble(3) == 8) { Console.WriteLine("PASS: Compose"); pass++; } else Console.WriteLine("FAIL: Compose(3) = " + addThenDouble(3)); total++; var plus3 = ApplyN(x => x + 1, 3); var identity = ApplyN(x => x * 100, 0); if (plus3(10) == 13 && identity(7) == 7) { Console.WriteLine("PASS: ApplyN"); pass++; } else Console.WriteLine("FAIL: ApplyN"); total++; var (inc, read) = MakeCounter(); var (inc2, read2) = MakeCounter(); inc(); inc(); inc2(); if (read() == 2 && read2() == 1) { Console.WriteLine("PASS: independent closures"); pass++; } else Console.WriteLine($"FAIL: counters {read()} / {read2()}"); total++; var evens = Where(new[] { 1, 2, 3, 4, 5, 6 }, n => n % 2 == 0); if (string.Join(",", evens) == "2,4,6") { Console.WriteLine("PASS: Where"); pass++; } else Console.WriteLine("FAIL: Where, got " + string.Join(",", evens)); Console.WriteLine($"--- {pass}/{total} checks passed ---"); } }