You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.5 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IPA.Utilities
  8. {
  9. /// <summary>
  10. /// Extensions for <see cref="IEnumerable{T}"/> that don't currently exist in <c>System.Linq</c>.
  11. /// </summary>
  12. public static class EnumerableExtensions
  13. {
  14. /// <summary>
  15. /// Adds a value to the beginning of the sequence.
  16. /// </summary>
  17. /// <typeparam name="T">the type of the elements of <paramref name="seq"/></typeparam>
  18. /// <param name="seq">a sequence of values</param>
  19. /// <param name="prep">the value to prepend to <paramref name="seq"/></param>
  20. /// <returns>a new sequence beginning with <paramref name="prep"/></returns>
  21. public static IEnumerable<T> Prepend<T>(this IEnumerable<T> seq, T prep)
  22. => new PrependEnumerable<T>(seq, prep);
  23. private sealed class PrependEnumerable<T> : IEnumerable<T>
  24. {
  25. private readonly IEnumerable<T> rest;
  26. private readonly T first;
  27. public PrependEnumerable(IEnumerable<T> rest, T first)
  28. {
  29. this.rest = rest;
  30. this.first = first;
  31. }
  32. public IEnumerator<T> GetEnumerator()
  33. {
  34. yield return first;
  35. foreach (var v in rest) yield return v;
  36. }
  37. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  38. }
  39. }
  40. }