Last active
August 29, 2015 14:14
-
-
Save eulerfx/04ec6d5551ddab2499cf to your computer and use it in GitHub Desktop.
F# HList unsafe fold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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