@@ -538,46 +538,57 @@ type Cancellable<'TResult> = Cancellable of (System.Threading.CancellationToken
538538[<CompilationRepresentation( CompilationRepresentationFlags.ModuleSuffix) >]
539539module Cancellable =
540540
541+ /// Run a cancellable computation using the given cancellation token
542+ let run ( ct : System.Threading.CancellationToken ) ( Cancellable oper ) =
543+ if ct.IsCancellationRequestedthen
544+ ValueOrCancelled.Cancelled( OperationCanceledException())
545+ else
546+ oper ct
547+
541548/// Bind the result of a cancellable computation
542- let bind f ( Cancellable f1 ) =
549+ let bind fcomp1 =
543550 Cancellable( fun ct ->
544- if ct.IsCancellationRequestedthen
545- ValueOrCancelled.Cancelled( OperationCanceledException())
546- else
547- match f1 ctwith
548- | ValueOrCancelled.Value res1->
549- if ct.IsCancellationRequestedthen
550- ValueOrCancelled.Cancelled( OperationCanceledException())
551- else
552- let ( Cancellable f2 ) = f res1
553- f2 ct
554- | ValueOrCancelled.Cancelled err1->
555- ValueOrCancelled.Cancelled err1)
551+ match run ct comp1with
552+ | ValueOrCancelled.Value v1-> run ct( f v1)
553+ | ValueOrCancelled.Cancelled err1-> ValueOrCancelled.Cancelled err1)
556554
557555/// Map the result of a cancellable computation
558- let map f ( Cancellable f1 ) =
556+ let map foper =
559557 Cancellable( fun ct ->
560- match f1 ctwith
561- | ValueOrCancelled.Valueres1 -> ValueOrCancelled.Value( fres1 )
562- | ValueOrCancelled.Cancellederr1 -> ValueOrCancelled.Cancellederr1 )
558+ match run ct oper with
559+ | ValueOrCancelled.Valueres -> ValueOrCancelled.Value( fres )
560+ | ValueOrCancelled.Cancellederr -> ValueOrCancelled.Cancellederr )
563561
564562/// Return a simple value as the result of a cancellable computation
565563let ret x = Cancellable( fun _ -> ValueOrCancelled.Value x)
566564
567565/// Fold a cancellable computation along a sequence of inputs
568566let fold f acc seq =
569- ( ret acc, seq) ||> Seq.fold( fun acc x -> acc|> bind( fun acc -> f acc x))
567+ Cancellable( fun ct ->
568+ ( ValueOrCancelled.Value acc, seq)
569+ ||> Seq.fold( fun acc x ->
570+ match accwith
571+ | ValueOrCancelled.Value accv-> run ct( f accv x)
572+ | res-> res))
570573
571574/// Iterate a cancellable computation over a collection
572575let each f seq =
573- ([], seq) ||> fold( fun acc x -> f x|> map( fun x2 -> x2:: acc)) |> map List.rev
576+ Cancellable( fun ct ->
577+ ( ValueOrCancelled.Value[], seq)
578+ ||> Seq.fold( fun acc x ->
579+ match accwith
580+ | ValueOrCancelled.Value acc->
581+ match run ct( f x) with
582+ | ValueOrCancelled.Value x2-> ValueOrCancelled.Value( x2:: acc)
583+ | ValueOrCancelled.Cancelled err1-> ValueOrCancelled.Cancelled err1
584+ | canc-> canc)
585+ |> function
586+ | ValueOrCancelled.Value acc-> ValueOrCancelled.Value( List.rev acc)
587+ | canc-> canc)
574588
575589/// Delay a cancellable computation
576590let delay ( f : unit -> Cancellable < 'T >) = Cancellable( fun ct -> let ( Cancellable g ) = f() in g ct)
577591
578- /// Run a cancellable computation using the given cancellation token
579- let run ct ( Cancellable f ) = f ct
580-
581592/// Run the computation in a mode where it may not be cancelled. The computation never results in a
582593/// ValueOrCancelled.Cancelled.
583594let runWithoutCancellation comp =