forked from noblethrasher/Prelude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinq.cs
212 lines (166 loc) · 6.21 KB
/
Linq.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Prelude
{
public static class Linq
{
public static bool CountIs<T>(this IEnumerable<T> xs, Func<int, bool> pred)
{
var list = xs as IList<T>;
var array = xs as T[];
if (list != null)
return pred(list.Count);
if (array != null)
return pred(array.Length);
return pred(xs.Count());
}
public static bool CountIs<T>(this IEnumerable<T> xs, int n)
{
var list = xs as IList<T>;
var array = xs as T[];
if (list != null)
return list.Count == n;
if (array != null)
return array.Length == n;
int i = 0;
foreach (var x in xs)
if (i++ > n)
return false;
return i == n;
}
public static IEnumerable<TResult> MapLast<T, TResult>(this IEnumerable<T> xs, Func<T, TResult> init, Func<T, TResult> tail)
{
var enumerator = xs.GetEnumerator();
if (enumerator.MoveNext())
{
loop:
var x = enumerator.Current;
while (enumerator.MoveNext())
{
yield return init(x);
goto loop;
}
yield return tail(x);
}
}
public static IEnumerable<TResult> MapAlternate<T, TResult>(this IEnumerable<T> xs, Func<T, TResult> odd, Func<T, TResult> even)
{
var on_odd = false;
foreach (var x in xs)
yield return ((on_odd = !on_odd) ? odd : even)(x);
}
//Alternative to LINQ's "select"
public static IEnumerable<TResult> Map<T, TResult>(this IEnumerable<T> xs, Func<T, TResult> f)
{
return xs.Select(f);
}
//Alternative to LINQ's "select"
public static IEnumerable<TResult> Map<T, TResult>(this IEnumerable<T> xs, int n, Func<T, int, TResult> fn)
{
return xs.Select(fn);
}
//public static IEnumerable<T> Bind<S, T>(this IEnumerable<S> xs, Func<S, IEnumerable<T>> f)
//{
// return xs.SelectMany(f);
//}
//public static IEnumerable<T> Bind<S, T>(this IEnumerable<S> xs, Func<S, int, IEnumerable<T>> f)
//{
// return xs.SelectMany(f);
//}
//public static IEnumerable<U> Bind<S, T, U>(this IEnumerable<S> xs, Func<S, IEnumerable<T>> f, Func<S, T, U> g)
//{
// return xs.SelectMany(f, g);
//}
//public static IEnumerable<U> Bind<S, T, U>(this IEnumerable<S> xs, Func<S, int, IEnumerable<T>> f, Func<S, T, U> g)
//{
// return xs.SelectMany(f, g);
//}
public static IEnumerable<T> Except<T>(this IEnumerable<T> xs, T x)
{
return xs.Except (new[] { x });
}
public static IEnumerable<T> Reject<T>(this IEnumerable<T> xs, Func<T, bool> predicate)
{
return xs.Where(x => !predicate(x));
}
public static IEnumerable<T> SkipAll<T>(this IEnumerable<T> xs)
{
#pragma warning disable
while (false)
yield return default(T);
#pragma warning restore
}
public sealed class Partition<T>
{
public readonly IEnumerable<T> Part;
public readonly IEnumerable<T> Rest;
internal Partition(IEnumerable<T> Part, IEnumerable<T> Rest)
{
this.Part = Part;
this.Rest = Rest;
}
}
public sealed class TaggedEnumerable<K, T> : IEnumerable<T>
{
public K Tag { get; private set; }
IEnumerable<T> list;
internal TaggedEnumerable(K tag, IEnumerable<T> list)
{
Tag = tag;
this.list = list;
}
public IEnumerator<T> GetEnumerator()
{
return list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public sealed class TaggedEnumerableCollection<K, T> : IEnumerable<TaggedEnumerable<K, T>>
{
readonly List<TaggedEnumerable<K, T>> xs;
public readonly IEnumerable<T> Remainder;
internal TaggedEnumerableCollection(IEnumerable<TaggedEnumerable<K, T>> xs, IEnumerable<T> Remainder)
{
this.xs = new List<TaggedEnumerable<K, T>>(xs);
this.Remainder = Remainder;
}
public IEnumerator<TaggedEnumerable<K, T>> GetEnumerator()
{
return xs.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public static Partition<T> PartitionBy<T>(this IEnumerable<T> xs, Func<T, bool> p)
{
var part = new List<T>();
var rest = new List<T>();
foreach (var x in xs)
if (p(x))
part.Add(x);
else
rest.Add(x);
return new Partition<T>(part, rest);
}
public static TaggedEnumerableCollection<U, T> PartitionBy<T, U>(this IEnumerable<T> xs, IEnumerable<U> ys, Func<U, Func<T, bool>> predicateBuilder)
{
var ps = ys.Select(y => new { Tag = y, Predicate = predicateBuilder(y) });
var partitions = new List<TaggedEnumerable<U,T>>();
Partition<T> partition = null;
foreach (var p in ps)
{
partition = xs.PartitionBy(p.Predicate);
partitions.Add(new TaggedEnumerable<U,T>(p.Tag, partition.Part));
xs = partition.Rest;
}
return new TaggedEnumerableCollection<U, T>(partitions, partition.Rest);
}
}
}