Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf61981

Browse files
dsymeKevinRansom
authored andcommitted
fix bind seqquences (dotnet#2480)
1 parent6924fc5 commitaf61981

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

‎src/absil/illib.fs‎

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -538,46 +538,57 @@ type Cancellable<'TResult> = Cancellable of (System.Threading.CancellationToken
538538
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
539539
moduleCancellable=
540540

541+
/// Run a cancellable computation using the given cancellation token
542+
letrun(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-
letbind f(Cancellable f1)=
549+
letbind 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-
letmap f(Cancellable f1)=
556+
letmap foper=
559557
Cancellable(fun ct->
560-
matchf1 ctwith
561-
| ValueOrCancelled.Valueres1-> ValueOrCancelled.Value(fres1)
562-
| ValueOrCancelled.Cancellederr1-> ValueOrCancelled.Cancellederr1)
558+
matchrun ct operwith
559+
| ValueOrCancelled.Valueres-> ValueOrCancelled.Value(fres)
560+
| ValueOrCancelled.Cancellederr-> ValueOrCancelled.Cancellederr)
563561

564562
/// Return a simple value as the result of a cancellable computation
565563
letret x= Cancellable(fun _-> ValueOrCancelled.Value x)
566564

567565
/// Fold a cancellable computation along a sequence of inputs
568566
letfold 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
572575
leteach 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
576590
letdelay(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-
letrun 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.
583594
letrunWithoutCancellation comp=

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp