// Kyoo - A portable and vast media library solution. // Copyright (c) Kyoo. // // See AUTHORS.md and LICENSE file in the project root for full license information. // // Kyoo is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // Kyoo is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . using System; using System.Collections.Generic; namespace Kyoo.Utils; /// /// A set of extensions class for enumerable. /// public static class EnumerableExtensions { /// /// If the enumerable is empty, execute an action. /// /// The enumerable to check /// The action to execute is the list is empty /// The type of items inside the list /// The iterator proxied, there is no dual iterations. public static IEnumerable IfEmpty(this IEnumerable self, Action action) { static IEnumerable Generator(IEnumerable self, Action action) { using IEnumerator enumerator = self.GetEnumerator(); if (!enumerator.MoveNext()) { action(); yield break; } do { yield return enumerator.Current; } while (enumerator.MoveNext()); } return Generator(self, action); } /// /// A foreach used as a function with a little specificity: the list can be null. /// /// The list to enumerate. If this is null, the function result in a no-op /// The action to execute for each arguments /// The type of items in the list public static void ForEach(this IEnumerable? self, Action action) { if (self == null) return; foreach (T i in self) action(i); } }