Compare commits

..

No commits in common. "1bed96f1fe5a78c5dad492ab4a22cf870343584c" and "2b0d56b1a2d2abd2f5279b4a8712b7d9d61c8e97" have entirely different histories.

3 changed files with 33 additions and 41 deletions

View File

@ -1,4 +0,0 @@
:: Compile bare .NET 2 with C# 4
@echo off
SET CSC=C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc
%CSC% /nostdlib /noconfig /nowin32manifest "/lib:C:\Windows\Microsoft.NET\Framework\v2.0.50727" "/r:mscorlib.dll" %*

View File

@ -6,18 +6,11 @@ namespace AdrianKousz.Util
{
private static readonly DateTime Epoch = new DateTime(1970, 1, 1);
public static DateTime ToDateTime(this int timestamp)
{
public static DateTime FromUnixTime(double timestamp) {
return Epoch.AddSeconds(timestamp);
}
public static DateTime ToDateTime(this double timestamp)
{
return Epoch.AddSeconds(timestamp);
}
public static double ToUnixTime(this DateTime t)
{
public static double ToUnixTime(this DateTime t) {
return t.Subtract(Epoch).TotalSeconds;
}
}

View File

@ -20,8 +20,8 @@ namespace AdrianKousz.Util
}
public static ICollection<T> Fill<T>(this ICollection<T> list, int count) where T : new() { return Fill(list, count, () => new T()); }
public static TColl Fill<T, TColl>(this TColl list, int count, T value) where TColl : ICollection<T> { return Fill(list, count, () => value); }
public static TColl Fill<T, TColl>(this TColl list, int count, Func<T> f) where TColl : ICollection<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());
return list;
@ -29,27 +29,21 @@ namespace AdrianKousz.Util
#endregion
#region Type Helpers
#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, int count = 12)
public static List<T> CreateList<T>(this T example)
{
return new List<T>(count);
return new List<T>();
}
public static T[] NewArray<T>(this IEnumerable<T> source, int count)
{
return new T[count];
}
#endregion
public static List<T> NewList<T>(this IEnumerable<T> source, int count = 12)
{
return new List<T>(count);
}
#region Materializers
public static List<T> ToList<T>(this IEnumerable<T> source)
{
@ -75,26 +69,19 @@ namespace AdrianKousz.Util
return -1;
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T> f)
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> f)
{
foreach (var el in source) f(el);
return source;
}
public static void ForEach<K, V>(this IDictionary<K, V> source, Action<K, V> f)
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;
}
public static void ForBoth<T1, T2>(this IEnumerable<T1> s1, IEnumerable<T2> s2, Action<T1, T2> f)
{
using (var en1 = s1.GetEnumerator())
using (var en2 = s2.GetEnumerator())
{
while (en1.MoveNext() && en2.MoveNext())
f(en1.Current, en2.Current);
}
}
// Linq: Select
public static IEnumerable<TResult> Map<T, TResult>(this IEnumerable<T> source, Func<T, TResult> f)
{
var result = GetList<T, TResult>(source);
@ -102,6 +89,7 @@ namespace AdrianKousz.Util
return result;
}
// Linq: Where
public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> f)
{
var result = GetList<T, T>(source);
@ -118,21 +106,31 @@ namespace AdrianKousz.Util
return result;
}
// Linq: Aggregate
public static T FoldL1<T>(this IEnumerable<T> source, Func<T, T, T> f)
{
using (var en = source.GetEnumerator())
{
if (!en.MoveNext()) throw new InvalidOperationException("No elements");
var result = en.Current;
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>();
ForBoth(ks, vs, (k, v) => { result[k] = v; });
using (var en1 = ks.GetEnumerator())
using (var en2 = vs.GetEnumerator())
{
while (en1.MoveNext() && en2.MoveNext())
result[en1.Current] = en2.Current;
}
return result;
}
@ -150,7 +148,12 @@ namespace AdrianKousz.Util
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);
ForBoth(s1, s2, (e1, e2) => { result.Add(f(e1, e2)); });
using (var en1 = s1.GetEnumerator())
using (var en2 = s2.GetEnumerator())
{
while (en1.MoveNext() && en2.MoveNext())
result.Add(f(en1.Current, en2.Current));
}
return result;
}