Skip to content

Instantly share code, notes, and snippets.

@eulerfx
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eulerfx/04ec6d5551ddab2499cf to your computer and use it in GitHub Desktop.
Save eulerfx/04ec6d5551ddab2499cf to your computer and use it in GitHub Desktop.
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
let inline fold (hl:#HList) (f:Fold<'b>) : 'b = f $ hl
let inline foldLength hl = fold hl (Fold((fun l _ -> l + 1), 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment