Skip to content

Instantly share code, notes, and snippets.

View Porges's full-sized avatar
🏠
Working from home

George Pollard Porges

🏠
Working from home
View GitHub Profile
@Porges
Porges / num.fs
Created February 23, 2015 21:42
why doesn't this work?
type Num<'a> = {
add : 'a -> 'a -> 'a;
subtract : 'a -> 'a -> 'a;
multiply : 'a -> 'a -> 'a;
divide : 'a -> 'a -> 'a;
}
type System.Int32 with
static member NumInstance () =
{
@Porges
Porges / equality.hs
Created February 27, 2015 22:10
Data.Type.Equality
{-# LANGUAGE TypeOperators, ConstraintKinds, TypeFamilies #-}
import Data.Type.Equality
-- if we have two functions
-- a -> b
-- a' -> b'
-- and a proof that the function types are the same
-- then b must equal b'
proof1 ::
let a = fun b -> b // ok : ('a -> 'a) * int
, 2
let b = fun b -> b // syntax error
, 2
let c = fun b -> b // syntax error
, 2
let d = fun b -> b // syntax error
@Porges
Porges / atomicappend.cs
Last active January 11, 2016 20:01
possible framework bug?
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
namespace AtomicAppend
{
@Porges
Porges / typetree.cs
Created March 12, 2015 19:30
Something fun you can do with overlapping interfaces...
using System;
using System.Collections.Generic;
namespace TypeTree
{
public interface IGet<out T>
{
T Get { get; }
}
// direct-ish translation
let allOrNone (xs : 'a option list) : 'a list option =
List.foldBack
(fun x st -> x |> Option.bind (fun x -> st |> Option.bind (fun st -> Some (x::st))))
xs
(Some [])
// shorter
let allOrNone2 (xs : 'a option list) : 'a list option =
List.foldBack
@Porges
Porges / once.fs
Last active August 29, 2015 14:17
open System
open System.Threading
type OnceLazy (action : unit -> unit) =
let it = Lazy(action, LazyThreadSafetyMode.ExecutionAndPublication)
member this.Force() = it.Force()
type Once (action : unit -> unit) =
let mutable performed = 0
member this.Force() =
// First, the validation type (applicative only):
type Validation<'e, 't> =
| ErrorCollection of 'e list
| Validated of 't
let (<*>) (f : Validation<'e, 'a -> 'b>) (v : Validation<'e, 'a>) : Validation<'e, 'b> =
match f, v with
| ErrorCollection e1, ErrorCollection e2 -> ErrorCollection (List.append e1 e2)
| ErrorCollection e1, _ -> ErrorCollection e1
| _, ErrorCollection e2 -> ErrorCollection e2
internal struct TableIndex
{
public readonly byte Value;
public TableIndex(byte value)
{
Value = value;
}
}
[StructLayout(LayoutKind.Explicit)]
public struct IntOrDouble
{
private enum Type : byte
{
Integral,
Floating
}
[FieldOffset(0)]