@@ -4,7 +4,6 @@ namespace FSharpPlus
44[<RequireQualifiedAccess>]
55module Result =
66 open FSharp.Core .CompilerServices
7- open System
87
98 /// Applies the wrapped value to the wrapped function when both are Ok and returns a wrapped result or the first Error.
109 /// <param name =" f " >The function wrapped in an Ok or an Error.</param >
@@ -15,44 +14,55 @@ module Result =
1514 /// <summary >If value is Ok, returns both of them tupled. Otherwise it returns the Error value twice in a tuple.</summary >
1615 /// <param name =" source " >The value.</param >
1716 /// <returns >The resulting tuple.</returns >
18- let unzip ( source : Result < 'T * 'U , 'Error >) : Result < 'T , 'Error > * Result < 'U , 'Error > = match source with Ok ( x, y) -> Ok x, Ok y | Error e -> Error e, Error e
17+ let unzip ( source : Result < 'T1 * 'T2 , 'Error >) : Result < 'T1 , 'Error > * Result < 'T2 , 'Error > =
18+ match source with Ok ( x, y) -> Ok x, Ok y | Error e -> Error e, Error e
19+
20+ /// <summary >If value is Ok, returns all three of them tupled. Otherwise it returns the Error value three times in a tuple.</summary >
21+ /// <param name =" source " >The value.</param >
22+ /// <returns >The resulting tuple.</returns >
23+ let unzip3 ( source : Result < 'T1 * 'T2 * 'T3 , 'Error >) : Result < 'T1 , 'Error > * Result < 'T2 , 'Error > * Result < 'T3 , 'Error > =
24+ match source with
25+ | Ok ( x, y, z) -> Ok x, Ok y, Ok z
26+ | Error e -> Error e, Error e, Error e
1927
2028 /// <summary >Creates a Result value from a pair of Result values.</summary >
21- /// <param name =" x " >The first Result value.</param >
22- /// <param name =" y " >The second Result value.</param >
23- ///
29+ /// <param name =" source1 " >The first Result value.</param >
30+ /// <param name =" source2 " >The second Result value.</param >
2431 /// <returns >The tupled value, or the first Error.</returns >
25- let zip ( x : Result < 'T , 'Error >) ( y : Result < 'U , 'Error >) : Result < 'T * 'U , 'Error > = match x, y with Ok a, Ok b -> Ok ( a, b) | Error e, _ | _, Error e -> Error e
32+ let zip ( source1 : Result < 'T1 , 'Error >) ( source2 : Result < 'T2 , 'Error >) : Result < 'T1 * 'T2 , 'Error > =
33+ match source1, source2 with
34+ | Ok a, Ok b -> Ok ( a, b) | Error e, _ | _, Error e -> Error e
2635
2736 /// <summary >Creates a Result value from a three Result values.</summary >
28- /// <param name =" x " >The first Result value.</param >
29- /// <param name =" y " >The second Result value.</param >
30- /// <param name =" z " >The third Result value.</param >
31- ///
37+ /// <param name =" source1 " >The first Result value.</param >
38+ /// <param name =" source2 " >The second Result value.</param >
39+ /// <param name =" source3 " >The third Result value.</param >
3240 /// <returns >The tupled value, or the first Error.</returns >
33- let zip3 ( x : Result < 'T , 'Error >) ( y : Result < 'U , 'Error >) ( z : Result < 'V , 'Error >) : Result < 'T * 'U * 'V , 'Error > = match x, y, z with Ok a, Ok b, Ok c -> Ok ( a, b, c) | Error e, _, _ | _, Error e, _ | _, _, Error e -> Error e
41+ let zip3 ( source1 : Result < 'T1 , 'Error >) ( source2 : Result < 'T2 , 'Error >) ( source3 : Result < 'T3 , 'Error >) : Result < 'T1 * 'T2 * 'T3 , 'Error > =
42+ match source1, source2, source3 with
43+ | Ok a, Ok b, Ok c -> Ok ( a, b, c)
44+ | Error e, _, _ | _, Error e, _ | _, _, Error e -> Error e
3445
3546 /// <summary >Creates a Result value from a pair of Result values, using a function to combine them.</summary >
36- /// <param name =" f " >The mapping function.</param >
37- /// <param name =" x " >The first Result value.</param >
38- /// <param name =" y " >The second Result value.</param >
39- ///
47+ /// <param name =" mapper " >The mapping function.</param >
48+ /// <param name =" source1 " >The first Result value.</param >
49+ /// <param name =" source2 " >The second Result value.</param >
4050 /// <returns >The combined value, or the first Error.</returns >
41- let map2 f ( x : Result < 'T , 'Error >) ( y : Result < 'U , 'Error >) : Result < 'V , 'Error > = match x, y with Ok a, Ok b -> Ok ( f a b) | Error e, _ | _, Error e -> Error e
51+ let map2 mapper ( source1 : Result < 'T1 , 'Error >) ( source2 : Result < 'T2 , 'Error >) : Result < 'T3 , 'Error > =
52+ match source1, source2 with
53+ | Ok a, Ok b -> Ok ( mapper a b)
54+ | Error e, _ | _, Error e -> Error e
4255
4356 /// <summary >Creates a Result value from three Result values, using a function to combine them.</summary >
4457 /// <param name =" f " >The mapping function.</param >
4558 /// <param name =" x " >The first Result value.</param >
4659 /// <param name =" y " >The second Result value.</param >
4760 /// <param name =" z " >The third Result value.</param >
48- ///
4961 /// <returns >The combined value, or the first Error.</returns >
50- let map3 f ( x : Result < 'T , 'Error >) ( y : Result < 'U , 'Error >) ( z : Result < 'V , 'Error >) : Result < 'W , 'Error > =
51- match x, y, z with
52- | Ok a, Ok b, Ok c -> Ok( f a b c)
53- | Error e, _, _
54- | _, Error e, _
55- | _, _, Error e -> Error e
62+ let map3 mapper ( source1 : Result < 'T1 , 'Error >) ( source2 : Result < 'T2 , 'Error >) ( source3 : Result < 'T3 , 'Error >) : Result < 'T4 , 'Error > =
63+ match source1, source2, source3 with
64+ | Ok a, Ok b, Ok c -> Ok ( mapper a b c)
65+ | Error e, _, _ | _, Error e, _ | _, _, Error e -> Error e
5666
5767 /// <summary >Maps both Ok and Error of a Result.</summary >
5868 /// <param name =" errorMapper " >Function to be applied to source, if it contains an Error value.</param >
@@ -71,13 +81,21 @@ module Result =
7181 let flatten source : Result < 'T , 'Error > = match source with Ok ( Ok v) -> Ok v | Ok ( Error e) | Error e -> Error e
7282
7383 /// Like Result.bindError but with flipped arguments.
74- let inline catch x f = x |> function Ok v -> Ok v | Error e -> ( f: 't-> _) e : Result< 'v, 'e>
84+ /// <summary >If the input value is an Ok leaves it unchanged, otherwise maps the Error value.</summary >
85+ /// <param name =" f " >A function that takes the error and transforms it into another error.</param >
86+ /// <param name =" source " >The source input value.</param >
87+ /// <returns >A result of the same type as the input.</returns >
88+ let inline catch ( source : Result < 'T , 'TError >) ( f : 'TError -> Result < 'T , 'UError >) : Result < 'T , 'UError > =
89+ match source with
90+ | Ok v -> Ok v
91+ | Error e -> f e
7592
7693 /// <summary >If the input value is an Ok leaves it unchanged, otherwise maps the Error value and flattens the resulting nested Result.</summary >
7794 /// <param name =" binder " >A function that takes the error and transforms it into a result.</param >
7895 /// <param name =" source " >The source input value.</param >
7996 /// <returns >A result of the output type of the binder.</returns >
80- let inline bindError ( binder : 'Error -> Result < 'T , 'Error2 >) ( source : Result < 'T , 'Error >) = match source with Ok v -> Ok v | Error e -> binder e
97+ let inline bindError ( binder : 'TError -> Result < 'T , 'UError >) ( source : Result < 'T , 'TError >) : Result < 'T , 'UError > =
98+ match source with Ok v -> Ok v | Error e -> binder e
8199
82100 /// <summary ><c >iterError f inp</c > executes <c >match inp with Ok _ -> () | Error x -> f x</c >.</summary >
83101 ///
@@ -97,16 +115,21 @@ module Result =
97115 /// <param name =" fError " >Function to be applied to source, if it contains an Error value.</param >
98116 /// <param name =" source " >The source value, containing an Ok or an Error.</param >
99117 /// <returns >The result of applying either functions.</returns >
100- let inline either ( fOk : 'T -> 'U ) ( fError : 'Error -> 'U ) source = match source with Ok v -> fOk v | Error e -> fError e
118+ let inline either ( fOk : 'T -> 'U ) ( fError : 'Error -> 'U ) source = match source with Ok v -> fOk v | Error e -> fError e
101119
102- /// Creates a safe version of the supplied function, which returns a Result<'U,exn> instead of throwing exceptions.
103- let protect ( f : 'T -> 'U ) x =
120+ /// <summary >Creates a safe version of the supplied function, which returns a Result<'U, exn> instead of throwing exceptions.</summary >
121+ /// <param name =" unsafeFunction " >The function that may throw exceptions.</param >
122+ /// <param name =" source " >The input value for the function.</param >
123+ /// <returns >An Ok of the function result, or an Error of the caught exception.</returns >
124+ let protect ( unsafeFunction : 'T -> 'U ) source : Result < 'U , exn > =
104125 try
105- Ok ( f x )
126+ Ok ( unsafeFunction source )
106127 with e -> Error e
107128
108- /// Gets the 'Ok' value. If it's an 'Error' this function will throw an exception.
109- let get ( source : Result < 'T , 'Error >) =
129+ /// <summary >Gets the 'Ok' value. If it's an 'Error' this function will throw an exception.</summary >
130+ /// <param name =" source " >The input result.</param >
131+ /// <returns >The 'Ok' value.</returns >
132+ let get ( source : Result < 'T , 'Error >) =
110133 match source with
111134 | Ok x -> x
112135 | Error e ->
@@ -124,7 +147,7 @@ module Result =
124147 /// Note: this function has since been added to FSharp.Core.
125148 /// It will be removed in next major release of FSharpPlus.
126149 /// </remarks >
127- let defaultValue ( value : 'T ) ( result : Result < 'T , 'Error >) : 'T = match result with Ok v -> v | _ -> value
150+ let defaultValue ( value : 'T ) ( result : Result < 'T , 'Error >) : 'T = match result with Ok v -> v | _ -> value
128151
129152 /// <summary >Gets the value of the result if the result is <c >Ok</c >, otherwise evaluates <paramref name =" defThunk " /> and returns the result.</summary >
130153 ///
@@ -134,14 +157,18 @@ module Result =
134157 /// Note: this function has since been added to FSharp.Core.
135158 /// It will be removed in next major release of FSharpPlus.
136159 /// </remarks >
137- let defaultWith ( defThunk : 'Error -> 'T ) ( result : Result < 'T , 'Error >) : 'T = match result with Ok v -> v | Error e -> defThunk e
160+ let defaultWith ( defThunk : 'Error -> 'T ) ( result : Result < 'T , 'Error >) : 'T = match result with Ok v -> v | Error e -> defThunk e
138161
139- /// Converts a Result<'T,'Error> to a Choice<'T,'Error>.
140- let toChoice ( source : Result < 'T , 'U >) = match source with Ok x-> Choice1Of2 x | Error x -> Choice2Of2 x
162+ /// Converts a Result to a Choice.
163+ /// <param name =" source " >The input Result value.</param >
164+ /// <returns >A Choice value representing the same data.</returns >
165+ let toChoice ( source : Result < 'T , 'Error >) : Choice < 'T , 'Error > = match source with Ok x -> Choice1Of2 x | Error x -> Choice2Of2 x
166+
167+ /// Converts a Choice to a Result.
168+ /// <param name =" source " >The input Choice value.</param >
169+ /// <returns >A Result value representing the same data.</returns >
170+ let ofChoice ( source : Choice < 'T , 'Error >) : Result < 'T , 'Error > = match source with Choice1Of2 x -> Ok x | Choice2Of2 x -> Error x
141171
142- /// Creates a Result<'T,'Error> from a Choice<'T,'Error>.
143- let ofChoice ( source : Choice < 'T , 'U >) = match source with Choice1Of2 x-> Ok x | Choice2Of2 x -> Error x
144-
145172 /// <summary >
146173 /// Creates two lists by classifying the values depending on whether they were wrapped with Ok or Error.
147174 /// </summary >
@@ -164,15 +191,28 @@ module Result =
164191 coll1.Close (), coll2.Close ()
165192 #endif
166193
167- let apply2With combiner f ( x : Result < 'T , 'Error >) ( y : Result < 'U , 'Error >) : Result < 'V , 'Error > =
168- match x, y with
169- | Ok a, Ok b -> Ok ( f a b)
194+ /// <summary >Creates a Result value from a pair of Result values, using a function to combine errors if both are Error.</summary >
195+ /// <param name =" combiner " >Function to combine error values.</param >
196+ /// <param name =" mapper " >The mapping function.</param >
197+ /// <param name =" source1 " >The first Result value.</param >
198+ /// <param name =" source2 " >The second Result value.</param >
199+ /// <returns >The combined value, or the combined Error.</returns >
200+ let apply2With combiner ( mapper : 'T1 -> 'T2 -> 'U ) ( source1 : Result < 'T1 , 'Error >) ( source2 : Result < 'T2 , 'Error >) : Result < 'U , 'Error > =
201+ match source1, source2 with
202+ | Ok a, Ok b -> Ok ( mapper a b)
170203 | Error e, Ok _ | Ok _, Error e -> Error e
171204 | Error e1, Error e2 -> Error ( combiner e1 e2)
172205
173- let apply3With combiner f ( x : Result < 'T , 'Error >) ( y : Result < 'U , 'Error >) ( z : Result < 'V , 'Error >) : Result < 'W , 'Error > =
174- match x, y, z with
175- | Ok a, Ok b, Ok c -> Ok ( f a b c)
206+ /// <summary >Creates a Result value from three Result values, using a function to combine errors if more than one are Error.</summary >
207+ /// <param name =" combiner " >Function to combine error values.</param >
208+ /// <param name =" mapper " >The mapping function.</param >
209+ /// <param name =" source1 " >The first Result value.</param >
210+ /// <param name =" source2 " >The second Result value.</param >
211+ /// <param name =" source3 " >The third Result value.</param >
212+ /// <returns >The combined value, or the combined Error.</returns >
213+ let apply3With combiner ( mapper : 'T1 -> 'T2 -> 'T3 -> 'U ) ( source1 : Result < 'T1 , 'Error >) ( source2 : Result < 'T2 , 'Error >) ( source3 : Result < 'T3 , 'Error >) : Result < 'U , 'Error > =
214+ match source1, source2, source3 with
215+ | Ok a, Ok b, Ok c -> Ok ( mapper a b c)
176216 | Error e, Ok _, Ok _ | Ok _, Error e, Ok _ | Ok _, Ok _, Error e -> Error e
177217 | Ok _, Error e1, Error e2 | Error e1, Ok _, Error e2 | Error e1, Error e2, Ok _ -> Error ( combiner e1 e2)
178218 | Error e1, Error e2, Error e3 -> Error ( combiner ( combiner e1 e2) e3)
0 commit comments