/Utilities/Collections/EnumerableExtensions.cs
C# | 151 lines | 95 code | 10 blank | 46 comment | 0 complexity | f16739cab01468794a14c215d52cca9b MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Collections.Generic;
- using NUnit.Framework;
-
- namespace Delta.Utilities.Collections
- {
- /// <summary>
- /// Extensions to <see cref="IEnumerable{T}"/>, make sure to use this
- /// namespace when using this extensions. Includes ForEach, TryDo, Do and
- /// EnumerateAll methods for IEnumerable items.
- /// </summary>
- public static class EnumerableExtensions
- {
- #region ForEach (Static)
- /// <summary>
- /// Iterates the <paramref name="source"/> and applies the
- /// <paramref name="action"/> to each item.
- /// </summary>
- /// <typeparam name="TItem">TItem</typeparam>
- /// <param name="action">Action</param>
- /// <param name="source">Source</param>
- public static void ForEach<TItem>(this IEnumerable<TItem> source,
- Action<TItem> action)
- {
- foreach (var item in source)
- {
- action(item);
- }
- }
- #endregion
-
- #region TryDo (Static)
- /// <summary>
- /// Allows chaining actions on a set of items for later processing,
- /// filtering or projection (transformation), ignoring exceptions that
- /// might happen in the action.
- /// </summary>
- /// <typeparam name="T">T</typeparam>
- /// <param name="action">Action</param>
- /// <param name="source">Source</param>
- /// <returns>An enumeration with the same items as the source.</returns>
- public static IEnumerable<T> TryDo<T>(this IEnumerable<T> source,
- Action<T> action)
- {
- foreach (var item in source)
- {
- try
- {
- action(item);
- }
- catch
- {
- }
-
- yield return item;
- }
- }
- #endregion
-
- #region Do (Static)
- /// <summary>
- /// Allows chaining actions on a set of items for later processing,
- /// filtering or projection (transformation).
- /// </summary>
- /// <typeparam name="T">T</typeparam>
- /// <param name="action">Action</param>
- /// <param name="source">Source</param>
- /// <returns>An enumeration with the same items as the source.</returns>
- public static IEnumerable<T> Do<T>(this IEnumerable<T> source,
- Action<T> action)
- {
- foreach (var item in source)
- {
- action(item);
- yield return item;
- }
- }
- #endregion
-
- #region EnumerateAll (Static)
- /// <summary>
- /// Enumerates all elements in the source, so that any intermediate
- /// action or side-effect from it is performed eagerly.
- /// Note: Does nothing inside the enumeration!
- /// </summary>
- /// <typeparam name="T">T</typeparam>
- /// <param name="source">Source</param>
- public static void EnumerateAll<T>(this IEnumerable<T> source)
- {
- foreach (var item in source)
- {
- }
- }
- #endregion
-
- /// <summary>
- /// Tests for EnumerableExtensions. Not really useful, we just use some
- /// numbers to test the functionality, but in order to use this class for
- /// real a state machine (e.g. for AI) should be used.
- /// </summary>
- internal class EnumerableExtensionsTests
- {
- #region TestForEach
- [Test]
- public void TestForEach()
- {
- List<int> someNumbers = new List<int>();
- someNumbers.Add(1);
- someNumbers.Add(2);
- someNumbers.Add(5);
- someNumbers.Add(10);
-
- // And add all numbers
- int allNumbers = 0;
- someNumbers.ForEach(delegate(int number)
- {
- allNumbers += number;
- });
- Assert.Equal(allNumbers, 1 + 2 + 5 + 10);
- }
- #endregion
-
- #region TestDo
- [Test, Category("LongRunning")]
- public void TestDo()
- {
- List<int> someNumbers = new List<int>();
- someNumbers.Add(1);
- someNumbers.Add(2);
- someNumbers.Add(5);
- someNumbers.Add(10);
-
- // Define action what to do when calling the Do method below
- Action<int> printNumberAndSquare = delegate(int number)
- {
- Console.WriteLine(
- "Number: " + number + ", Square: " + number * number);
- };
-
- // And print them all out, calling them one by one
- // Note: This is obviously not very useful code, it just shows how
- // to use the Do method. You need more useful state machine code!
- foreach (int number in someNumbers.Do(printNumberAndSquare))
- {
- Console.WriteLine("Processed number: " + number);
- }
- }
- #endregion
- }
- }
- }