// Copyright (c) The Geekeey Authors // SPDX-License-Identifier: EUPL-1.2 namespace Geekeey.Extensions.Result; /// /// Extensions for or relating to . /// public static partial class Extensions { /// /// Turns a sequence of results into a single result containing the success values in the results only if all the /// results have success values. /// /// The results to turn into a single sequence. /// The type of the success values in the results. /// A single result containing a sequence of all the success values from the original sequence of results, /// or the first failure value encountered within the sequence. /// /// This method completely enumerates the input sequence before returning and is not lazy. As a consequence of this, /// the sequence within the returned result is an . /// public static Result> Join(this IEnumerable> results) { _ = results.TryGetNonEnumeratedCount(out var count); var list = new List(count); foreach (var result in results) { if (!result.TryGetValue(out var value, out var error)) { return new Result>(error); } list.Add(value); } return list; } /// /// /// For parallel execution of the async tasks, one should await the Task.WhenAll() of the provided list /// before calling this function /// /// // ReSharper disable once InconsistentNaming public static async Task>> Join(this IEnumerable>> results) { _ = results.TryGetNonEnumeratedCount(out var count); var list = new List(count); foreach (var result in results) { if (!(await result).TryGetValue(out var value, out var error)) { return new Result>(error); } list.Add(value); } return list; } /// /// /// For parallel execution of the async tasks, one should await the Task.WhenAll() of the provided list /// before calling this function /// /// // ReSharper disable once InconsistentNaming public static async ValueTask>> Join(this IEnumerable>> results) { _ = results.TryGetNonEnumeratedCount(out var count); var list = new List(count); foreach (var result in results) { if (!(await result).TryGetValue(out var value, out var error)) { return new Result>(error); } list.Add(value); } return list; } }