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 关注
- 113 浏览
添加回答
举报