5 people like it.
Like the snippet!
IterateeCPS
An iteratee that uses continuation-passing style as an optimization. There is no more discriminated union, and the signature should feel familiar to those using Async.StartWithContinuations.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
|
module FSharp.Monad.Iteratee.CPS
open System
type Stream<'a> =
| Chunk of 'a
| Empty
| EOF
type IterateeCPS<'el,'a,'r> = Iteratee of (((Stream<'el> -> IterateeCPS<'el,'a,'r>) * exn option -> 'r) -> ('a * Stream<'el> -> 'r) -> 'r)
let runIter (Iteratee(i)) onCont onDone = i onCont onDone
let returnI x = Iteratee(fun _ onDone -> onDone(x, Empty))
let rec bind (m: IterateeCPS<'el,'a,'r>) (f: 'a -> IterateeCPS<'el,'b,'r>) : IterateeCPS<'el,'b,'r> =
Iteratee(fun onCont onDone ->
let mdone (a, s) =
let fcont (k, e) =
match e with
| None -> runIter (k s) onCont onDone
| Some _ -> onCont(k, e)
match s with
| Empty -> runIter (f a) onCont onDone
| _ -> runIter (f a) fcont (fun (x, _) -> onDone(x, s))
in runIter m (fun (k, e) -> onCont(k >> (fun m' -> bind m' f), e)) mdone)
type IterateeCPSBuilder() =
member this.Return(x) = returnI x
member this.ReturnFrom(m:IterateeCPS<_,_,_>) = m
member this.Bind(m, k) = bind m k
member this.Zero() = returnI ()
member this.Combine(comp1, comp2) = bind comp1 (fun () -> comp2)
member this.Delay(f) = bind (returnI ()) f
let iterateeCPS = IterateeCPSBuilder()
let rec throw e = Iteratee(fun onCont _ -> onCont((fun _ -> throw e), (Some e)))
let throwRecoverable e i = Iteratee(fun onCont _ -> onCont(i, (Some e)))
let doneI x str = Iteratee(fun _ onDone -> onDone(x, str))
let contI k e = Iteratee(fun onCont _ -> onCont(k, e))
let liftI k = Iteratee(fun onCont _ -> onCont(k, None))
let joinI outer = bind outer (fun inner ->
Iteratee(fun onCont onDone ->
let od (x, _) = onDone(x, Empty)
let rec oc = function
| k, None -> runIter (k EOF) oc' od
| _, Some e -> runIter (throw e) onCont od
and oc' (_, e) = runIter (throw (Option.get e)) oc od
runIter inner oc od))
|
Multiple items
namespace FSharp
--------------------
namespace Microsoft.FSharp
namespace FSharp.Monad
namespace FSharp.Monad.Iteratee
module CPS
from FSharp.Monad.Iteratee
namespace System
type Stream<'a> =
| Chunk of 'a
| Empty
| EOF
Full name: FSharp.Monad.Iteratee.CPS.Stream<_>
union case Stream.Chunk: 'a -> Stream<'a>
union case Stream.Empty: Stream<'a>
union case Stream.EOF: Stream<'a>
type IterateeCPS<'el,'a,'r> = | Iteratee of (((Stream<'el> -> IterateeCPS<'el,'a,'r>) * exn option -> 'r) -> ('a * Stream<'el> -> 'r) -> 'r)
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPS<_,_,_>
union case IterateeCPS.Iteratee: (((Stream<'el> -> IterateeCPS<'el,'a,'r>) * exn option -> 'r) -> ('a * Stream<'el> -> 'r) -> 'r) -> IterateeCPS<'el,'a,'r>
type exn = Exception
Full name: Microsoft.FSharp.Core.exn
type 'T option = Option<'T>
Full name: Microsoft.FSharp.Core.option<_>
val runIter : IterateeCPS<'a,'b,'c> -> onCont:((Stream<'a> -> IterateeCPS<'a,'b,'c>) * exn option -> 'c) -> onDone:('b * Stream<'a> -> 'c) -> 'c
Full name: FSharp.Monad.Iteratee.CPS.runIter
val i : (((Stream<'a> -> IterateeCPS<'a,'b,'c>) * exn option -> 'c) -> ('b * Stream<'a> -> 'c) -> 'c)
val onCont : ((Stream<'a> -> IterateeCPS<'a,'b,'c>) * exn option -> 'c)
val onDone : ('b * Stream<'a> -> 'c)
val returnI : x:'a -> IterateeCPS<'b,'a,'c>
Full name: FSharp.Monad.Iteratee.CPS.returnI
val x : 'a
val onDone : ('a * Stream<'b> -> 'c)
val bind : m:IterateeCPS<'el,'a,'r> -> f:('a -> IterateeCPS<'el,'b,'r>) -> IterateeCPS<'el,'b,'r>
Full name: FSharp.Monad.Iteratee.CPS.bind
val m : IterateeCPS<'el,'a,'r>
val f : ('a -> IterateeCPS<'el,'b,'r>)
val onCont : ((Stream<'el> -> IterateeCPS<'el,'b,'r>) * exn option -> 'r)
val onDone : ('b * Stream<'el> -> 'r)
val mdone : ('a * Stream<'el> -> 'r)
val a : 'a
val s : Stream<'el>
val fcont : ((Stream<'el> -> IterateeCPS<'el,'b,'r>) * exn option -> 'r)
val k : (Stream<'el> -> IterateeCPS<'el,'b,'r>)
val e : exn option
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val x : 'b
val k : (Stream<'el> -> IterateeCPS<'el,'a,'r>)
val m' : IterateeCPS<'el,'a,'r>
Multiple items
type IterateeCPSBuilder =
new : unit -> IterateeCPSBuilder
member Bind : m:IterateeCPS<'i,'j,'k> * k:('j -> IterateeCPS<'i,'l,'k>) -> IterateeCPS<'i,'l,'k>
member Combine : comp1:IterateeCPS<'d,unit,'e> * comp2:IterateeCPS<'d,'f,'e> -> IterateeCPS<'d,'f,'e>
member Delay : f:(unit -> IterateeCPS<'a,'b,'c>) -> IterateeCPS<'a,'b,'c>
member Return : x:'p -> IterateeCPS<'q,'p,'r>
member ReturnFrom : m:IterateeCPS<'m,'n,'o> -> IterateeCPS<'m,'n,'o>
member Zero : unit -> IterateeCPS<'g,unit,'h>
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPSBuilder
--------------------
new : unit -> IterateeCPSBuilder
val this : IterateeCPSBuilder
member IterateeCPSBuilder.Return : x:'p -> IterateeCPS<'q,'p,'r>
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPSBuilder.Return
val x : 'p
member IterateeCPSBuilder.ReturnFrom : m:IterateeCPS<'m,'n,'o> -> IterateeCPS<'m,'n,'o>
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPSBuilder.ReturnFrom
val m : IterateeCPS<'m,'n,'o>
member IterateeCPSBuilder.Bind : m:IterateeCPS<'i,'j,'k> * k:('j -> IterateeCPS<'i,'l,'k>) -> IterateeCPS<'i,'l,'k>
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPSBuilder.Bind
val m : IterateeCPS<'i,'j,'k>
val k : ('j -> IterateeCPS<'i,'l,'k>)
member IterateeCPSBuilder.Zero : unit -> IterateeCPS<'g,unit,'h>
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPSBuilder.Zero
member IterateeCPSBuilder.Combine : comp1:IterateeCPS<'d,unit,'e> * comp2:IterateeCPS<'d,'f,'e> -> IterateeCPS<'d,'f,'e>
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPSBuilder.Combine
val comp1 : IterateeCPS<'d,unit,'e>
val comp2 : IterateeCPS<'d,'f,'e>
member IterateeCPSBuilder.Delay : f:(unit -> IterateeCPS<'a,'b,'c>) -> IterateeCPS<'a,'b,'c>
Full name: FSharp.Monad.Iteratee.CPS.IterateeCPSBuilder.Delay
val f : (unit -> IterateeCPS<'a,'b,'c>)
val iterateeCPS : IterateeCPSBuilder
Full name: FSharp.Monad.Iteratee.CPS.iterateeCPS
val throw : e:exn -> IterateeCPS<'a,'b,'c>
Full name: FSharp.Monad.Iteratee.CPS.throw
val e : exn
val throwRecoverable : e:exn -> i:(Stream<'a> -> IterateeCPS<'a,'b,'c>) -> IterateeCPS<'a,'b,'c>
Full name: FSharp.Monad.Iteratee.CPS.throwRecoverable
val i : (Stream<'a> -> IterateeCPS<'a,'b,'c>)
val doneI : x:'a -> str:Stream<'b> -> IterateeCPS<'b,'a,'c>
Full name: FSharp.Monad.Iteratee.CPS.doneI
val str : Stream<'b>
val contI : k:(Stream<'a> -> IterateeCPS<'a,'b,'c>) -> e:exn option -> IterateeCPS<'a,'b,'c>
Full name: FSharp.Monad.Iteratee.CPS.contI
val k : (Stream<'a> -> IterateeCPS<'a,'b,'c>)
val liftI : k:(Stream<'a> -> IterateeCPS<'a,'b,'c>) -> IterateeCPS<'a,'b,'c>
Full name: FSharp.Monad.Iteratee.CPS.liftI
val joinI : outer:IterateeCPS<'a,IterateeCPS<'b,'c,'d>,'d> -> IterateeCPS<'a,'c,'d>
Full name: FSharp.Monad.Iteratee.CPS.joinI
val outer : IterateeCPS<'a,IterateeCPS<'b,'c,'d>,'d>
val inner : IterateeCPS<'b,'c,'d>
val onCont : ((Stream<'a> -> IterateeCPS<'a,'c,'d>) * exn option -> 'd)
val onDone : ('c * Stream<'a> -> 'd)
val od : ('c * 'e -> 'd)
val x : 'c
val oc : ((Stream<'e> -> IterateeCPS<'e,'c,'d>) * exn option -> 'd)
val k : (Stream<'e> -> IterateeCPS<'e,'c,'d>)
val oc' : ((Stream<'e> -> IterateeCPS<'e,'c,'d>) * exn option -> 'd)
module Option
from Microsoft.FSharp.Core
val get : option:'T option -> 'T
Full name: Microsoft.FSharp.Core.Option.get
More information