Compare commits

...

2 Commits

Author SHA1 Message Date
Adrian 2b0d56b1a2 ForEach for dictionaries 2016-02-22 16:25:44 +01:00
Adrian b6c478dcff Eager evaluation in Enumerables, other minor changes
* Eager evaluation of iterating functions,
  they are already in System.Linq
* Remove index parameters in functions
* Create array or list of anonymous objects
* Dictionary creation
* ToArray and ToList
2016-02-22 01:13:05 +01:00
1 changed files with 104 additions and 34 deletions

View File

@ -10,30 +10,53 @@ namespace AdrianKousz.Util
{
#region Fillers
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, i => value); }
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)
public static T[] Fill<T>(this T[] array) where T : new() { return Fill(array, () => new T()); }
public static T[] Fill<T>(this T[] array, T value) { return Fill(array, () => value); }
public static T[] Fill<T>(this T[] array, Func<T> f)
{
var min = array.GetLowerBound(0);
var max = array.GetUpperBound(0);
for (var i = min; i <= max; i++)
array[i] = f(i);
var len = array.Length;
for (var i = 0; i < len; i++) array[i] = f();
return array;
}
public static IList<T> Fill<T>(this IList<T> list, int count) where T : new() { return Fill(list, i => new T(), count); }
public static IList<T> Fill<T>(this IList<T> list, T value, int count) { return Fill(list, i => value, count); }
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)
public static ICollection<T> Fill<T>(this ICollection<T> list, int count) where T : new() { return Fill(list, count, () => new T()); }
public static ICollection<T> Fill<T>(this ICollection<T> list, int count, T value) { return Fill(list, count, () => value); }
public static ICollection<T> Fill<T>(this ICollection<T> list, int count, Func<T> f)
{
for (var i = 0; i < count; i++)
list.Add(f(i));
for (var i = 0; i < count; i++) list.Add(f());
return list;
}
#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
public static int IndexOf<T>(this IEnumerable<T> source, T value, Func<T, T, bool> comparer)
@ -46,58 +69,105 @@ namespace AdrianKousz.Util
return -1;
}
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)
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> f)
{
var i = 0;
foreach (var el in source) f(i++, el);
foreach (var el in source) f(el);
return source;
}
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;
}
// Linq: Select
public static IEnumerable<TResult> Map<T, TResult>(this IEnumerable<T> source, Func<T, TResult> f)
{
foreach (var el in source) yield return f(el);
var result = GetList<T, TResult>(source);
foreach (var el in source) result.Add(f(el));
return result;
}
// Linq: Where
public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> f)
{
foreach (var el in source) if (f(el)) yield return el;
var result = GetList<T, T>(source);
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)
{ 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)
{
var folded = initial;
foreach (var el in source) folded = f(folded, el);
return folded;
var result = initial;
foreach (var el in source) result = f(result, el);
return result;
}
// Linq: Aggregate
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");
T folded = en.Current;
while (en.MoveNext()) folded = f(folded, en.Current);
return folded;
T result = en.Current;
while (en.MoveNext()) result = f(result, en.Current);
return result;
}
}
#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)
{
using (var en1 = s1.GetEnumerator()) {
using (var en2 = s2.GetEnumerator()) {
while (en1.MoveNext() && en2.MoveNext()) {
yield return f(en1.Current, en2.Current);
}
}
var result = GetList<T1, TResult>(s1);
using (var en1 = s1.GetEnumerator())
using (var en2 = s2.GetEnumerator())
{
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
}
}