Change Fillers in Enumerables
* Fillers should not get a value from the array * Add Fillers for lists * FoldL for functions without return values * Add stream copy compatibility function
This commit is contained in:
parent
d74b04d447
commit
69a8cbac86
2 changed files with 29 additions and 9 deletions
src/main/AdrianKousz.Util
|
@ -10,21 +10,28 @@ namespace AdrianKousz.Util
|
|||
{
|
||||
#region Fillers
|
||||
|
||||
public static T[] Fill<T>(this T[] array) where T : new() { return Fill(array, (i, x) => new T()); }
|
||||
public static T[] Fill<T>(this T[] array, T value) { return Fill(array, (i, x) => value); }
|
||||
public static T[] Fill<T>(this T[] array, Func<int, T> f) { return Fill(array, (i, x) => f(i)); }
|
||||
public static T[] Fill<T>(this T[] array, Func<T> f) { return Fill(array, (i, x) => f()); }
|
||||
public static T[] Fill<T>(this T[] array, Func<T, T> f) { return Fill(array, (i, x) => f(x)); }
|
||||
|
||||
public static T[] Fill<T>(this T[] array, Func<int, T, T> f)
|
||||
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)
|
||||
{
|
||||
var min = array.GetLowerBound(0);
|
||||
var max = array.GetUpperBound(0);
|
||||
for (var i = min; i <= max; i++)
|
||||
array[i] = f(i, array[i]);
|
||||
array[i] = f(i);
|
||||
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)
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
list.Add(f(i));
|
||||
return list;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Functional Classics
|
||||
|
@ -39,10 +46,12 @@ namespace AdrianKousz.Util
|
|||
return -1;
|
||||
}
|
||||
|
||||
public static void ForEach<T>(this IEnumerable<T> source, Action<int, 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)
|
||||
{
|
||||
var i = 0;
|
||||
foreach (var el in source) f(i++, el);
|
||||
return source;
|
||||
}
|
||||
|
||||
// Linq: Select
|
||||
|
@ -57,6 +66,8 @@ namespace AdrianKousz.Util
|
|||
foreach (var el in source) if (f(el)) yield return el;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -42,6 +42,15 @@ namespace AdrianKousz.Util
|
|||
return new StreamWriter(s);
|
||||
}
|
||||
|
||||
public static void CopyTo(this Stream sin, Stream sout) {
|
||||
var bufferlength = 10240;
|
||||
var buffer = new byte[bufferlength];
|
||||
var read = 0;
|
||||
while ((read = sin.Read(buffer, 0, bufferlength)) > 0) {
|
||||
sout.Write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool ParseBoolean(string s)
|
||||
{
|
||||
s = s.Trim().ToLower();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue