Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ForEach LINQ extension. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions LINQExtensions/LINQExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ public static void Shuffle<T>(this List<T> list)
}
}

/// <summary>
/// Applies the action to each element of the container in place.
/// </summary>
/// <typeparam name="T">The type of elemtens in the container.</typeparam>
/// <param name="container">A container whose elements are to be affected.</param>
/// <param name="action">The function to be applied to the elements in the container.</param>
public static void ForEach<T>(this IEnumerable<T> container, Action<T> action)
{
foreach (T element in container)
{
action(element);
}
}

/// <summary>
/// Returns a random element from the array.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions LINQExtensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A collection of extension methods for `IEnumerable`, `List` and arrays.

## Examples

# RandomElement/Shuffle/ToOneLineString
# RandomElement/Shuffle/ToOneLineString/ForEach

```C#
int[] items = new int[] {1, 2, 3, 4, 5};
Expand All @@ -19,6 +19,9 @@ items.Shuffle();
// Example output:
// "3", "5", "2", "1", "4"
Debug.Log(items.ToOneLineString());

// Applies the function that takes the respective type to all the elements
items.ForEach(i => Debug.Log(i));
```

# Nearest
Expand Down Expand Up @@ -46,4 +49,4 @@ public class LINQExtensionsExamples : MonoBehaviour

## Dependencies

None.
None.