Compare commits

..

No commits in common. "2b0d56b1a2d2abd2f5279b4a8712b7d9d61c8e97" and "3f887db1dc588bc695acbb5f222232bda958e393" have entirely different histories.

1 changed files with 34 additions and 104 deletions

View File

@ -10,53 +10,30 @@ namespace AdrianKousz.Util
{ {
#region Fillers #region Fillers
public static T[] Fill<T>(this T[] array) where T : new() { return Fill(array, () => new T()); } public static T[] Fill<T>(this T[] array) where T : new() { return Fill(array, i => new T()); }
public static T[] Fill<T>(this T[] array, T value) { return Fill(array, () => value); } public static T[] Fill<T>(this T[] array, T value) { return Fill(array, i => value); }
public static T[] Fill<T>(this T[] array, Func<T> f) public static T[] Fill<T>(this T[] array, Func<T> f) { return Fill(array, i => f()); }
public static T[] Fill<T>(this T[] array, Func<int, T> f)
{ {
var len = array.Length; var min = array.GetLowerBound(0);
for (var i = 0; i < len; i++) array[i] = f(); var max = array.GetUpperBound(0);
for (var i = min; i <= max; i++)
array[i] = f(i);
return array; return array;
} }
public static ICollection<T> Fill<T>(this ICollection<T> list, int count) where T : new() { return Fill(list, count, () => new T()); } public static IList<T> Fill<T>(this IList<T> list, int count) where T : new() { return Fill(list, i => new T(), count); }
public static ICollection<T> Fill<T>(this ICollection<T> list, int count, T value) { return Fill(list, count, () => value); } public static IList<T> Fill<T>(this IList<T> list, T value, int count) { return Fill(list, i => value, count); }
public static ICollection<T> Fill<T>(this ICollection<T> list, int count, Func<T> f) public static IList<T> Fill<T>(this IList<T> list, Func<T> f, int count) { return Fill(list, i => f(), count); }
public static IList<T> Fill<T>(this IList<T> list, Func<int, T> f, int count)
{ {
for (var i = 0; i < count; i++) list.Add(f()); for (var i = 0; i < count; i++)
list.Add(f(i));
return list; return list;
} }
#endregion #endregion
#region Anonymous Type Helpers
public static T[] CreateArray<T>(this T example, int count)
{
return new T[count];
}
public static List<T> CreateList<T>(this T example)
{
return new List<T>();
}
#endregion
#region Materializers
public static List<T> ToList<T>(this IEnumerable<T> source)
{
return source as List<T> ?? new List<T>(source);
}
public static T[] ToArray<T>(this IEnumerable<T> source)
{
return source as T[] ?? ToList(source).ToArray();
}
#endregion
#region Functional Classics #region Functional Classics
public static int IndexOf<T>(this IEnumerable<T> source, T value, Func<T, T, bool> comparer) public static int IndexOf<T>(this IEnumerable<T> source, T value, Func<T, T, bool> comparer)
@ -69,105 +46,58 @@ namespace AdrianKousz.Util
return -1; return -1;
} }
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> f) public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> f) { return ForEach(source, (i, x) => f(x)); }
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<int, T> f)
{ {
foreach (var el in source) f(el); var i = 0;
return source; foreach (var el in source) f(i++, el);
}
public static IDictionary<K, V> ForEach<K, V>(this IDictionary<K, V> source, Action<K, V> f)
{
foreach (var el in source) f(el.Key, el.Value);
return source; return source;
} }
// Linq: Select // Linq: Select
public static IEnumerable<TResult> Map<T, TResult>(this IEnumerable<T> source, Func<T, TResult> f) public static IEnumerable<TResult> Map<T, TResult>(this IEnumerable<T> source, Func<T, TResult> f)
{ {
var result = GetList<T, TResult>(source); foreach (var el in source) yield return f(el);
foreach (var el in source) result.Add(f(el));
return result;
} }
// Linq: Where // Linq: Where
public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> f) public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> f)
{ {
var result = GetList<T, T>(source); foreach (var el in source) if (f(el)) yield return el;
foreach (var el in source) if (f(el)) result.Add(el);
return result;
} }
public static TResult FoldL<T, TResult>(this IEnumerable<T> source, TResult initial, Action<TResult, T> f) public static TResult FoldL<T, TResult>(this IEnumerable<T> source, TResult initial, Action<TResult, T> f)
{ return FoldL(source, initial, (folded, el) => { f(folded, el); return folded; }); } { return FoldL(source, initial, (folded, el) => { f(folded, el); return folded; }); }
public static TResult FoldL<T, TResult>(this IEnumerable<T> source, TResult initial, Func<TResult, T, TResult> f) public static TResult FoldL<T, TResult>(this IEnumerable<T> source, TResult initial, Func<TResult, T, TResult> f)
{ {
var result = initial; var folded = initial;
foreach (var el in source) result = f(result, el); foreach (var el in source) folded = f(folded, el);
return result; return folded;
} }
// Linq: Aggregate // Linq: Aggregate
public static T FoldL1<T>(this IEnumerable<T> source, Func<T, T, T> f) public static T FoldL1<T>(this IEnumerable<T> source, Func<T, T, T> f)
{ {
using (var en = source.GetEnumerator()) using (var en = source.GetEnumerator()) {
{
if (!en.MoveNext()) throw new InvalidOperationException("No elements"); if (!en.MoveNext()) throw new InvalidOperationException("No elements");
T result = en.Current; T folded = en.Current;
while (en.MoveNext()) result = f(result, en.Current); while (en.MoveNext()) folded = f(folded, en.Current);
return result; return folded;
} }
} }
#endregion
#region Zippers
public static IDictionary<K, V> ToDictionary<K, V>(this IEnumerable<K> ks, IEnumerable<V> vs)
{
var result = new Dictionary<K, V>();
using (var en1 = ks.GetEnumerator())
using (var en2 = vs.GetEnumerator())
{
while (en1.MoveNext() && en2.MoveNext())
result[en1.Current] = en2.Current;
}
return result;
}
public static IDictionary<T, T> ToDictionary<T>(this IEnumerable<T> source)
{
var result = new Dictionary<T, T>();
var iskey = true;
var k = default(T);
foreach (var el in source)
if (iskey = !iskey) result[k] = el;
else k = el;
return result;
}
public static IEnumerable<TResult> Zip<T1, T2, TResult>(this IEnumerable<T1> s1, IEnumerable<T2> s2, Func<T1, T2, TResult> f) public static IEnumerable<TResult> Zip<T1, T2, TResult>(this IEnumerable<T1> s1, IEnumerable<T2> s2, Func<T1, T2, TResult> f)
{ {
var result = GetList<T1, TResult>(s1); using (var en1 = s1.GetEnumerator()) {
using (var en1 = s1.GetEnumerator()) using (var en2 = s2.GetEnumerator()) {
using (var en2 = s2.GetEnumerator()) while (en1.MoveNext() && en2.MoveNext()) {
{ yield return f(en1.Current, en2.Current);
while (en1.MoveNext() && en2.MoveNext()) }
result.Add(f(en1.Current, en2.Current));
} }
return result;
} }
#endregion
#region Implementation Detail
private static List<TResult> GetList<T, TResult>(IEnumerable<T> source)
{
var collection = source as ICollection<T>;
var length = collection == null ? 4 : collection.Count;
return new List<TResult>(length);
} }
#endregion #endregion
} }
} }