forked from noblethrasher/Prelude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctional.cs
58 lines (46 loc) · 1.2 KB
/
Functional.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Prelude
{
public static class Functional
{
public static Func<X, Y> Memoize<X, Y>(this Func<X, Y> f)
{
var memo = new Dictionary<X, Y>();
return x =>
{
var y = default(Y);
if (!memo.TryGetValue(x, out y))
{
y = f(x);
memo.Add(x, y);
}
return y;
};
}
public static Func<X, Z> Compose<X, Y, Z>(this Func<X, Y> f, Func<Y, Z> g)
{
return x => g(f(x));
}
public static Func<A, Func<B, C>> Curry<A, B, C>(this Func<A, B, C> f)
{
return a => b => f(a, b);
}
public static Func<A, Func<B, Func<C, D>>> Curry<A, B, C, D>(this Func<A, B, C, D> f)
{
return a => b => c => f(a, b, c);
}
}
public abstract class Unit
{
public static readonly Unit Value = new _Unit();
private Unit()
{
}
class _Unit : Unit
{
}
}
}