17 using System.Collections.Generic;
18 using System.Collections.Immutable;
19 using System.Collections.ObjectModel;
38 return new ReadOnlyDictionary<K, V>(enumerable.ToDictionary());
51 return new HashSet<TResult>(enumerable.Select(selector));
62 public static List<TResult>
ToList<T, TResult>(
this IEnumerable<T> enumerable, Func<T, TResult> selector)
64 return enumerable.Select(selector).ToList();
75 public static TResult[]
ToArray<T, TResult>(
this IEnumerable<T> enumerable, Func<T, TResult> selector)
77 return enumerable.Select(selector).ToArray();
90 return enumerable.Select(selector).ToImmutableArray();
101 return enumerable ==
null || !enumerable.Any();
110 public static T
Median<T>(
this IEnumerable<T> enumerable)
112 var collection = enumerable.ToList();
113 return collection.OrderBy(x => x).Skip(collection.Count/2).First();
126 return collection.Select(selector).Median();
142 throw new ArgumentNullException(nameof(list));
144 if (comparer ==
null)
146 throw new ArgumentNullException(nameof(comparer));
150 var upper = list.Count - 1;
152 while (lower <= upper)
154 var middle = lower + (upper - lower) / 2;
155 var comparisonResult = comparer(value, list[middle]);
156 if (comparisonResult < 0)
160 else if (comparisonResult > 0)
182 return BinarySearch(list, value, Comparer<TItem>.Default);
195 return list.BinarySearch(value, comparer.Compare);
204 public static IEnumerable<T>
Memoize<T>(
this IEnumerable<T> enumerable)
220 public static IEnumerable<T>
Range<T>(T start, T end, Func<T, T> incrementer,
bool includeEndPoint =
false)
221 where T : IComparable
226 while (current.CompareTo(end) <= 0)
228 yield
return current;
229 current = incrementer(current);
234 while (current.CompareTo(end) < 0)
236 yield
return current;
237 current = incrementer(current);
251 public static IEnumerable<IEnumerable<T>>
GroupAdjacentBy<T>(
this IEnumerable<T> enumerable, Func<T, T, bool> grouper)
253 using (var e = enumerable.GetEnumerator())
257 var list =
new List<T> {e.Current};
258 var pred = e.Current;
261 if (grouper(pred, e.Current))
268 list =
new List<T> {e.Current};
288 if(ReferenceEquals(left, right))
292 return !left.SetEquals(right);
305 while (enumerator.MoveNext())
307 yield
return enumerator.Current;
324 return dictionary.TryGetValue(key, out obj) ? obj : defaultValue;
333 public static void DoForEach<T>(
this IEnumerable<T> source, Action<T> action)
335 foreach (var element
in source)