1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public void Main() { 5 .Show() .Pipe(Math.Double) .Log("It shoould double the number.") .Show() .Pipe(Math.Add, 1) .Pipe(Math.Square) .Show(); Console.WriteLine("----------");
var list = new List<int> { 1, 2, 3 }; var fn = (IEnumerable<int> p) => p.Select(Math.Square); list .Pipe(fn) .Show(); }
public static class PipeExtension { public static TOut Pipe<TIn, TOut>(this TIn input, Func<TIn, TOut> fn) => fn(input); public static TOut Pipe<TIn, TParam, TOut>(this TIn input, Func<TIn, TParam, TOut> fn, TParam p1) => fn(input, p1);
public static T Log<T>(this T t, string message) { Console.WriteLine($"[{DateTime.Now.ToUniversalTime():yyyy-MM-dd:HH:mm:ss}] {message}"); return t; } public static T Show<T>(this T t) { switch (t) { case ValueType: case string: Console.WriteLine($"[{DateTime.Now.ToUniversalTime():yyyy-MM-dd:HH:mm:ss}] {t}"); break; default: Console.WriteLine($"[{DateTime.Now.ToUniversalTime():yyyy-MM-dd:HH:mm:ss}] {JsonSerializer.Serialize(t)}"); break; } return t; } }
public static class Math { public static int Add(int i, int j) => i + j; public static int Mult(int i, int j) => i * j; public static int Double(int i) => i * 2; public static int Square(int i) => i * i; }
|