Skip to content

Commit d655dd9

Browse files
committed
Rename array and list extension methods
1 parent fd83578 commit d655dd9

File tree

2 files changed

+95
-52
lines changed

2 files changed

+95
-52
lines changed

Runtime/Extensions/ArrayExtensions.cs

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public static T[] Concat<T>(this T[] array, T[] elements)
6767
/// <param name="array">The array to search in.</param>
6868
/// <param name="element">The element to search for.</param>
6969
/// <returns>True if the array contains the element, false otherwise.</returns>
70-
public static bool Contains<T>(this T[] array, T element) where T : IEquatable<T>
70+
public static bool Contains<T>(this T[] array, T element)
71+
where T : IEquatable<T>
7172
{
7273
for (int i = 0; i < array.Length; i++)
7374
{
@@ -79,6 +80,44 @@ public static bool Contains<T>(this T[] array, T element) where T : IEquatable<T
7980
return false;
8081
}
8182

83+
/// <summary>
84+
/// Checks if the array contains an element that satisfies a predicate.
85+
/// </summary>
86+
/// <typeparam name="T">The type of the array.</typeparam>
87+
/// <param name="array">The array to search in.</param>
88+
/// <param name="predicate">The predicate to use.</param>
89+
/// <returns>True if any element satisfies the predicate.</returns>
90+
public static bool Contains<T>(this T[] array, Predicate<T> predicate)
91+
{
92+
for (int i = 0; i < array.Length; i++)
93+
{
94+
if (predicate(array[i]) == true) {
95+
return true;
96+
}
97+
}
98+
99+
return false;
100+
}
101+
102+
/// <summary>
103+
/// Checks if the array contains all elements that satisfy a predicate.
104+
/// </summary>
105+
/// <typeparam name="T">The type of the array.</typeparam>
106+
/// <param name="array">The array to search in.</param>
107+
/// <param name="predicate">The predicate to use.</param>
108+
/// <returns>True if all elements satisfy the predicate.</returns>
109+
public static bool ContainsAll<T>(this T[] array, Predicate<T> predicate)
110+
{
111+
for (int i = 0; i < array.Length; i++)
112+
{
113+
if (predicate(array[i]) == false) {
114+
return false;
115+
}
116+
}
117+
118+
return true;
119+
}
120+
82121
/// <summary>
83122
/// Returns the element at the specified index.
84123
/// </summary>
@@ -263,7 +302,8 @@ public static void ForEach<T>(this T[] array, Action<T> action)
263302
/// <param name="array">The array to search in.</param>
264303
/// <param name="element">The element to search for.</param>
265304
/// <returns>The index of the element in the array.</returns>
266-
public static int IndexOf<T>(this T[] array, T element) where T : IEquatable<T>
305+
public static int IndexOf<T>(this T[] array, T element)
306+
where T : IEquatable<T>
267307
{
268308
for (int i = 0; i < array.Length; i++)
269309
{
@@ -276,41 +316,23 @@ public static int IndexOf<T>(this T[] array, T element) where T : IEquatable<T>
276316
}
277317

278318
/// <summary>
279-
/// Checks if any element in the array satisfies a predicate.
319+
/// Returns the index of the first element in the array that satisfies
320+
/// the predicate.
280321
/// </summary>
281322
/// <typeparam name="T">The type of the array.</typeparam>
282323
/// <param name="array">The array to search in.</param>
283324
/// <param name="predicate">The predicate to use.</param>
284-
/// <returns>True if any element satisfies the predicate.</returns>
285-
public static bool IsAny<T>(this T[] array, Predicate<T> predicate)
325+
/// <returns>The index of the element, or -1 if not found.</returns>
326+
public static int IndexOf<T>(this T[] array, Predicate<T> predicate)
286327
{
287328
for (int i = 0; i < array.Length; i++)
288329
{
289330
if (predicate(array[i]) == true) {
290-
return true;
291-
}
292-
}
293-
294-
return false;
295-
}
296-
297-
/// <summary>
298-
/// Checks if each element in the array satisfies a predicate.
299-
/// </summary>
300-
/// <typeparam name="T">The type of the array.</typeparam>
301-
/// <param name="array">The array to search in.</param>
302-
/// <param name="predicate">The predicate to use.</param>
303-
/// <returns>True if all elements satisfy the predicate.</returns>
304-
public static bool IsEach<T>(this T[] array, Predicate<T> predicate)
305-
{
306-
for (int i = 0; i < array.Length; i++)
307-
{
308-
if (predicate(array[i]) == false) {
309-
return false;
331+
return i;
310332
}
311333
}
312334

313-
return true;
335+
return -1;
314336
}
315337

316338
/// <summary>

Runtime/Extensions/ListExtensions.cs

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,45 @@ public static List<T> CombinedWith<T>(this List<T> list, List<T> other)
4242
return combinedList;
4343
}
4444

45+
46+
/// <summary>
47+
/// Checks if the list contains an item that satisfies a predicate.
48+
/// </summary>
49+
/// <typeparam name="T">The type of the list.</typeparam>
50+
/// <param name="list">The list to search in.</param>
51+
/// <param name="predicate">The predicate to use.</param>
52+
/// <returns>True if any item satisfies the predicate.</returns>
53+
public static bool Contains<T>(this List<T> list, Predicate<T> predicate)
54+
{
55+
foreach (T item in list)
56+
{
57+
if (predicate(item) == true) {
58+
return true;
59+
}
60+
}
61+
62+
return false;
63+
}
64+
65+
/// <summary>
66+
/// Checks if the list contains all items that satisfy a predicate.
67+
/// </summary>
68+
/// <typeparam name="T">The type of the list.</typeparam>
69+
/// <param name="list">The list to search in.</param>
70+
/// <param name="predicate">The predicate to use.</param>
71+
/// <returns>True if all items satisfy the predicate.</returns>
72+
public static bool ContainsAll<T>(this List<T> list, Predicate<T> predicate)
73+
{
74+
foreach (T item in list)
75+
{
76+
if (predicate(item) == false) {
77+
return false;
78+
}
79+
}
80+
81+
return true;
82+
}
83+
4584
/// <summary>
4685
/// Returns the item at the specified index.
4786
/// </summary>
@@ -137,41 +176,23 @@ public static void For<T>(this List<T> list, Action<(T item, int index)> action)
137176
}
138177

139178
/// <summary>
140-
/// Checks if any item in the list satisfies a predicate.
141-
/// </summary>
142-
/// <typeparam name="T">The type of the list.</typeparam>
143-
/// <param name="list">The list to search in.</param>
144-
/// <param name="predicate">The predicate to use.</param>
145-
/// <returns>True if any item satisfies the predicate.</returns>
146-
public static bool IsAny<T>(this List<T> list, Predicate<T> predicate)
147-
{
148-
foreach (T item in list)
149-
{
150-
if (predicate(item) == true) {
151-
return true;
152-
}
153-
}
154-
155-
return false;
156-
}
157-
158-
/// <summary>
159-
/// Checks if each item in the list satisfies a predicate.
179+
/// Returns the index of the first item in the list that satisfies the
180+
/// predicate.
160181
/// </summary>
161182
/// <typeparam name="T">The type of the list.</typeparam>
162183
/// <param name="list">The list to search in.</param>
163184
/// <param name="predicate">The predicate to use.</param>
164-
/// <returns>True if all items satisfy the predicate.</returns>
165-
public static bool IsEach<T>(this List<T> list, Predicate<T> predicate)
185+
/// <returns>The index of the item, or -1 if not found.</returns>
186+
public static int IndexOf<T>(this List<T> list, Predicate<T> predicate)
166187
{
167-
foreach (T item in list)
188+
for (int i = 0; i < list.Count; i++)
168189
{
169-
if (predicate(item) == false) {
170-
return false;
190+
if (predicate(list[i]) == true) {
191+
return i;
171192
}
172193
}
173194

174-
return true;
195+
return -1;
175196
}
176197

177198
/// <summary>

0 commit comments

Comments
 (0)