Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / gist:3497131
Created August 28, 2012 10:47
Unlambda
// http://www.madore.org/~david/programs/unlambda/
type C = F -> unit
and F = F of ((F * C) -> unit)
let getReader (source : string) =
let pos = ref 0
(fun () -> let result = source.[!pos] in pos := !pos + 1; result)
let eval (source : string) =
@palladin
palladin / gist:3737214
Created September 17, 2012 13:18
Monadic Trampoline
type TrampValue<'T> =
| DelayValue of Delay<'T>
| ReturnValue of Return<'T>
| BindValue of IBind<'T>
and ITramp<'T> =
abstract member Value : TrampValue<'T>
abstract member Run : unit -> 'T
and Delay<'T>(f : unit -> ITramp<'T>) =
member self.Func = f
@palladin
palladin / gist:3761004
Created September 21, 2012 11:39
Async.Choice
open System
[<AutoOpen>]
module AsyncEx =
type private SuccessException<'T>(value : 'T) =
inherit Exception()
member self.Value = value
type Microsoft.FSharp.Control.Async with
// efficient raise
@palladin
palladin / gist:3765785
Created September 22, 2012 10:35
raise vs Async.Raise
type Microsoft.FSharp.Control.Async with
static member Raise (e : #exn) = Async.FromContinuations(fun (_,econt,_) -> econt e)
#time
for i in [|1..1000000|] do
try
raise <| new System.Exception()
with ex -> ()
async {
@palladin
palladin / gist:4421522
Created December 31, 2012 17:43
Church numerals
type Church =
abstract Apply<'T> : ('T -> 'T) * 'T -> 'T
let zero =
{
new Church with
member self.Apply(f, x) = x
}
let succ (nat : Church) =
@palladin
palladin / gist:4524544
Created January 13, 2013 15:15
Type Safe Higher-order abstract syntax via GADT encoding
type Expr<'T> =
abstract Eval : unit -> 'T
let eval (x : Expr<'T>) = x.Eval()
let lift (value : 'T) =
{ new Expr<'T> with
member self.Eval () = value
}
let tup (first : Expr<'T>) (second : Expr<'S>) =
@palladin
palladin / gist:5076013
Created March 3, 2013 13:00
GC Friendly Fixpoint
#time
let rec Y f x = f (Y f) x
let Y' f x =
let r = ref Unchecked.defaultof<'a -> 'b>
r := (fun x -> f !r x)
f !r x
let iter f x = if x = 100000000 then x else f (x + 1)
@palladin
palladin / gist:5182248
Created March 17, 2013 16:20
Generic Collections, Type Classes and friends
// http://lampwww.epfl.ch/~odersky/papers/fsttcs2009.pdf
open System
open System.Text
open System.Collections.Generic
// Type classes encoding for iteration and building
type Iterable<'From, 'Elem> = Iterable of (('Elem -> unit) -> unit)
type Iter = Iter with
static member inline ($) (_ : Iter, values : 'Elem []) =
Iterable (fun f ->
@palladin
palladin / gist:5484150
Created April 29, 2013 19:40
Loop Unrolling
type Peano = interface end
and Zero = Zero with
static member inline (|*|) (f, Zero) = f $ Zero
interface Peano
and Succ<'a when 'a :> Peano> = Succ of 'a with
static member inline (|*|) (f, Succ(x)) = f $ Succ(x)
interface Peano
type PeanoToInt = PeanoToInt with
// http://clojure.com/blog/2012/05/08/reducers-a-library-and-model-for-collection-processing.html
module Reducer =
open System
open System.Text
open System.Collections.Generic
type ReduceFunc<'T,'R> =
abstract Invoke : 'T * 'R -> 'R