From 29eb9d5fa0ba9e8a54afc342d6cf91cc3ad1b843 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sat, 23 Apr 2016 02:38:44 +0200 Subject: [PATCH 1/3] DateTime double and int extension methods --- doc/csc2.bat | 4 ++++ src/main/AdrianKousz.Util/DateTimes.cs | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 doc/csc2.bat diff --git a/doc/csc2.bat b/doc/csc2.bat new file mode 100644 index 0000000..791a452 --- /dev/null +++ b/doc/csc2.bat @@ -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" %* diff --git a/src/main/AdrianKousz.Util/DateTimes.cs b/src/main/AdrianKousz.Util/DateTimes.cs index 3fad687..7777c85 100644 --- a/src/main/AdrianKousz.Util/DateTimes.cs +++ b/src/main/AdrianKousz.Util/DateTimes.cs @@ -6,11 +6,18 @@ namespace AdrianKousz.Util { 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); } - 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; } } From d10d5c4805718bba75dc709a9bebc58731df1848 Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 25 Apr 2016 16:37:36 +0200 Subject: [PATCH 2/3] Add list init methods --- src/main/AdrianKousz.Util/Enumerables.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/AdrianKousz.Util/Enumerables.cs b/src/main/AdrianKousz.Util/Enumerables.cs index 993d8e2..3bf7802 100644 --- a/src/main/AdrianKousz.Util/Enumerables.cs +++ b/src/main/AdrianKousz.Util/Enumerables.cs @@ -20,8 +20,8 @@ namespace AdrianKousz.Util } public static ICollection Fill(this ICollection list, int count) where T : new() { return Fill(list, count, () => new T()); } - public static ICollection Fill(this ICollection list, int count, T value) { return Fill(list, count, () => value); } - public static ICollection Fill(this ICollection list, int count, Func f) + public static TColl Fill(this TColl list, int count, T value) where TColl : ICollection { return Fill(list, count, () => value); } + public static TColl Fill(this TColl list, int count, Func f) where TColl : ICollection { for (var i = 0; i < count; i++) list.Add(f()); return list; @@ -36,9 +36,19 @@ namespace AdrianKousz.Util return new T[count]; } - public static List CreateList(this T example) + public static List CreateList(this T example, int count = 12) { - return new List(); + return new List(count); + } + + public static T[] NewArray(this IEnumerable source, int count) + { + return new T[count]; + } + + public static List NewList(this IEnumerable source, int count = 12) + { + return new List(count); } #endregion From 1bed96f1fe5a78c5dad492ab4a22cf870343584c Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 26 Apr 2016 15:13:59 +0200 Subject: [PATCH 3/3] Add ForBoth --- src/main/AdrianKousz.Util/Enumerables.cs | 45 +++++++++--------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/main/AdrianKousz.Util/Enumerables.cs b/src/main/AdrianKousz.Util/Enumerables.cs index 3bf7802..7898561 100644 --- a/src/main/AdrianKousz.Util/Enumerables.cs +++ b/src/main/AdrianKousz.Util/Enumerables.cs @@ -29,7 +29,7 @@ namespace AdrianKousz.Util #endregion - #region Anonymous Type Helpers + #region Type Helpers public static T[] CreateArray(this T example, int count) { @@ -51,10 +51,6 @@ namespace AdrianKousz.Util return new List(count); } - #endregion - - #region Materializers - public static List ToList(this IEnumerable source) { return source as List ?? new List(source); @@ -79,19 +75,26 @@ namespace AdrianKousz.Util return -1; } - public static IEnumerable ForEach(this IEnumerable source, Action f) + public static void ForEach(this IEnumerable source, Action f) { foreach (var el in source) f(el); - return source; } - public static IDictionary ForEach(this IDictionary source, Action f) + public static void ForEach(this IDictionary source, Action f) { foreach (var el in source) f(el.Key, el.Value); - return source; } - // Linq: Select + public static void ForBoth(this IEnumerable s1, IEnumerable s2, Action f) + { + using (var en1 = s1.GetEnumerator()) + using (var en2 = s2.GetEnumerator()) + { + while (en1.MoveNext() && en2.MoveNext()) + f(en1.Current, en2.Current); + } + } + public static IEnumerable Map(this IEnumerable source, Func f) { var result = GetList(source); @@ -99,7 +102,6 @@ namespace AdrianKousz.Util return result; } - // Linq: Where public static IEnumerable Filter(this IEnumerable source, Func f) { var result = GetList(source); @@ -116,31 +118,21 @@ namespace AdrianKousz.Util return result; } - // Linq: Aggregate public static T FoldL1(this IEnumerable source, Func f) { using (var en = source.GetEnumerator()) { if (!en.MoveNext()) throw new InvalidOperationException("No elements"); - T result = en.Current; + var result = en.Current; while (en.MoveNext()) result = f(result, en.Current); return result; } } - #endregion - - #region Zippers - public static IDictionary ToDictionary(this IEnumerable ks, IEnumerable vs) { var result = new Dictionary(); - using (var en1 = ks.GetEnumerator()) - using (var en2 = vs.GetEnumerator()) - { - while (en1.MoveNext() && en2.MoveNext()) - result[en1.Current] = en2.Current; - } + ForBoth(ks, vs, (k, v) => { result[k] = v; }); return result; } @@ -158,12 +150,7 @@ namespace AdrianKousz.Util public static IEnumerable Zip(this IEnumerable s1, IEnumerable s2, Func f) { var result = GetList(s1); - using (var en1 = s1.GetEnumerator()) - using (var en2 = s2.GetEnumerator()) - { - while (en1.MoveNext() && en2.MoveNext()) - result.Add(f(en1.Current, en2.Current)); - } + ForBoth(s1, s2, (e1, e2) => { result.Add(f(e1, e2)); }); return result; }