You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Applies a function to each element of a sequence and returns a single result
42
45
/// </summary>
43
46
/// <param name="f">The function to apply to each element</param>
44
47
/// <param name="xs">The input sequence</param>
45
-
/// <returns>A result with the ok elements in a sequence or the first error occurring in the sequence</returns>
46
-
/// <remarks>This function is equivalent to <see cref="traverseResultM'"/> but applying and initial state of 'Seq.empty'</remarks>
48
+
/// <returns>A result with the ok elements in an array, or the first error occurring in the sequence</returns>
49
+
/// <remarks>This function is equivalent to <see cref="traverseResultM'"/> but applying an initial state of 'Seq.empty'</remarks>
47
50
lettraverseResultM f xs = traverseResultM' (Ok Seq.empty) f xs
48
51
49
52
/// <summary>
@@ -60,45 +63,55 @@ let sequenceResultM xs = traverseResultM id xs
60
63
/// <param name="state">The initial state</param>
61
64
/// <param name="f">The function to apply to each element</param>
62
65
/// <param name="xs">The input sequence</param>
63
-
/// <returns>A result with the ok elements in a sequence or a sequence of all errors occuring in the original sequence</returns>
64
-
let inlinetraverseResultA' state ([<InlineIfLambda>]f:'okInput ->Result<'okOutput,'error>)xs =
65
-
letfolder state x =
66
-
match state, f x with
67
-
| Error errors, Error e ->
68
-
seq{
69
-
e
70
-
yield! Seq.rev errors
71
-
}
72
-
|> Seq.rev
73
-
|> Error
74
-
| Ok oks, Ok ok ->
75
-
seq{
76
-
ok
77
-
yield! Seq.rev oks
78
-
}
79
-
|> Seq.rev
80
-
|> Ok
81
-
| Ok _, Error e ->
82
-
Seq.singleton e
83
-
|> Error
84
-
| Error _, Ok _-> state
66
+
/// <returns>If no Errors encountered, an Ok result bearing an array of the ok elements from the 'state' followed by those gathered from the sequence, or an Error bearing an array of all errors from the 'state' and/or those in the sequence</returns>
| Ok _->()// as the initial state was failure, oks are irrelevant
83
+
| Error e -> errs.Add e
84
+
85
+
Error(errs.ToArray())
86
+
| Ok initialSuccesses ->
87
+
88
+
letoks= ResizeArray(initialSuccesses)
89
+
leterrs= ResizeArray()
90
+
91
+
for x in xs do
92
+
match f x with
93
+
| Error e -> errs.Add e
94
+
| Ok r when errs.Count =0-> oks.Add r
95
+
| Ok _->()// no point saving results we won't use given the end result will be Error
96
+
97
+
match errs.ToArray()with
98
+
|[||]-> Ok(oks.ToArray())
99
+
| errs -> Error errs
87
100
88
101
/// <summary>
89
102
/// Applies a function to each element of a sequence and returns a single result
90
103
/// </summary>
91
104
/// <param name="f">The function to apply to each element</param>
92
105
/// <param name="xs">The input sequence</param>
93
-
/// <returns>A result with the ok elements in a sequence or a sequence of all errors occuring in the original sequence</returns>
94
-
/// <remarks>This function is equivalent to <see cref="traverseResultA'"/> but applying and initial state of 'Seq.empty'</remarks>
106
+
/// <returns>A result with the ok elements in an array or an array of all errors from across the 'state' and the sequence</returns>
107
+
/// <remarks>This function is equivalent to <see cref="traverseResultA'"/> but applying an initial state of Seq.empty'</remarks>
95
108
lettraverseResultA f xs = traverseResultA' (Ok Seq.empty) f xs
96
109
97
110
/// <summary>
98
111
/// Converts a sequence of results into a single result
99
112
/// </summary>
100
113
/// <param name="xs">The input sequence</param>
101
-
/// <returns>A result with the ok elements in a sequence or a sequence of all errors occuring in the original sequence</returns>
114
+
/// <returns>A result with the ok elements in an array or an array of all errors from across the 'state' and the sequence</returns>
102
115
/// <remarks>This function is equivalent to <see cref="traverseResultA"/> but auto-applying the 'id' function</remarks>
103
116
letsequenceResultA xs = traverseResultA id xs
104
117
@@ -146,7 +159,7 @@ let inline traverseAsyncResultM'
146
159
/// <param name="f">The function to apply to each element</param>
147
160
/// <param name="xs">The input sequence</param>
148
161
/// <returns>An async result with the ok elements in a sequence or the first error occurring in the sequence</returns>
149
-
/// <remarks>This function is equivalent to <see cref="traverseAsyncResultM'"/> but applying and initial state of 'Seq.empty'</remarks>
162
+
/// <remarks>This function is equivalent to <see cref="traverseAsyncResultM'"/> but applying an initial state of 'Seq.empty'</remarks>
150
163
lettraverseAsyncResultM f xs =
151
164
traverseAsyncResultM' (async{return Ok Seq.empty }) f xs
152
165
@@ -205,7 +218,7 @@ let inline traverseAsyncResultA'
205
218
/// <param name="f">The function to apply to each element</param>
206
219
/// <param name="xs">The input sequence</param>
207
220
/// <returns>An async result with the ok elements in a sequence or a sequence of all errors occuring in the original sequence</returns>
208
-
/// <remarks>This function is equivalent to <see cref="traverseAsyncResultA'"/> but applying and initial state of 'Seq.empty'</remarks>
221
+
/// <remarks>This function is equivalent to <see cref="traverseAsyncResultA'"/> but applying an initial state of 'Seq.empty'</remarks>
209
222
lettraverseAsyncResultA f xs =
210
223
traverseAsyncResultA' (async{return Ok Seq.empty }) f xs
211
224
@@ -256,7 +269,7 @@ let inline traverseOptionM'
256
269
/// <param name="f">The function to apply to each element</param>
257
270
/// <param name="xs">The input sequence</param>
258
271
/// <returns>An option containing Some sequence of elements or None if any of the function applications return None</returns>
259
-
/// <remarks>This function is equivalent to <see cref="traverseOptionM'"/> but applying and initial state of 'Seq.empty'</remarks>
272
+
/// <remarks>This function is equivalent to <see cref="traverseOptionM'"/> but applying an initial state of 'Seq.empty'</remarks>
260
273
lettraverseOptionM f xs = traverseOptionM' (Some Seq.empty) f xs
261
274
262
275
/// <summary>
@@ -311,7 +324,7 @@ let inline traverseAsyncOptionM'
311
324
/// <param name="f">The function to apply to each element</param>
312
325
/// <param name="xs">The input sequence</param>
313
326
/// <returns>An async option containing Some sequence of elements or None if any of the function applications return None</returns>
314
-
/// <remarks>This function is equivalent to <see cref="traverseAsyncOptionM'"/> but applying and initial state of 'Async { return Some Seq.empty }'</remarks>
327
+
/// <remarks>This function is equivalent to <see cref="traverseAsyncOptionM'"/> but applying an initial state of 'Async { return Some Seq.empty }'</remarks>
315
328
lettraverseAsyncOptionM f xs =
316
329
traverseAsyncOptionM' (async{return Some Seq.empty }) f xs
317
330
@@ -364,7 +377,7 @@ let inline traverseVOptionM'
364
377
/// <param name="f">The function to apply to each element</param>
365
378
/// <param name="xs">The input sequence</param>
366
379
/// <returns>A voption containing Some sequence of elements or None if any of the function applications return None</returns>
367
-
/// <remarks>This function is equivalent to <see cref="traverseVOptionM'"/> but applying and initial state of 'ValueSome Seq.empty'</remarks>
380
+
/// <remarks>This function is equivalent to <see cref="traverseVOptionM'"/> but applying an initial state of 'ValueSome Seq.empty'</remarks>
0 commit comments