Compare commits

...

3 Commits

Author SHA1 Message Date
Adrian 1bed96f1fe Add ForBoth 2016-04-26 15:13:59 +02:00
Adrian d10d5c4805 Add list init methods 2016-04-25 16:37:36 +02:00
Adrian 29eb9d5fa0 DateTime double and int extension methods 2016-04-23 02:38:44 +02:00
3 changed files with 41 additions and 33 deletions

4
doc/csc2.bat 100644
View File

@ -0,0 +1,4 @@
:: 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,11 +6,18 @@ namespace AdrianKousz.Util
{ {
private static readonly DateTime Epoch = new DateTime(1970, 1, 1); private static readonly DateTime Epoch = new DateTime(1970, 1, 1);
public static DateTime FromUnixTime(double timestamp) { public static DateTime ToDateTime(this int timestamp)
{
return Epoch.AddSeconds(timestamp); return Epoch.AddSeconds(timestamp);
} }
public static double ToUnixTime(this DateTime t) { public static DateTime ToDateTime(this double timestamp)
{
return Epoch.AddSeconds(timestamp);
}
public static double ToUnixTime(this DateTime t)
{
return t.Subtract(Epoch).TotalSeconds; 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 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 TColl Fill<T, TColl>(this TColl list, int count, T value) where TColl : ICollection<T> { return Fill(list, count, () => value); }
public static ICollection<T> Fill<T>(this ICollection<T> list, int count, Func<T> f) public static TColl Fill<T, TColl>(this TColl list, int count, Func<T> f) where TColl : ICollection<T>
{ {
for (var i = 0; i < count; i++) list.Add(f()); for (var i = 0; i < count; i++) list.Add(f());
return list; return list;
@ -29,21 +29,27 @@ namespace AdrianKousz.Util
#endregion #endregion
#region Anonymous Type Helpers #region Type Helpers
public static T[] CreateArray<T>(this T example, int count) public static T[] CreateArray<T>(this T example, int count)
{ {
return new T[count]; return new T[count];
} }
public static List<T> CreateList<T>(this T example) public static List<T> CreateList<T>(this T example, int count = 12)
{ {
return new List<T>(); return new List<T>(count);
} }
#endregion public static T[] NewArray<T>(this IEnumerable<T> source, int count)
{
return new T[count];
}
#region Materializers public static List<T> NewList<T>(this IEnumerable<T> source, int count = 12)
{
return new List<T>(count);
}
public static List<T> ToList<T>(this IEnumerable<T> source) public static List<T> ToList<T>(this IEnumerable<T> source)
{ {
@ -69,19 +75,26 @@ namespace AdrianKousz.Util
return -1; return -1;
} }
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> f) public static void ForEach<T>(this IEnumerable<T> source, Action<T> f)
{ {
foreach (var el in source) f(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) public static void ForEach<K, V>(this IDictionary<K, V> source, Action<K, V> f)
{ {
foreach (var el in source) f(el.Key, el.Value); foreach (var el in source) f(el.Key, el.Value);
return source;
} }
// Linq: Select 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);
}
}
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); var result = GetList<T, TResult>(source);
@ -89,7 +102,6 @@ namespace AdrianKousz.Util
return result; return result;
} }
// 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); var result = GetList<T, T>(source);
@ -106,31 +118,21 @@ namespace AdrianKousz.Util
return result; return result;
} }
// 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; var result = en.Current;
while (en.MoveNext()) result = f(result, en.Current); while (en.MoveNext()) result = f(result, en.Current);
return result; return result;
} }
} }
#endregion
#region Zippers
public static IDictionary<K, V> ToDictionary<K, V>(this IEnumerable<K> ks, IEnumerable<V> vs) public static IDictionary<K, V> ToDictionary<K, V>(this IEnumerable<K> ks, IEnumerable<V> vs)
{ {
var result = new Dictionary<K, V>(); var result = new Dictionary<K, V>();
using (var en1 = ks.GetEnumerator()) ForBoth(ks, vs, (k, v) => { result[k] = v; });
using (var en2 = vs.GetEnumerator())
{
while (en1.MoveNext() && en2.MoveNext())
result[en1.Current] = en2.Current;
}
return result; return result;
} }
@ -148,12 +150,7 @@ namespace AdrianKousz.Util
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); var result = GetList<T1, TResult>(s1);
using (var en1 = s1.GetEnumerator()) ForBoth(s1, s2, (e1, e2) => { result.Add(f(e1, e2)); });
using (var en2 = s2.GetEnumerator())
{
while (en1.MoveNext() && en2.MoveNext())
result.Add(f(en1.Current, en2.Current));
}
return result; return result;
} }