@@ -167,6 +167,51 @@ let traverseAsyncResultM f xs =
167167/// <remarks>This function is equivalent to <see cref="traverseAsyncResultM"/> but auto-applying the 'id' function</remarks>
168168let sequenceAsyncResultM xs = traverseAsyncResultM id xs
169169
170+ /// <summary>
171+ /// Applies a function to each element of a sequence and returns a single Task result
172+ /// </summary>
173+ /// <param name="state">The initial state</param>
174+ /// <param name="f">The function to apply to each element</param>
175+ /// <param name="xs">The input sequence</param>
176+ /// <returns>A task result with the ok elements in an array or the first error encountered in the state or the sequence</returns>
177+ let inline traverseTaskResultM '
178+ ( state : TaskResult < 'okOutput seq , 'error >)
179+ ( [<InlineIfLambda>] f : 'okInput -> TaskResult < 'okOutput , 'error >)
180+ ( xs : 'okInput seq )
181+ : TaskResult < 'okOutput [], 'error > =
182+ if isNull xs then
183+ nullArg ( nameof xs)
184+
185+ task {
186+ match ! state with
187+ | Error e -> return Error e
188+ | Ok initialSuccesses ->
189+ let oks = ResizeArray( initialSuccesses)
190+ let mutable ok = true
191+ let mutable err = Unchecked.defaultof< 'error>
192+ use e = xs.GetEnumerator()
193+
194+ while ok
195+ && e.MoveNext() do
196+ match ! f e.Current with
197+ | Ok r -> oks.Add r
198+ | Error e ->
199+ ok <- false
200+ err <- e
201+
202+ return if ok then Ok( oks.ToArray()) else Error err
203+ }
204+
205+ /// <summary>
206+ /// Applies a function to each element of a sequence and returns a single Task result
207+ /// </summary>
208+ /// <param name="f">The function to apply to each element</param>
209+ /// <param name="xs">The input sequence</param>
210+ /// <returns>A task result with the ok elements in an array or the first error occurring in the sequence</returns>
211+ /// <remarks>This function is equivalent to <see cref="traverseTaskResultM'"/> but applying an initial state of 'Seq.empty'</remarks>
212+ let traverseTaskResultM f xs =
213+ traverseTaskResultM' ( TaskResult.ok Seq.empty) f xs
214+
170215/// <summary>
171216/// Applies a function to each element of a sequence and returns a single async result
172217/// </summary>
0 commit comments