Advertisement
Pro_Unit

FuncExtensions

Sep 16th, 2023
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. using System;
  2.  
  3. namespace FunctionalProgrammingInCSharp
  4. {
  5.     public static class FuncExtensions
  6.     {
  7.         public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(this Func<T1, T2, TResult> func) =>
  8.             x => y => func(x, y);
  9.  
  10.         public static Func<T2, TResult> Partial<T1, T2, TResult>(this Func<T1, T2, TResult> function, T1 arg1) =>
  11.             b => function(arg1, b);
  12.  
  13.         public static Func<T2, T1, TResult> Flip<T1, T2, TResult>(this Func<T1, T2, TResult> function) =>
  14.             (b, a) => function(a, b);
  15.  
  16.         public static Func<T1, TResult> FlipAndPartial<T1, T2, TResult>(this Func<T1, T2, TResult> func, T2 value) =>
  17.             func.Flip().Partial(value);
  18.  
  19.         public static Func<T, TResult> Compose<T, TResult>(this Func<T, TResult> f, Func<T, T> g) where T : TResult =>
  20.             x => f(g(x));
  21.        
  22.         public static Func<T, C, Func<T, C, R>, R> Compose<T, C, R>(this Func<T, C, Func<T, C, R>, R> outer, Func<T, C, Func<T, C, R>, R> inner)
  23.         {
  24.             return (target, token, next) => outer(target, token, (t, c) => inner(t, c, next));
  25.         }
  26.        
  27.         public static Func<T> Aggregate<T>(this Func<T> action, Func<T, T> other)
  28.         {
  29.             T a = action();
  30.             T b = other(a);
  31.             return () => b;
  32.         }
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement