Skip to content

Instantly share code, notes, and snippets.

View cameronsjo's full-sized avatar

Cameron Sjo cameronsjo

View GitHub Profile
@cameronsjo
cameronsjo / LinqPad.FileIo
Created April 24, 2015 20:27
LinqPad FileIO Extensions
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)));
}
public static void TimedTest(Action action, string message = "", int decimalPlaces = 1)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
action.Invoke();
stopwatch.Stop();
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());
}
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))
<Query Kind="Program" />
void Main()
{
var file = FileIO.GetFile();
var assembly = Assembly.LoadFile(file);
var types = new Dictionary<int, dynamic>();
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);
/// <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>
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);
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
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>();