Skip to content

Instantly share code, notes, and snippets.

<?xml version="1.0" encoding="utf-8"?>
<Edmx Version="3.0" xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
<Runtime>
<ConceptualModels>
<Schema Namespace="Test" Alias="Self" p4:UseStrongSpatialTypes="false" xmlns:p4="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="Contact">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" p4:StoreGeneratedPattern="Identity" />
@lasandell
lasandell / MappingTest.cs
Last active August 3, 2018 15:49
Code example to accompany my answer to this stackoverflow question: http://stackoverflow.com/questions/7424501/automapper-for-funcs-between-selector-types/7425211
using AutoMapper;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace MappingTest
{
class Cat
{
public string Name { get; set; }
@lasandell
lasandell / ExpressionArgumentMatcher.cs
Created April 10, 2013 17:03
Class to compare two lambda expressions so that methods taking expressions as arguments can be mocked with NSubstitute.
/// <summary>
/// Compare two expressions so that methods taking lambda
/// expressions as arguments can be mocked with NSubstitute.
/// <example>
/// _ordersRepository.Find(Expr.Is&lt;Order&gt;(o => o.Id == 2)).Returns(order2)
/// </example>
/// </summary>
public static class Expr
{
public static Expression<Func<TArg, bool>> Is<TArg>(Expression<Func<TArg, bool>> expression)
@lasandell
lasandell / distributed.md
Last active December 25, 2015 08:59
Erlang Camp Day 2

Distributed Erlang

Starting Nodes

erl -sname somename

erl -setcookie foo -sname somename

let (|Value|_|) value item =
if item = value
then Some()
else None
let x = 1
match [1] with
| [Value x] -> "match"
| _ -> "no match"
// I'm using lots of operating overloading voodoo to avoid
// having to specify static type constraints manually.
// I'm also using MemoryStream / BinaryReader which is probably
// slow but it was the easiest way to read values sequentially.
// It's currently just using the size of the receiving datatype
// rather than specifying it, though that could change.
open System
open System.IO
// Computation expression builder that uses a custom bind operation
// to allow binding a value to a name and yielding it as part of a
// list in one go.
type ListBuilder() =
member this.Bind(x, f) = x::f(x)
member this.Delay(f) = f()
member this.Combine(x, xs) = x @ xs
member this.Yield(x) = [x]
let list = ListBuilder()
// This was the idea. Unfortunately, it doesn't work. The expression expects every successive item to be
// the same type the first expression, ProvidedProperty. I can upcast them all to MemberInfo, but then they
// don't work as arguments to the next items in the expression.
let createType name (parameters:obj[]) =
let providedAsmPath = Path.Combine(config.TemporaryFolder, "FixedLengthTypes.dll")
let providedAsm = ProvidedAssembly(providedAsmPath)
let length = parameters.[0] :?> int
let ty = ProvidedTypeDefinition(asm, "FixedLengthTypes", name, Some typeof<obj>, IsErased = false)
ty.AddMembers(list {
yield ProvidedProperty("Length", typeof<int>, IsStatic = true,
@lasandell
lasandell / GuidLayout.cs
Last active August 29, 2015 13:56
Comparison of custom log4net Layout class I wrote in C# versus its F# equivalent.
public class GuidLayout : RawPropertyLayout
{
public override object Format(LoggingEvent loggingEvent)
{
var baseFormat = base.Format(loggingEvent);
if (baseFormat is Guid)
return baseFormat;
var guidString = baseFormat as string;
// Functor instances. Define Functor as a single case discriminated union
// to help with overload resolution.
type Functor = Functor with
static member fmap(Functor, mapping, option) = Option.map mapping option
static member fmap(Functor, mapping, list) = List.map mapping list
// Helper function to resolve Functor overload. Needed because we can't specify a concrete type
// such as Functor in a statically-resolved type parameter constraint, so we instead pass in an
// instance of Functor to resolve the ^o parameter. Also notice we have ^c and ^d instead of
// ^f< ^a > and ^f < ^b > because F# doesn't allow this.