Skip to content

Instantly share code, notes, and snippets.

I Δ(I) I Δ(I)
(0,⊥) (0,⊥) (1,⊥) (1,⊥)
(⊥,0) (⊥,0) (⊥,1) (⊥,1)
(0,0) (0,0) (1,1) (1,1)
(0,1) (0,0), (1,1) (1,0) (0,0), (1,1)
@eulerfx
eulerfx / unique-id-task-spec.md
Last active August 12, 2017 20:20
Unique-id Task Specification
I Δ(I)
(0,⊥,⊥) (0,⊥,⊥), (1,⊥,⊥), (2,⊥,⊥)
(⊥,0,⊥) (⊥,0,⊥), (⊥,1,⊥), (⊥,2,⊥)
(⊥,⊥,0) (⊥,⊥,0), (⊥,⊥,1), (⊥,⊥,2)
(0,0,⊥) (0,1,⊥), (1,0,⊥), (0,2,⊥), (2,0,⊥), (2,1,⊥), (1,2,⊥)
(0,⊥,0) (0,⊥,1), (1,⊥,0), (0,⊥,2), (2,⊥,0), (2,⊥,1), (1,⊥,2)
(⊥,0,0) (⊥,0,1), (⊥,1,0), (⊥,0,2), (⊥,2,0), (⊥,2,1), (⊥,1,2)
(0,0,0) (0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)
@eulerfx
eulerfx / opt-parse.fs
Created June 21, 2017 12:04
F# applicative CLI option parser
let readInt (s:string) =
match System.Int32.TryParse s with
| true,i -> Some i
| _ -> None
type Opt<'a> = Opt of name:string * defaultValue:'a option * read:(string -> 'a option)
with
static member Name<'a> (Opt(n,_,_) : Opt<'a>) = n
static member Read<'a> (Opt(_,_,r) : Opt<'a>) = r
static member Default<'a> (Opt(_,d,_) : Opt<'a>) = d
@eulerfx
eulerfx / pfp.fs
Created November 9, 2016 18:16
Probabilistic functional programming F#
/// A probability is a number between 0 and 1 inclusive.
type Prob = float
/// A distribution is a sequence of outcome-probability pairs such that the
/// probabilities sum to 1.
type Dist<'a> = D of seq<'a * Prob>
/// A spread takes a sequence of elements and assigns probabilities.
type Spread<'a> = 'a list -> Dist<'a>
@eulerfx
eulerfx / fad.fs
Last active May 14, 2016 15:53
F# automatic differentiation
/// A value together with its derivative.
[<NoComparison>]
type D<'a> = D of 'a * 'a
module D =
[<GeneralizableValue>]
let inline GenericTwo () =
LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne
@eulerfx
eulerfx / DVar.fs
Created May 14, 2016 13:07
DVar - dependent/dynamic variables
open System
open System.Threading
/// A dependant variable.
type DVar<'a> = private { cell : 'a ref ; event : Event<'a> }
/// Operations on dependant variables.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module DVar =
@eulerfx
eulerfx / Tcp.fs
Created May 14, 2016 09:14
F# sockets, framing, sessions
/// Operations on Berkley sockets.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Socket =
/// Executes an async socket operation.
let exec (alloc:unit -> SocketAsyncEventArgs, free:SocketAsyncEventArgs -> unit) (config:SocketAsyncEventArgs -> unit) (op:SocketAsyncEventArgs -> bool) (map:SocketAsyncEventArgs -> 'a) =
Async.FromContinuations <| fun (ok, error, _) ->
let args = alloc ()
@eulerfx
eulerfx / AsyncEvent.fs
Created February 6, 2015 14:55
AsyncEvent (an F# async based Rx)
/// An async observer.
type AsyncObserver<'a> = 'a option -> Async<unit>
module AsyncObserver =
let inline post a (obs:AsyncObserver<'a>) = obs (Some a)
let inline stop (obs:AsyncObserver<'a>) = obs None
let contramap (f:'b -> 'a) (o:AsyncObserver<'a>) : AsyncObserver<'b> =
@eulerfx
eulerfx / HListFoldish.fs
Last active August 29, 2015 14:14
F# HList unsafe fold
// for http://www.fssnip.net/d2
type Fold<'b> = Fold of f:('b -> obj -> 'b) * z:'b
with
static member inline ($) (Fold(_,z), HNil) = z
static member inline ($) (Fold(f,z), HCons(a,xs)) = f (Fold(f,z) |*| xs) a
// use
@eulerfx
eulerfx / HopacIoCh.fs
Created December 10, 2014 16:11
An F# Hopac input-output channel to ensure that inputs passed to a function are processed one at a time in fifo order
open Hopac
open Hopac.Infixes
open Hopac.Job.Infixes
open Hopac.Extensions
type IoCh<'i, 'o> = Ch<'i * IVar<'o>>
module IoCh =
let create (f:'i -> Job<'o>) : Job<IoCh<'i, 'o>> =