Helpers.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Reactive
  6. {
  7. internal static class Helpers
  8. {
  9. public static int? GetLength<T>(IEnumerable<T> source)
  10. {
  11. return source switch
  12. {
  13. T[] array => array.Length,
  14. IList<T> list => list.Count,
  15. _ => null
  16. };
  17. }
  18. public static IObservable<T> Unpack<T>(IObservable<T> source)
  19. {
  20. bool hasOpt;
  21. do
  22. {
  23. hasOpt = false;
  24. if (source is IEvaluatableObservable<T> eval)
  25. {
  26. source = eval.Eval();
  27. hasOpt = true;
  28. }
  29. } while (hasOpt);
  30. return source;
  31. }
  32. public static bool All(this bool[] values)
  33. {
  34. foreach (var value in values)
  35. {
  36. if (!value)
  37. {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. public static bool AllExcept(this bool[] values, int index)
  44. {
  45. for (var i = 0; i < values.Length; i++)
  46. {
  47. if (i != index)
  48. {
  49. if (!values[i])
  50. {
  51. return false;
  52. }
  53. }
  54. }
  55. return true;
  56. }
  57. }
  58. }