Skip to content

Instantly share code, notes, and snippets.

@mausch
mausch / Program.cs
Last active August 29, 2015 14:02
Catching/reifying exceptions in an IEnumerable
using System;
using System.Collections.Generic;
using System.Linq;
/*
Closest things I found are http://stackoverflow.com/q/3835633 or http://stackoverflow.com/q/7188623
but the solutions there either handle exceptions with a side effect (an Action<Exception>) or just filter out the exceptions or are limited in other ways.
This also continues enumeration after exceptions until the end of the IEnumerable, accumulating both values and exceptions.
It's useful to wrap sequences modeling potentially partial lazy computations.
@mausch
mausch / gist:29e643138f8af2ff1ef5
Created June 25, 2014 15:54
Applying a lifestyle to a collection of Windsor component registrations
using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.ModelBuilder.Descriptors;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
module String =
open System.Text.RegularExpressions
///Makes a string suitable for use as part of a URL:
///Removes syntax, replaces spaces with dashes and lowers case,
///then trims first word after 30 chars
let prettify (x:string) =
let parsed = Regex.Replace(x.Trim(),"[^0-9a-zA-Z ]","").Replace(" ","-").ToLower()
using System;
public abstract class Term {
private Term() { }
public abstract T Match<T>(Func<Var, T> var, Func<App, T> app, Func<Fun, T> fun);
// TODO: equality, ToString()
/// <summary>
Module Module1
Sub Foo(Of T)(xs As T(), f As Func(Of T, Boolean))
End Sub
Sub Main()
Foo({1}, Function(x) True)
End Sub
End Module
@mausch
mausch / gist:ad4e1c4173451513eadd
Created July 24, 2014 04:01
Generate ADO.NET parameters automatically in a type-safe way.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
class Program {
static void Main(string[] args) {
var s = new Sql("SELECT * FROM TABLE WHERE Id = ") + Sql.P(1) + " and Name = " + Sql.P("John Doe");

Given a big domain of types that don't implement IEquatable, e.g (simplified here):

class A {
	public int a;
	public string b;
	public IEnumerable<int?> c;
	public IEnumerable<B> d;
}
using System;
using System.Linq;
static class Program {
static void Main(string[] args) {
var a = new int?[] { 1, 2, null, 4 };
{
// "Where" here does not change the output type. Pass it to some other function and the information about this filter is lost,
// i.e. "You made no reusable note of what you did." ( http://www.hxa.name/notes/note-hxa7241-20131124T0927Z.html )
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var source = new int[0];
// throws InvalidOperationException, not documented in http://msdn.microsoft.com/en-us/library/bb534962.aspx . Should not even compile as AStruct doesn't implement IComparable.
using System;
static class L {
public static Func<T> F<T>(Func<T> f) { return f; }
// add overloads for other arities
}
class Program {
static void Main(string[] args) {
int i = L.F(() => 0)();