PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace/Utilities/Functional.cs

https://bitbucket.org/VahidN/interlace
C# | 181 lines | 117 code | 39 blank | 25 comment | 16 complexity | f3f20360c3eaf7f524dc7b5c840a16e3 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. #endregion
  32. namespace Interlace.Utilities
  33. {
  34. public delegate T BinaryOperator<T>(T lhs, T rhs);
  35. public static class Functional
  36. {
  37. public static List<TDestination> CastList<TDestination>(IEnumerable source)
  38. {
  39. List<TDestination> destination = new List<TDestination>();
  40. foreach (object value in source)
  41. {
  42. destination.Add((TDestination)value);
  43. }
  44. return destination;
  45. }
  46. public static void ApplyAdd<TSource, TDestination>(IEnumerable<TSource> source, ICollection<TDestination> destination)
  47. where TSource : TDestination
  48. {
  49. foreach (TSource obj in source)
  50. {
  51. destination.Add(obj);
  52. }
  53. }
  54. public static void ApplyAdd<TSource, TDestination>(IEnumerable<TSource> source, int offset, int length, ICollection<TDestination> destination)
  55. where TSource : TDestination
  56. {
  57. IEnumerator<TSource> enumerator = source.GetEnumerator();
  58. int i = 0;
  59. while (i < offset)
  60. {
  61. if (!enumerator.MoveNext()) break;
  62. i++;
  63. }
  64. while (i < offset + length)
  65. {
  66. if (!enumerator.MoveNext()) break;
  67. destination.Add(enumerator.Current);
  68. i++;
  69. }
  70. }
  71. public static bool Contains<T>(IEnumerable<T> list, T obj)
  72. {
  73. foreach (T candidate in list)
  74. {
  75. if (obj.Equals(candidate)) return true;
  76. }
  77. return false;
  78. }
  79. public static T Reduce<T>(BinaryOperator<T> binaryOperator, IEnumerable<T> list)
  80. {
  81. IEnumerator<T> e = list.GetEnumerator();
  82. if (!e.MoveNext()) return default(T);
  83. T accumulator = e.Current;
  84. while (e.MoveNext())
  85. {
  86. accumulator = binaryOperator(accumulator, e.Current);
  87. }
  88. return accumulator;
  89. }
  90. public static int IndexOf<T>(IEnumerable<T> list, T obj)
  91. {
  92. int i = 0;
  93. foreach (T candidate in list)
  94. {
  95. if (object.Equals(candidate, obj))
  96. {
  97. return i;
  98. }
  99. i++;
  100. }
  101. return -1;
  102. }
  103. public static bool ListStartsWith<T>(IEnumerable<T> list, IEnumerable<T> prefix)
  104. {
  105. IEnumerator<T> prefixEnumerator = prefix.GetEnumerator();
  106. IEnumerator<T> listEnumerator = list.GetEnumerator();
  107. while (prefixEnumerator.MoveNext())
  108. {
  109. if (!listEnumerator.MoveNext()) return false;
  110. if (!object.Equals(prefixEnumerator.Current, listEnumerator.Current)) return false;
  111. }
  112. return true;
  113. }
  114. public static T[] RemovePrefix<T>(IEnumerable<T> list, IEnumerable<T> prefix)
  115. {
  116. IEnumerator<T> prefixEnumerator = prefix.GetEnumerator();
  117. IEnumerator<T> listEnumerator = list.GetEnumerator();
  118. while (true)
  119. {
  120. if (!listEnumerator.MoveNext()) return new T[] { };
  121. if (!prefixEnumerator.MoveNext()) break;
  122. if (!object.Equals(prefixEnumerator.Current, listEnumerator.Current)) break;
  123. }
  124. List<T> newList = new List<T>();
  125. do
  126. {
  127. newList.Add(listEnumerator.Current);
  128. }
  129. while (listEnumerator.MoveNext());
  130. return newList.ToArray();
  131. }
  132. public static T[] Concatenate<T>(params IEnumerable<T>[] lists)
  133. {
  134. List<T> newList = new List<T>();
  135. foreach (IEnumerable<T> list in lists)
  136. {
  137. newList.AddRange(list);
  138. }
  139. return newList.ToArray();
  140. }
  141. }
  142. }