Added simple String.Split extension
[IEnumerableExtras.git] / C# / EnumerableMap.cs
blob44a275387952befed5d334c9bc94cac8d1e01b39
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
5 namespace IEnumerableExtras
7 /// <summary>
8 /// <see cref="IEnumerable{T}"/> map extension.
9 /// </summary>
10 public static class EnumerableMap
12 /// <summary>
13 /// Produce new instance of <see cref="IEnumerable{TResult}"/> by applying
14 /// predicate to each element in this instance of <see cref="IEnumerable"/>.
15 /// </summary>
16 /// <typeparam name="TResult">Resulting collection item type.</typeparam>
17 /// <param name="collection">This instance of <see cref="IEnumerable{T}"/>.</param>
18 /// <param name="predicate">Function returnting objects of type <typeparamref name="TResult"/> based on objects of type <typeparamref name="T"/>.</param>
19 /// <returns></returns>
20 public static IEnumerable<TResult> Map< TResult >( this IEnumerable collection, Func<object, TResult> predicate )
22 foreach ( var item in collection )
24 yield return predicate( item );
28 /// <summary>
29 /// Produce new instance of <see cref="IEnumerable{TResult}"/> by applying
30 /// predicate to each element in this instance of <see cref="IEnumerable{T}"/>.
31 /// </summary>
32 /// <typeparam name="T">This collection's item type.</typeparam>
33 /// <typeparam name="TResult">Resulting collection item type.</typeparam>
34 /// <param name="collection">This instance of <see cref="IEnumerable{T}"/>.</param>
35 /// <param name="predicate">Function returnting objects of type <typeparamref name="TResult"/> based on objects of type <typeparamref name="T"/>.</param>
36 /// <returns></returns>
37 public static IEnumerable<TResult> Map< T, TResult >( this IEnumerable<T> collection, Func<T, TResult> predicate )
39 foreach ( var item in collection )
41 yield return predicate( item );
45 public static IEnumerable<TResult> Map< T, TResult >( this IEnumerable<T> collection, Func<T, int, TResult> predicate )
47 int i = 0;
48 foreach ( var item in collection )
50 yield return predicate( item, i++ );