@@ -70,4 +70,26 @@ module AsyncExtensions =
7070 /// Creates an async computation which binds the result of the specified
7171 /// async computation to the specified function. The computation produced
7272 /// by the specified function is returned.
73- static member inline bind f a = async.Bind( a, f)
73+ static member inline bind f a = async.Bind( a, f)
74+
75+ /// Maps over an async computation which produces a choice value
76+ /// using a function which maps over Choice1Of2 and itself returns a choice.
77+ /// A value of Choice2Of2 is treated like an error and passed through.
78+ static member mapChoice ( f : 'a -> Choice < 'b , 'e >) ( a : Async < Choice < 'a , 'e >>) : Async < Choice < 'b , 'e >> =
79+ a |> Async.map ( function
80+ | Choice1Of2 a' -> f a'
81+ | Choice2Of2 e -> Choice2Of2 e)
82+
83+ /// Binds an async computation producing a choice value to another async
84+ /// computation producing a choice such that a Choice2Of2 value is passed through.
85+ static member bindChoice ( f : 'a -> Async < Choice < 'b , 'e >>) ( a : Async < Choice < 'a , 'e >>) : Async < Choice < 'b , 'e >> =
86+ a |> Async.bind ( function
87+ | Choice1Of2 a' -> f a'
88+ | Choice2Of2 e -> Choice2Of2 e |> async.Return)
89+
90+ /// Binds an async computation producing a choice value to another async
91+ /// computation producing a choice such that a Choice2Of2 value is passed through.
92+ static member bindChoices ( f : 'a -> Async < Choice < 'b , 'e2 >>) ( a : Async < Choice < 'a , 'e1 >>) : Async < Choice < 'b , Choice < 'e1 , 'e2 >>> =
93+ a |> Async.bind ( function
94+ | Choice1Of2 a' -> f a' |> Async.map ( function Choice1Of2 b -> Choice1Of2 b | Choice2Of2 e2 -> Choice2Of2 ( Choice2Of2 e2))
95+ | Choice2Of2 e1 -> Choice2Of2 ( Choice1Of2 e1) |> async.Return)
0 commit comments