Skip to content

Instantly share code, notes, and snippets.

namespace Game
open System
open System.Diagnostics
module Cards =
type Suit = Clubs | Diamonds | Hearts | Spades
with
override x.ToString() =
match x with
@diegofrata
diegofrata / ByteConverter.cs
Created May 24, 2013 10:45
Casting a struct from a byte array using C-style unions and without the unsafe keyword. Yay!
[StructLayout(LayoutKind.Explicit)]
public struct ByteConverter<T> where T : struct
{
#region Private Members
private static int SIZE_OF = Marshal.SizeOf(typeof(T));
[FieldOffset(0)]
private byte[] _bytes;
@diegofrata
diegofrata / Validation.fs
Last active December 17, 2015 14:18
Validation Workflow
module RockSpot.Runtime.Validation
open Microsoft.FSharp.Quotations
open System
open System.Text
open FSharpx.Regex.Compiled
open FSharpx.Linq.QuotationEvaluation
type Test =
module Tests =
type User = { Name : string; Age : int }
//let user = { Name = "Diego"; Age = 26 }
let validate ux = validation ux {
required <@ ux.Name @>
minLength <@ ux.Name @> 3
adhoc (if ux.Name = "Diego" then Success else Failure "hahaha")
@diegofrata
diegofrata / ServiceStack.fs
Created May 4, 2013 13:38
Support for Option Types on ServiceStack
namespace RockSpot.Runtime
open System
open ServiceStack.Text
module ServiceStack =
let inline deserializeOption x =
match x with
| null -> None
@diegofrata
diegofrata / FSharpAop.fs
Last active March 22, 2016 07:46
SheepAspect + F# + INotifyPropertyChanged = Joy.
namespace FSharp.Windows
open System.ComponentModel
[<AbstractClass>]
type Model() =
let propertyChangedEvent = Event<_,_>()
interface INotifyPropertyChanged with
@diegofrata
diegofrata / DivRem.fs
Created February 1, 2013 21:00
F# DivRem Operator
namespace System.Numerics
[<AutoOpen>]
module DivRem =
type DivRem = DivRem with
static member (/%) (x : int32, DivRem) = fun y -> System.Math.DivRem(x, y)
static member (/%) (x : int64, DivRem) = fun y -> System.Math.DivRem(x, y)
static member (/%) (x : bigint, DivRem) = fun y -> BigInteger.DivRem(x, y)
let inline (/%) x y = (x /% DivRem) y
@diegofrata
diegofrata / QueryExtensions.fs
Created January 23, 2013 01:55
Extending QueryBuilder
// Author: Tomas Petricek (www.tomasp.net), Diego Frata
open System.Linq
// Extend the standard QueryBuilder type with an additional
// custom operation (that must be expressed in terms of other
// query operations) and marked with ReflectedDefinition
type Linq.QueryBuilder with
[<ReflectedDefinition; CustomOperation("exactlyOneOrNone")>]
member __.ExactlyOneOrNone (source : Linq.QuerySource<'T, 'U>) : 'T option =
query.ExactlyOneOrDefault(query.Select(source, fun x -> Some x))
@diegofrata
diegofrata / Repository.fs
Last active December 10, 2015 06:28 — forked from anonymous/Repository.fs
Very Simple MongoDB Repository for F#
open System.Collections
open System.Configuration
open System.Data.Entity.Design.PluralizationServices
open System.Linq
open FluentMongo.Linq
open MongoDB.Bson
open MongoDB.Driver
open MongoDB.Driver.Builders