This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class FileIO | |
| { | |
| private static string LastDirectoryKey = "extensions.fileio.lastdirectory"; | |
| public static IEnumerable<string> GetFilesRecursivelyFromDirectory(string directory) | |
| { | |
| return Directory.EnumerateFiles(directory).Concat( | |
| Directory.EnumerateDirectories(directory) | |
| .SelectMany(subdir => GetFilesRecursivelyFromDirectory(subdir))); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void TimedTest(Action action, string message = "", int decimalPlaces = 1) | |
| { | |
| var stopwatch = new Stopwatch(); | |
| stopwatch.Start(); | |
| action.Invoke(); | |
| stopwatch.Stop(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static string GenerateRandomString(int length = 10) | |
| { | |
| const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz"; | |
| var r = new Random(); | |
| return new string(Enumerable.Repeat(alphabet, length) | |
| .Select(s => s[r.Next(s.Length)]) | |
| .ToArray()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static double StdDev<T>(this IEnumerable<T> list, Func<T, double> values) | |
| { | |
| // Reference: http://stackoverflow.com/questions/2253874/linq-equivalent-for-standard-deviation | |
| // Reference: http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/ | |
| var mean = 0.0; | |
| var sum = 0.0; | |
| var stdDev = 0.0; | |
| var n = 0; | |
| foreach (var value in list.Select(values)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Query Kind="Program" /> | |
| void Main() | |
| { | |
| var file = FileIO.GetFile(); | |
| var assembly = Assembly.LoadFile(file); | |
| var types = new Dictionary<int, dynamic>(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static Dictionary<TKey, TValue> ToReverseDictionary<TModel, TKey, TValue>(this IEnumerable<TModel> collection, | |
| Func<TModel, IEnumerable<TKey>> keySelector, | |
| Func<TModel, TValue> valueSelector) | |
| { | |
| var dictionary = new Dictionary<TKey,TValue>(); | |
| foreach (var item in collection) | |
| { | |
| var value = valueSelector.Invoke(item); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Compares an object against two other objects (upper and lower bound objects) to see if the value is between them. | |
| /// </summary> | |
| /// <typeparam name="T">Type of the object</typeparam> | |
| /// <param name="obj">Object for value to be compared against.</param> | |
| /// <param name="lower">Lower bound of the comparison</param> | |
| /// <param name="upper">Upper bound of the comparison</param> | |
| /// <param name="options">Options for performing the between</param> | |
| /// <returns>Returns true if the value is between the two bounds.</returns> | |
| public static bool Between<T>(this T obj, T lower, T upper, BetweenOptions options = BetweenOptions.Default) where T : IComparable<T> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static DateTime Truncate(this DateTime date, DateTimeResolution resolution = DateTimeResolution.Hour) | |
| { | |
| switch (resolution) | |
| { | |
| case DateTimeResolution.Year: | |
| return new DateTime(date.Year, 1, 1); | |
| case DateTimeResolution.Month: | |
| return new DateTime(date.Year, date.Month, 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Configuration; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Web; | |
| using System.Web.Http; | |
| using System.Web.Http.Controllers; | |
| namespace Filters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public IEnumerable<string> GetRanges(IEnumerable<int> values) | |
| { | |
| // Get the distinct sorted values. | |
| var sorted = values.Distinct() | |
| .OrderBy(v => v) | |
| .ToArray(); | |
| // We use a hashset, because it's the fastest way to see if a value has already been consumed. | |
| var used = new HashSet<int>(); | |
| var output = new List<string>(); |
OlderNewer