forked from rebcabin/DotNetExtensionsImproved
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumerableExtensions.cs
284 lines (252 loc) · 12.2 KB
/
EnumerableExtensions.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// The MIT License
// Portions Copyright (c) 2011 Jordan E. Terrell, licensed to
// Microsoft Corporation under the MIT license (copied below).
//
// Portions Copyright (c) 2011 Microsoft Corporation
//
// Portions adapted from http://northhorizon.net/ under the
// Creative Commons Attribution license
// http://creativecommons.org/licenses/by/3.0/us/)
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
namespace Experimental.DotNetExtensions
{
/// <summary>
/// Provides extension methods for <see cref="IEnumerable{T}"/> and <see cref="IEnumerable"/>.
/// </summary>
public static class IEnumerable
{
/// <summary>
/// The standard monadic "Return" operation.
/// </summary>
/// <typeparam name="T">The type of the thing to lift into the monad.</typeparam>
/// <param name="singleValue">The sone value to lift into the monad.</param>
/// <returns>An IEnumerable containing a single item of type T.</returns>
public static IEnumerable<T> Return<T>(this T singleValue)
{
return new[] { singleValue } as IEnumerable<T>;
}
/// <summary>
/// Constructs the outer product (outer join) of two IEnumerables, as an IEnumerable of Tuples.
/// </summary>
/// <param name="these">The first IEnumerable.</param>
/// <param name="those">The second IEnumerable.</param>
/// <returns>The IEnumerable of pairs (as Tuples) of the inputs.</returns>
public static IEnumerable<Tuple<A, B>> Outer<A, B>(
this IEnumerable<A> these,
IEnumerable<B> those)
{
Contract.Requires(these != null);
Contract.Requires(those != null);
return from a in these
from b in those
select Tuple.Create(a, b);
}
/// <summary>
/// Gets the input argument that produces the maximum value of the specified function.
/// </summary>
/// <typeparam name="A">The type of items in the collection.</typeparam>
/// <typeparam name="T">The type of the value yielded from the specified function.</typeparam>
/// <param name="collection">The target collection.</param>
/// <param name="function">The function used to produce values.</param>
/// <returns>The argument that produces the highest value.</returns>
public static A ArgMax<A, V>(
this IEnumerable<A> collection,
Func<A, V> function)
where V : IComparable<V>
{
Contract.Requires(collection != null);
Contract.Requires(function != null);
return ArgComp(collection, function, GreaterThan);
}
private static bool GreaterThan<A>(A first, A second) where A : IComparable<A>
{
return first.CompareTo(second) > 0;
}
/// <summary>
/// Gets the intput argument that produces the minimum value of the specified function.
/// </summary>
/// <typeparam name="A">The type of items in the collection.</typeparam>
/// <typeparam name="T">The type of the value yielded from the specified function.</typeparam>
/// <param name="collection">The target collection.</param>
/// <param name="function">The function used to produce values.</param>
/// <returns>The argument that produces the least value.</returns>
public static A ArgMin<A, V>(
this IEnumerable<A> collection,
Func<A, V> function)
where V : IComparable<V>
{
Contract.Requires(collection != null);
Contract.Requires(function != null);
return ArgComp(collection, function, LessThan);
}
private static bool LessThan<A>(A first, A second) where A : IComparable<A>
{
return first.CompareTo(second) < 0;
}
private static A ArgComp<A, V>(
IEnumerable<A> collection,
Func<A, V> function,
Func<V, V, bool> accept)
where V : IComparable<V>
{
Contract.Requires(collection != null);
Contract.Requires(function != null);
Contract.Requires(accept != null);
var isSet = false;
var maxArg = default(A);
var extremeValue = default(V);
foreach (var item in collection)
{
var value = function(item);
if (!isSet || accept(value, extremeValue))
{
maxArg = item;
extremeValue = value;
isSet = true;
}
}
return maxArg;
}
/// <summary>
/// Encapsulates returned argument-and-value pairs from ArgAndMax and ArgAndMin.
/// </summary>
/// <typeparam name="A">The argument at which the IEnumerable has its extreme value.</typeparam>
/// <typeparam name="T">The extreme value.</typeparam>
public class ArgumentValuePair<A, V>
{
public A Argument { get; set; }
public V Value { get; set; }
}
/// <summary>
/// Gets the argument that produces the maximum value and the maximum value produced by the specified function.
/// </summary>
/// <typeparam name="A">The type of items in the collection.</typeparam>
/// <typeparam name="T">The type of the value yielded from the specified function.</typeparam>
/// <param name="collection">The target collection.</param>
/// <param name="function">The function used to produce values.</param>
/// <returns>Instance of an anonymous type with properties "Argument" and "Value" for the argument producing the maximum value and the minimum value.</returns>
public static ArgumentValuePair<A, V> ArgAndMax<A, V>(
this IEnumerable<A> collection,
Func<A, V> function)
where V : IComparable<V>
{
Contract.Requires(collection != null);
Contract.Requires(function != null);
return ArgAndComp(collection, function, GreaterThan);
}
/// <summary>
/// Gets the argument that produces the minimum value and the minimum value produced by the specified function.
/// </summary>
/// <typeparam name="A">The type of items in the collection.</typeparam>
/// <typeparam name="T">The type of the value yielded from the specified function.</typeparam>
/// <param name="collection">The target collection.</param>
/// <param name="function">The function used to produce values.</param>
/// <returns>Instance of a dynamic type with properties "Argument" and "Value" for the argument producing the minimum value and the minimum value.</returns>
public static ArgumentValuePair<A, V> ArgAndMin<A, V>(
this IEnumerable<A> collection,
Func<A, V> function)
where V : IComparable<V>
{
Contract.Requires(collection != null);
Contract.Requires(function != null);
return ArgAndComp(collection, function, LessThan);
}
private static ArgumentValuePair<A, V> ArgAndComp<A, V>(
IEnumerable<A> collection,
Func<A, V> function,
Func<V, V, bool> accept)
where V : IComparable<V>
{
Contract.Requires(collection != null);
Contract.Requires(function != null);
Contract.Requires(accept != null);
var isSet = false;
var maxArg = default(A);
var extremeValue = default(V);
foreach (var item in collection)
{
var value = function(item);
if (!isSet || accept(value, extremeValue))
{
maxArg = item;
extremeValue = value;
isSet = true;
}
}
return new ArgumentValuePair<A, V> { Argument = maxArg, Value = extremeValue };
}
/// <summary>
/// Perform an action for side effect pairwise on elements of a pair of IEnumerables.
/// </summary>
/// <typeparam name="T">The type of elements in the first IEnumerable.</typeparam>
/// <typeparam name="U">The type of elements in the second IEnumerable.</typeparam>
/// <param name="first">The first IEnumerable.</param>
/// <param name="second">The second IEnumerable.</param>
/// <param name="action">The action to perform on pairs of elements.</param>
public static void ZipDo<T, U>(
this IEnumerable<T> first,
IEnumerable<U> second,
Action<T, U> action)
{
Contract.Requires(first != null);
Contract.Requires(second != null);
Contract.Requires(action != null);
using (IEnumerator<T> firstEnumerator = first.GetEnumerator())
using (IEnumerator<U> secondEnumerator = second.GetEnumerator())
while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext())
action(firstEnumerator.Current, secondEnumerator.Current);
}
/// <summary>
/// Perform an action for side effect pairwise on adjacent members of an IEnumerable.
/// </summary>
/// <typeparam name="T">The type of elements in the input enumerable.</typeparam>
/// <param name="enumerable">The input enumerable.</param>
/// <param name="action">The action to perform on pairs of adjacent elements.</param>
public static IEnumerable<T> PairwiseDo<T>(
this IEnumerable<T> enumerable,
Action<T, T> action)
{
Contract.Requires(enumerable != null);
Contract.Requires(action != null);
Contract.Requires(enumerable.First() != null);
enumerable.ZipDo(enumerable.Skip(1), action);
return enumerable;
}
/// <summary>
/// Map a binary function over all adjacent pairs in the input enumerable.
/// </summary>
/// <typeparam name="T">The type of elements in the input enumerable.</typeparam>
/// <typeparam name="U">The type of element in the returned enumerable.</typeparam>
/// <param name="enumerable">The input enumerable.</param>
/// <param name="resultSelector">The function to map.</param>
/// <returns>An enumerable of results of mapping the function over the input enumerable, two adjacent elements at a time.</returns>
public static IEnumerable<U> Pairwise<T, U>(
this IEnumerable<T> enumerable,
Func<T, T, U> resultSelector)
{
Contract.Requires(enumerable != null);
Contract.Requires(resultSelector != null);
Contract.Requires(enumerable.First() != null);
return enumerable.Zip(enumerable.Skip(1), resultSelector);
}
}
}