1 回答

TA貢獻1829條經驗 獲得超13個贊
public static IEnumerable<T> Weave(
this IEnumerable<T> left,
IEnumerable<T> right,
Func<T, T, bool> chooser)
{
using(var leftEnum = left.GetEnumerator())
using(var rightEnum = right.GetEnumerator())
{
bool moreLeft = leftEnum.MoveNext;
bool moreRight = rightEnum.MoveNext;
while(moreLeft && moreRight)
{
if (chooser(leftEnum.Current, rightEnum.Current))
{
yield return leftEnum.Current;
moreLeft = leftEnum.MoveNext();
}
else
{
yield return rightEnum.Current;
moreRight = rightEnum.MoveNext();
}
}
// yield the buffered item, if any
if (moreLeft) yield return leftEnum.Current;
if (moreRight) yield return rightEnum.Current;
// yield any leftover elements
while (leftEnum.MoveNext()) yield return leftEnum.Current;
while (rightEnum.MoveNext()) yield return rightEnum.Current;
}
}
- 1 回答
- 0 關注
- 103 瀏覽
添加回答
舉報