Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace FunctionalProgrammingInCSharp
- {
- public static class FuncExtensions
- {
- public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(this Func<T1, T2, TResult> func) =>
- x => y => func(x, y);
- public static Func<T2, TResult> Partial<T1, T2, TResult>(this Func<T1, T2, TResult> function, T1 arg1) =>
- b => function(arg1, b);
- public static Func<T2, T1, TResult> Flip<T1, T2, TResult>(this Func<T1, T2, TResult> function) =>
- (b, a) => function(a, b);
- public static Func<T1, TResult> FlipAndPartial<T1, T2, TResult>(this Func<T1, T2, TResult> func, T2 value) =>
- func.Flip().Partial(value);
- public static Func<T, TResult> Compose<T, TResult>(this Func<T, TResult> f, Func<T, T> g) where T : TResult =>
- x => f(g(x));
- 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)
- {
- return (target, token, next) => outer(target, token, (t, c) => inner(t, c, next));
- }
- public static Func<T> Aggregate<T>(this Func<T> action, Func<T, T> other)
- {
- T a = action();
- T b = other(a);
- return () => b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement