-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctional.cs
More file actions
73 lines (56 loc) · 3.24 KB
/
Functional.cs
File metadata and controls
73 lines (56 loc) · 3.24 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Runtime.CompilerServices;
namespace MicroUtils
{
public readonly record struct Unit();
/// <summary>
/// Functional utilities
/// </summary>
public static partial class Functional
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static U Upcast<T, U>(this T t) where T : U => t;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Box<T>(this T t) where T : notnull => t;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Identity<T>(T x) => x;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Ignore<T>(T _) { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<B, C> PartialApply<A, B, C>(Func<A, B, C> f, A a) =>
(B b) => f(a, b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<B, C, D> PartialApply<A, B, C, D>(Func<A, B, C, D> f, A a) =>
(B b, C c) => f(a, b, c);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<B, C, D, E> PartialApply<A, B, C, D, E>(Func<A, B, C, D, E> f, A a) =>
(B b, C c, D d) => f(a, b, c, d);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<B, C, D, E, F> PartialApply<A, B, C, D, E, F>(Func<A, B, C, D, E, F> f, A a) =>
(B b, C c, D d, E e) => f(a, b, c, d, e);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<B, C, D, E, F, G> PartialApply<A, B, C, D, E, F, G>(Func<A, B, C, D, E, F, G> func, A a) =>
(B b, C c, D d, E e, F f) => func(a, b, c, d, e, f);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<B, C, D, E, F, G, H> PartialApply<A, B, C, D, E, F, G, H>(Func<A, B, C, D, E, F, G, H> func, A a) =>
(B b, C c, D d, E e, F f, G g) => func(a, b, c, d, e, f, g);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<A, Func<B, C>> Curry<A, B, C>(this Func<A, B, C> func) =>
a => b => func(a, b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<A, B, Func<C, D>> Curry<A, B, C, D>(this Func<A, B, C, D> func) =>
(a, b) => c => func(a, b, c);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<A, B, C, Func<D, E>> Curry<A, B, C, D, E>(this Func<A, B, C, D, E> func) =>
(a, b, c) => d => func(a, b, c, d);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<A, B, C, D, Func<E, F>> Curry<A, B, C, D, E, F>(this Func<A, B, C, D, E, F> func) =>
(a, b, c, d) => e => func(a, b, c, d, e);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<A, B, C, D, E, Func<F, G>> Curry<A, B, C, D, E, F, G>(this Func<A, B, C, D, E, F, G> func) =>
(a, b, c, d, e) => f => func(a, b, c, d, e, f);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Func<A, B, C, D, E, F, Func<G, H>> Curry<A, B, C, D, E, F, G, H>(this Func<A, B, C, D, E, F, G, H> func) =>
(a, b, c, d, e, f) => g => func(a, b, c, d, e, f, g);
}
}