Created
April 29, 2013 19:40
-
-
Save palladin/5484150 to your computer and use it in GitHub Desktop.
Loop Unrolling
This file contains hidden or 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
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 | |
static member inline ($) (PeanoToInt, Zero) = 0 | |
static member inline ($) (PeanoToInt, Succ (x)) = 1 + (PeanoToInt |*| x) | |
type Repeat = Repeat with | |
static member inline ($) (Repeat, Zero) = fun f -> () | |
static member inline ($) (Repeat, (Succ (x) as p)) = fun f -> | |
(Repeat |*| x) f | |
f (PeanoToInt $ p) | |
let four = Succ (Succ (Succ (Succ Zero))) | |
let inline repeat step f = (Repeat $ step) f | |
// Examples | |
repeat four (fun index -> printfn "index: %d" index) | |
// zero-out | |
let array = [|1..8|] | |
for i in 0 .. 4 .. array.Length - 1 do | |
repeat four (fun index -> array.[i + (index - 1)] <- 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment