ソースコード
using System;
using System.Collections.Generic;
public static class IEnumerableExt
{
public static Tuple<IEnumerable<T>, IEnumerable<T>> Partition<T>
(
this IEnumerable<T> self,
Func<T, bool> predicate
)
{
var ok = new List<T>();
var ng = new List<T>();
foreach ( var n in self )
{
if ( predicate( n ) )
{
ok.Add( n );
}
else
{
ng.Add( n );
}
}
return Tuple.Create( ( IEnumerable<T> ) ok, ( IEnumerable<T> ) ng );
}
}
使用例1
var list = new[]
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
};
var result = list.Partition( c => c % 2 == 0 );
foreach ( var n in result.Item1 )
{
}
foreach ( var n in result.Item2 )
{
}
使用例2
var list = new[]
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
};
var ( ok, ng ) = list.Partition( c => c % 2 == 0 );
foreach ( var n in ok )
{
}
foreach ( var n in ng )
{
}
参考サイト様