Skip to content

Commit dd0d469

Browse files
committed
add Choice.getOrRaise
1 parent 1baea1f commit dd0d469

File tree

1 file changed

+14
-9
lines changed
  • src/FSharpx.Extras/ComputationExpressions

1 file changed

+14
-9
lines changed

src/FSharpx.Extras/ComputationExpressions/Monad.fs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,13 @@ module Choice =
747747
| Choice1Of2 a -> a
748748
| Choice2Of2 e -> invalidArg "choice" (sprintf "The choice value was Choice2Of2 '%A'" e)
749749
750+
/// If Choice is 1Of2, return its value.
751+
/// Otherwise throw the exception in 2Of2.
752+
let getOrRaise<'a, 'exn when 'exn :> exn> (c:Choice<'a, 'exn>) =
753+
match c with
754+
| Choice1Of2 r -> r
755+
| Choice2Of2 e -> raise e
756+
750757
/// Wraps a function, encapsulates any exception thrown within to a Choice
751758
let inline protect f x =
752759
try
@@ -1125,15 +1132,16 @@ module Task =
11251132
/// Active pattern that matches on flattened inner exceptions in an AggregateException
11261133
let (|AggregateExn|_|) (e:exn) =
11271134
match e with
1128-
| :? AggregateException as ae -> ae.Flatten().InnerExceptions
1129-
|> List.ofSeq
1130-
|> Some
1131-
| _ -> None
1135+
| :? AggregateException as ae ->
1136+
ae.Flatten().InnerExceptions
1137+
|> List.ofSeq
1138+
|> Some
1139+
| _ -> None
11321140

11331141
/// Creates a task that executes a specified task.
11341142
/// If this task completes successfully, then this function returns Choice1Of2 with the returned value.
11351143
/// If this task raises an exception before it completes then return Choice2Of2 with the raised exception.
1136-
let Catch (t:Task<'a>) =
1144+
let Catch (t:Task<'a>) : Task<Choice<'a, exn>> =
11371145
task {
11381146
try let! r = t
11391147
return Choice1Of2 r
@@ -1183,7 +1191,4 @@ module Task =
11831191
/// Creates a task that executes all the given tasks.
11841192
/// The paralelism is throttled, so that at most `throttle` tasks run at one time.
11851193
let ParallelWithTrottle throttle (tasks : seq<unit -> Task<'a>>) : (Task<'a[]>) =
1186-
let extractOrThrow = function
1187-
| Choice1Of2 r -> r
1188-
| Choice2Of2 e -> raise e
1189-
ParallelWithTrottleCustom extractOrThrow throttle tasks
1194+
ParallelWithTrottleCustom Choice.getOrRaise throttle tasks

0 commit comments

Comments
 (0)