Skip to content

Instantly share code, notes, and snippets.

@badmotorfinger
badmotorfinger / gist:9618765
Last active August 29, 2015 13:57
Use LINQ query expressions without requiring IEnumerables, to compose functions.
// Full credit goes to Wes Dyer http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx
// Explanation of how query expressions are translated in to extension method syntax.
public class Identity<T>
{
public T Value { get; private set; }
public Identity(T value) { this.Value = value; }
public static implicit operator Identity<T>(T value) {
return new Identity<T>(value);
@badmotorfinger
badmotorfinger / SelectMany God
Last active August 29, 2015 13:58
Using the SelectMany() God function.
void Main()
{
// An implementation of the Where method using the SelectMany() method.
TestWhere();
// An implementation of Join using the SelectMany() method.
TestJoin();
}
public static IEnumerable<T> Where<T>(IEnumerable<T> stuffs, Func<T, bool> predicate)
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Bleroy.Helpers {
public static class NotNull {
public static TProp Get<TSource, TProp>(this TSource source, Expression<Func<TSource, TProp>> property) where TSource : class {
if (source == null) return default(TProp);
var current = property.Body;
@badmotorfinger
badmotorfinger / json-pretty
Created July 29, 2014 05:26
A compact JSON beautifier
// Highly compact JSON beautifier/formatter. Not very efficient and could definitely be improved. The main issue is with
// the number of allocations and memory presure being placed on the heap and garbage collector if the input is large.
// It is the shortest in terms of code size though.
// Source: http://stackoverflow.com/questions/4580397/json-formatter-in-c/24782322#24782322
private const string INDENT_STRING = " ";
static string FormatJson(string json) {
int indentation = 0;
@badmotorfinger
badmotorfinger / JoinedStreamReader.cs
Created October 30, 2011 00:18
A collection of streams that can be read as one continuous stream
namespace Mvc.ResourceLoader.Util {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
public class JoinedStreamReader : Stream {
@badmotorfinger
badmotorfinger / TryParseFunc.cs
Created July 18, 2012 03:38
Generic function to cast a string value to another value using .NET's built in TryParse methods.
// Usage
var intResult = Parse("1", 0, int.TryParse); // Returns 1
var dateResult = Parse("asdf", DateTime.Now, DateTime.TryParse); // Returns 18/07/2012 1:39:06 PM
delegate TParsedValue ParseFunc<T, U, TParsedValue>(T input, out U output);
static V Parse<T, V>(T valueToBeParsed, V defaultVal, ParseFunc<T, V, bool> dele) {
V result;
public static class TestHelpers
{
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue)
{
ShouldEqualWithDiff(actualValue, expectedValue, DiffStyle.Full, Console.Out);
}
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle)
{
ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out);
@badmotorfinger
badmotorfinger / gist:5972009
Last active December 19, 2015 14:49
A LINQ extension method which eagerly finds an item or items with the lowest value in the collection.
[Test]
public void TestEmpty()
{
CollectionAssert.AreEqual(
new int[0].MinsBy(i => i),
new int[0]);
}
[Test]
public void TestPreserveOrderAndValue()
// Only let input Observable fire every 'n' seconds at most
// but unlike Throttle, items fire immediately when they aren't
// rate-limited.
public IObservable<T> RateLimit<T>(this IObservable<T> This, TimeSpan interval, IScheduler scheduler)
{
var slot = default(IObservable<Unit>);
var input = This.Publish().RefCount();
return input.Window(input, _ => {
if (slot != null) return slot;
@badmotorfinger
badmotorfinger / Enable-CSharp-WarningsAsErrors.ps1
Last active June 15, 2017 06:44
Recursively processes all csproj files in a directory and adds/updates a node for both Release and Debug configurations to treat the specified C# warnings as errors
# Warnings to treat as errors
# See end of script for descriptions
$cSharpWarnings = '162,168,169,219,1717,0067,649,618,183,184,1060,1058,809,672,612,1522,465,1998'
$formatting = ',1570,1574'
$interopWarnings = ',626,684,824'
$casWarnings = ',688'
$warnings = $cSharpWarnings + $formatting + $interopWarnings + $casWarnings
Get-ChildItem *.csproj -Recurse | % {