Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / gist:1086248
Created July 16, 2011 10:37
Monadic Memoization
// Inspired by http://www.cs.utexas.edu/~wcook/Drafts/2006/MemoMixins.pdf
// State Monad combined with Continuation Monad (StateT Monad transformer)
type StateContMonad<'s, 'a, 'r> = StateContMonad of ('s -> ('s -> 'a -> 'r) -> 'r)
// Computation Builder
type StateContBuilder() =
member self.Return value =
StateContMonad (fun state k -> k state value)
member self.Bind(StateContMonad contStateMonad, f) =
@palladin
palladin / gist:1086249
Created July 16, 2011 10:38
Functor => Applicative => Monad
#r "FSharp.PowerPack.dll"
open System
// Generic container of 'T
// Also parameterized by 'TypeClass : (new : unit -> 'TypeClass)
// to implicit get a 'TypeClass instance (like passing the type class dictionary)
// The idea is to encode Type Classes with subtype polymorphism and OOP Classes
type Generic<'T, 'TypeClass when 'TypeClass : (new : unit -> 'TypeClass)> = interface end
@palladin
palladin / gist:1088007
Created July 17, 2011 20:12
Type based Regex Active Patterns
// Based on http://blogs.msdn.com/b/chrsmith/archive/2008/02/22/regular-expressions-via-active-patterns.aspx
open System
open System.Text.RegularExpressions
let (|Match|_|) (pat:string) (inp:string) =
let m = Regex.Match(inp, pat) in
if m.Success
then Some (List.tail [ for g in m.Groups -> g.Value ])
@palladin
palladin / gist:1088009
Created July 17, 2011 20:13
Hughes's FuncList
// for more info http://www.cs.tufts.edu/~nr/cs257/archive/john-hughes/lists.pdf
type FuncList<'a> = 'a list -> 'a list
// Monoid comprehension
type FuncListBuilder() =
member self.Combine (first : FuncList<'a>, second : FuncList<'a>) : FuncList<'a> = (first << second)
member self.Zero() : FuncList<'a> = id
member self.Yield (value : 'a) : FuncList<'a> = fun tail -> value :: tail
member self.YieldFrom (value : FuncList<'a>) : FuncList<'a> = value
member self.Delay ( f : unit -> FuncList<'a>) : FuncList<'a> = (fun tail -> f () tail)
@palladin
palladin / gist:1088010
Created July 17, 2011 20:14
Hughes's CPSFuncList
type FuncList<'a> = 'a list -> 'a list
type CPSFuncList<'a> = FuncList<'a> -> FuncList<'a>
// Monoid comprehension
type CPSFuncListBuilder() =
member self.Combine (first : CPSFuncList<'a>, second : CPSFuncList<'a>) : CPSFuncList<'a> =
(fun k -> second (fun tail -> first (fun tail' -> k tail') tail))
member self.Zero() : CPSFuncList<'a> = (fun k tail -> k tail)
member self.Yield (value : 'a) : CPSFuncList<'a> = (fun k tail -> k (value :: tail))
member self.YieldFrom (value : CPSFuncList<'a>) : CPSFuncList<'a> = value
@palladin
palladin / gist:1088013
Created July 17, 2011 20:15
A beautiful fixed-point finding function
let rec iterate f value =
seq { yield value;
yield! iterate f (f value) }
let fixedPoint f initial =
iterate f initial
|> Seq.pairwise
|> Seq.pick (fun (first, second) ->
if first = second then Some first else None)
@palladin
palladin / gist:1088015
Created July 17, 2011 20:16
LazyList
open System.Collections
open System.Collections.Generic
// LazyList dataType + Monoid Structure
type LazyList<'T> = Empty
| Cons of 'T * (unit -> LazyList<'T>)
| Delay of (unit -> LazyList<'T>)
| Combine of LazyList<'T> * LazyList<'T> with
interface IEnumerable<'T> with
member self.GetEnumerator() =
@palladin
palladin / gist:1106567
Created July 26, 2011 11:50
Erik Lippert's Comma Quibbling
// http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx
#time
#r "FSharp.PowerPack.dll"
open System
open System.Text
let format (words : seq<string>) =
let sb (value : string) = new StringBuilder(value)
@palladin
palladin / gist:1134074
Created August 9, 2011 13:42
Async based MapReduce
open System
open System.Net
open Microsoft.FSharp.Control.WebExtensions
let noiseWords = [|"a"; "about"; "above"; "all"; "along"; "also"; "although"; "am"; "an"; "any"; "are"; "aren't"; "as"; "at";
"be"; "because"; "been"; "but"; "by"; "can"; "cannot"; "could"; "couldn't";
"did"; "didn't"; "do"; "does"; "doesn't"; "e.g."; "either"; "etc"; "etc."; "even"; "ever";
"for"; "from"; "further"; "get"; "gets"; "got"; "had"; "hardly"; "has"; "hasn't"; "having"; "he";
@palladin
palladin / gist:1180567
Created August 30, 2011 09:49
Scrap Your Boilerplate
// http://research.microsoft.com/enus/um/people/simonpj/papers/hmap/hmap.ps
// Type safe conversion functions
let cast<'T, 'R> (v : 'T) : 'R = v :> obj :?> 'R
let mkT<'T, 'R> (f : 'T -> 'T) : 'R -> 'R =
if typeof<'T> = typeof<'R> then (fun (v : 'R) -> v |> cast |> f |> cast) else id
let mkQ (r : 'R) (q : 'B -> 'R) (a : 'A) : 'R =
if typeof<'A> = typeof<'B> then
a |> cast |> q
else r