Skip to content

Instantly share code, notes, and snippets.

using System;
using Autofac;
using Newtonsoft.Json.Serialization;
/// Allows constructor dependencies to be resolved when deserializing JSON.NET instances.
public class DependencyContractResolver : DefaultContractResolver
{
/// But what if I wanted to extend CamelCasePropertyNamesContractResolver instead of
/// DefaultContractResolver in some cases? No way to switch it out without delegation.
Set-WindowsExplorerOptions -EnableShowHiddenFilesFoldersDrives -EnableShowProtectedOSFiles -EnableShowFileExtensions
Enable-RemoteDesktop
cinst fiddler4
cinst git-credential-winstore
cinst console-devel
cinst sublimetext2
cinst poshgit
cinst dotpeek
@lasandell
lasandell / ActivePatterns.fs
Last active August 29, 2015 14:27
Parsing DBus type signatures. Comparing FParsec solution to an earlier solution using active patterns and regular pattern matching. More info on what I'm parsing: http://dbus.freedesktop.org/doc/dbus-specification.html#type-system
[<RequireQualifiedAccess>]
type DBusType =
| Byte
| Boolean
| Int16
| UInt16
| Int32
| UInt32
| Int64
| UInt64
open System
type Disposable() =
do printfn "Constrcuted!"
interface IDisposable with
member x.Dispose() = printfn "Disposed!"
let sequence =
seq {
use disposable = new Disposable()
@lasandell
lasandell / pongce.fsx
Last active August 29, 2015 14:23
F# Ping Pong Computation Expression!
// State types
type Ping = Ping
type Pong = Pong
// Builder that requires every "ping" to be
// immediately answered by a "pong"
type PingBuilder() =
member __.Yield(()) = Pong
member __.Run(Pong) = ()
@lasandell
lasandell / rpnce.fsx
Last active August 29, 2015 14:23
F# RPN Calculator Computation Expression Builder!
type CalcBuilder() =
member __.Yield(()) = ()
member __.Run((x, ())) = x
[<CustomOperation("push")>]
member __.Push(rest, x) = (x, rest)
[<CustomOperation("add")>]
member __.Add((y, (x, rest))) = (x + y, rest)
@lasandell
lasandell / upcast.fsx
Last active August 29, 2015 14:09
The static coercion from type EventInfoProxy to 'a involves an indeterminate type based on information prior to this program point. Static coercions are not allowed on some types. Further type annotations are needed.
let annotateMember data (memb:'t :> #MemberInfo) =
match box memb with
| :? EventInfo as event ->
{ new EventInfoProxy(event) with
override __.GetCustomAttributesData() = data } :> 't
| :? MethodInfo as meth ->
{ new MethodInfoProxy(meth) with
override __.GetCustomAttributesData() = data } :> 't
| :? PropertyInfo as prop ->
{ new PropertyInfoProxy(prop) with
@lasandell
lasandell / json.fsx
Last active August 29, 2015 14:07
Simple JSON renderer. It doesn't do indentation nor does it have all the types. Feel free to extend.
type JObject = JProperty list
and JProperty = string * JValue
and JValue =
| JString of string
| JInt of int
| JObject of JObject
| JArray of JValue list
let rec renderJson jObject =
let quote value = sprintf "\"%s\"" value
// 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.
@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;