Map

Accumulateがこれでいいんだったら、Mapも書けるんじゃね?と思って書いてみた。


class Program
{
static void Main( string[] args )
{
List<int> list = new List<int>( new int[] { 1, 2, 3, 4, 5 } );

Converter<int, int> converter = delegate( int n ) { return n * n; };

foreach ( int var in Map<int, int>( converter, list.GetEnumerator() ) )
{
Console.WriteLine( var );
}
Console.WriteLine();

list.ConvertAll( converter ).ForEach( Console.WriteLine );
Console.Read();
}

static IEnumerable<TOutput> Map<TInput, TOutput>( Converter<TInput, TOutput> converter, IEnumerator<TInput> it )
{
while ( it.MoveNext() )
yield return converter( it.Current );
}
}

書いてから、Mapいらなくね?と思った…。
ConvertAllって、Mapみたいなものかも。

#list.ConvertAll( converter ).ForEach( Console.WriteLine ); ってスゴイ書き方だ。