Skip to content

Commit 27516f4

Browse files
committed
Add zipWith and mapWith funcs to Result
1 parent a96b925 commit 27516f4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/FSharpPlus/Extensions/Result.fs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,56 @@ module Result =
177177
| Ok _, Error e1, Error e2 | Error e1, Ok _, Error e2 | Error e1, Error e2, Ok _ -> Error (combiner e1 e2)
178178
| Error e1, Error e2, Error e3 -> Error (combiner (combiner e1 e2) e3)
179179

180+
/// <summary>Creates a Result value from a pair of Result values, using a function to combine errors if both are Error.</summary>
181+
/// <param name="combiner">Function to combine error values.</param>
182+
/// <param name="source1">The first Result value.</param>
183+
/// <param name="source2">The second Result value.</param>
184+
/// <returns>The tupled value, or the combined Error.</returns>
185+
let zipWith combiner (source1: Result<'T1, 'Error>) (source2: Result<'T2, 'Error>) : Result<'T1 * 'T2, 'Error> =
186+
match source1, source2 with
187+
| Ok a, Ok b -> Ok (a, b)
188+
| Error e, Ok _ | Ok _, Error e -> Error e
189+
| Error e1, Error e2 -> Error (combiner e1 e2)
190+
191+
/// <summary>Creates a Result value from three Result values, using a function to combine errors if more than one are Error.</summary>
192+
/// <param name="combiner">Function to combine error values.</param>
193+
/// <param name="source1">The first Result value.</param>
194+
/// <param name="source2">The second Result value.</param>
195+
/// <param name="source3">The third Result value.</param>
196+
/// <returns>The tupled value, or the combined Error.</returns>
197+
let zip3With combiner (source1: Result<'T1, 'Error>) (source2: Result<'T2, 'Error>) (source3: Result<'T3, 'Error>) : Result<'T1 * 'T2 * 'T3, 'Error> =
198+
match source1, source2, source3 with
199+
| Ok a, Ok b, Ok c -> Ok (a, b, c)
200+
| Error e, Ok _, Ok _ | Ok _, Error e, Ok _ | Ok _, Ok _, Error e -> Error e
201+
| Ok _, Error e1, Error e2 | Error e1, Ok _, Error e2 | Error e1, Error e2, Ok _ -> Error (combiner e1 e2)
202+
| Error e1, Error e2, Error e3 -> Error (combiner (combiner e1 e2) e3)
203+
204+
/// <summary>Creates a Result value from a pair of Result values, using a function to combine errors if both are Error.</summary>
205+
/// <param name="combiner">Function to combine error values.</param>
206+
/// <param name="f">The mapping function.</param>
207+
/// <param name="source1">The first Result value.</param>
208+
/// <param name="source2">The second Result value.</param>
209+
/// <returns>The combined value, or the combined Error.</returns>
210+
let map2With combiner f (source1: Result<'T1, 'Error>) (source2: Result<'T2, 'Error>) : Result<'U, 'Error> =
211+
match source1, source2 with
212+
| Ok a, Ok b -> Ok (f a b)
213+
| Error e, Ok _ | Ok _, Error e -> Error e
214+
| Error e1, Error e2 -> Error (combiner e1 e2)
215+
216+
/// <summary>Creates a Result value from three Result values, using a function to combine errors if more than one are Error.</summary>
217+
/// <param name="combiner">Function to combine error values.</param>
218+
/// <param name="f">The mapping function.</param>
219+
/// <param name="source1">The first Result value.</param>
220+
/// <param name="source2">The second Result value.</param>
221+
/// <param name="source3">The third Result value.</param>
222+
/// <returns>The combined value, or the combined Error.</returns>
223+
let map3With combiner f (source1: Result<'T1, 'Error>) (source2: Result<'T2, 'Error>) (source3: Result<'T3, 'Error>) : Result<'U, 'Error> =
224+
match source1, source2, source3 with
225+
| Ok a, Ok b, Ok c -> Ok (f a b c)
226+
| Error e, Ok _, Ok _ | Ok _, Error e, Ok _ | Ok _, Ok _, Error e -> Error e
227+
| Ok _, Error e1, Error e2 | Error e1, Ok _, Error e2 | Error e1, Error e2, Ok _ -> Error (combiner e1 e2)
228+
| Error e1, Error e2, Error e3 -> Error (combiner (combiner e1 e2) e3)
229+
180230
/// <summary>Ignores the value inside the result, if any.</summary>
181231
/// <param name="source">The result value.</param>
182232
/// <returns><c>Ok ()</c> if the result is <c>Ok</c>, <c>Error e</c> otherwise.</returns>

0 commit comments

Comments
 (0)