Skip to content

Commit 0c359ea

Browse files
committed
feat(Util): add foreach enum
1 parent dff76bc commit 0c359ea

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Loop.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public static void Range(int start, int end, Action<int> action)
3232
{
3333
for (int count = start; count < end; ++count)
3434
{
35-
if (action != null)
36-
action.Invoke(count);
35+
action?.Invoke(count);
3736
}
3837
}
3938

@@ -57,8 +56,7 @@ public static void ForEach<T>(ICollection<T> coll, Action<T> action)
5756
{
5857
ForEach(coll, (count, item) =>
5958
{
60-
if (action != null)
61-
action.Invoke(item);
59+
action?.Invoke(item);
6260
});
6361
}
6462
public static void ForEach<T>(ICollection<T> coll, Action<int, T> action)
@@ -67,8 +65,7 @@ public static void ForEach<T>(ICollection<T> coll, Action<int, T> action)
6765

6866
foreach (T item in coll)
6967
{
70-
if (action != null)
71-
action.Invoke(count, item);
68+
action?.Invoke(count, item);
7269

7370
++count;
7471
}
@@ -83,8 +80,7 @@ public static void ForEach<T, V>(IDictionary<T, V> dict, Action<KeyValuePair<T,
8380
{
8481
ForEach(dict, (_, entry) =>
8582
{
86-
if (action != null)
87-
action.Invoke(entry);
83+
action?.Invoke(entry);
8884
});
8985
}
9086
public static void ForEach<T, V>(IDictionary<T, V> dict, Action<int, KeyValuePair<T, V>> action)
@@ -93,8 +89,32 @@ public static void ForEach<T, V>(IDictionary<T, V> dict, Action<int, KeyValuePai
9389

9490
foreach (KeyValuePair<T, V> entry in dict)
9591
{
96-
if (action != null)
97-
action.Invoke(count, entry);
92+
action?.Invoke(count, entry);
93+
94+
++count;
95+
}
96+
}
97+
98+
/// <summary>
99+
/// Loop through enumerator.
100+
/// </summary>
101+
/// <param name="action"> The callback. </param>
102+
public static void ForEach<T>(Action<T> action)
103+
where T : Enum
104+
{
105+
ForEach<T>((_, item) =>
106+
{
107+
action?.Invoke(item);
108+
});
109+
}
110+
public static void ForEach<T>(Action<int, T> action)
111+
where T : Enum
112+
{
113+
int count = 0;
114+
115+
foreach (T item in Enum.GetValues(typeof(T)))
116+
{
117+
action?.Invoke(count, item);
98118

99119
++count;
100120
}

0 commit comments

Comments
 (0)