Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / gist:1293387
Created October 17, 2011 18:32
Monadic Retry
open System
open System.Threading
type ShouldRetry = ShouldRetry of (RetryCount * LastException -> bool * RetryDelay)
and RetryCount = int
and LastException = exn
and RetryDelay = TimeSpan
type RetryPolicy = RetryPolicy of ShouldRetry
type RetryPolicies() =
@palladin
palladin / gist:1451796
Created December 9, 2011 14:46
Polyvariadic fixpoint
// http://okmij.org/ftp/Computation/fixed-point-combinators.html
let force (value : Lazy<_>) = value.Force()
let fix f = let rec x = lazy (f x) in x
let fix' (fs : list<Lazy<list<'a -> 'b>> -> 'a -> 'b>) : Lazy<list<'a -> 'b>> =
fix (fun r -> fs |> List.map (fun f -> f r))
let fe l x =
let [e; o] = force l
@palladin
palladin / gist:1583661
Created January 9, 2012 16:22
n-ary Seq.map
let (<*>) fs xs = Seq.map2 (fun f x -> f x) fs xs
let map3 f xs bs cs = Seq.map2 f xs bs <*> cs
let map4 f xs bs cs ds = map3 f xs bs cs <*> ds
map4 (fun x b c d -> x + b + c + d) [1;2] [1;2] [1;2] [1;2] // [4; 8]
@palladin
palladin / gist:1634138
Created January 18, 2012 17:11
n-ary Seq.map (Numerals)
// For more info: ftp://ftp.cs.au.dk/pub/BRICS/RS/01/10/BRICS-RS-01-10.pdf
let (<*>) fs xs = Seq.map2 (fun f x -> f x) fs xs
let succ n fs xs = n (fs <*> xs)
let map n f = n (Seq.initInfinite (fun _ -> f))
// Numerals
let ``1``<'a1, 'r> : seq<('a1 -> 'r)> -> seq<'a1> -> seq<'r> =
succ id
let ``2``<'a1, 'a2, 'r> : seq<('a1 -> 'a2 -> 'r)> -> seq<'a1> -> seq<'a2> -> seq<'r> =
@palladin
palladin / gist:1677761
Created January 25, 2012 18:29
Bananas in F#
// http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.125
type ListF<'a, 'b> = Empty | Cons of 'a * 'b
type List<'a> = InL of ListF<'a, List<'a>> with
member self.Out() = let (InL x) = self in x
type PeanoF<'b> = Zero | Suc of 'b
type Peano = InP of PeanoF<Peano> with
member self.Out() = let (InP x) = self in x
let inline out x = (^MF : (member Out : unit -> ^F) (x))
@palladin
palladin / gist:1869460
Created February 20, 2012 14:25
Dining philosophers (Joinads)
open System
open FSharp.Extensions.Joinads
// Init
let n = 5
let chopsticks = [| for i = 1 to n do yield new Channel<unit>() |]
let hungry = [| for i = 1 to n do yield new Channel<unit>() |]
let philosophers = [| "Plato"; "Konfuzius"; "Socrates"; "Voltaire"; "Descartes" |]
let randomDelay (r : Random) = System.Threading.Thread.Sleep(r.Next(1, 10) * 1000)
@palladin
palladin / gist:2148764
Created March 21, 2012 15:46
Lazy Xml
#r "FSharp.Powerpack.dll"
open System
open System.Xml
open Microsoft.FSharp.Collections
let wiki = "c:\enwiki-20120307-pages-articles\enwiki-20120307-pages-articles.xml"
type name = string
type attributes = (string * string) list
@palladin
palladin / gist:2634310
Created May 8, 2012 11:17
Scrap Your Boilerplate (with class)
// http://homepages.cwi.nl/~ralf/syb3/
let inline gmap f g (x : ^R) : ^R = (f ? (g) <- x)
type Data = Data with
static member inline ($)(f : ^F, x : ^A) = gmap Data f x
static member inline (?<-)(Data, f, x : int) = x
static member inline (?<-)(Data, f, x : string) = x
static member inline (?<-)(Data, f, x : bool) = x
@palladin
palladin / gist:3032692
Created July 2, 2012 10:56
Y(n) Polyvariadic fixpoint
let rec Y f x = f (Y f) x
let rec Y2 f1 f2 =
let f1' = Y (fun f1' -> f1 f1' (Y (fun f2' -> f2 f1' f2')))
let f2' = Y (fun f2' -> f2 (Y (fun f1' -> f1 f1' f2')) f2')
f1', f2'
// Example
let even, odd =
Y2 (fun even odd x ->
@palladin
palladin / gist:3121922
Created July 16, 2012 10:09
HLists, Peano & Type-level computations
type HList = interface end
and HNil = HNil with
static member inline (|*|) (f, HNil) = f $ HNil
interface HList
and HCons<'a, 'b when 'b :> HList> = HCons of 'a * 'b with
static member inline (|*|) (f, HCons(x, xs)) = f $ HCons(x, xs)
interface HList
type Peano = interface end
and Zero = Zero with