From 089fe70e184b0c0f392c7dc4a3857f625e40af1b Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sat, 7 Dec 2019 23:44:26 -0600 Subject: [PATCH] Added own implementation of IEnumerable.Prepend --- IPA.Loader/IPA.Loader.csproj | 1 + IPA.Loader/Utilities/EnumerableExtensions.cs | 45 ++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 IPA.Loader/Utilities/EnumerableExtensions.cs diff --git a/IPA.Loader/IPA.Loader.csproj b/IPA.Loader/IPA.Loader.csproj index 9e0a7a0e..1785dc84 100644 --- a/IPA.Loader/IPA.Loader.csproj +++ b/IPA.Loader/IPA.Loader.csproj @@ -131,6 +131,7 @@ + diff --git a/IPA.Loader/Utilities/EnumerableExtensions.cs b/IPA.Loader/Utilities/EnumerableExtensions.cs new file mode 100644 index 00000000..61ba93a4 --- /dev/null +++ b/IPA.Loader/Utilities/EnumerableExtensions.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IPA.Utilities +{ + /// + /// Extensions for that don't currently exist in System.Linq. + /// + public static class EnumerableExtensions + { + /// + /// Adds a value to the beginning of the sequence. + /// + /// the type of the elements of + /// a sequence of values + /// the value to prepend to + /// a new sequence beginning with + public static IEnumerable Prepend(this IEnumerable seq, T prep) + => new PrependEnumerable(seq, prep); + + private sealed class PrependEnumerable : IEnumerable + { + private readonly IEnumerable rest; + private readonly T first; + + public PrependEnumerable(IEnumerable rest, T first) + { + this.rest = rest; + this.first = first; + } + + public IEnumerator GetEnumerator() + { + yield return first; + foreach (var v in rest) yield return v; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + } +}