JSB*_*ոգչ 14 c# iterator enumerator
我在解析文本文件时构建了两个数组.第一个包含列名,第二个包含当前行的值.我需要一次迭代两个列表来构建一个地图.现在我有以下内容:
var currentValues = currentRow.Split(separatorChar);
var valueEnumerator = currentValues.GetEnumerator();
foreach (String column in columnList)
{
valueEnumerator.MoveNext();
valueMap.Add(column, (String)valueEnumerator.Current);
}
Run Code Online (Sandbox Code Playgroud)
这很好用,但它并不能完全满足我的优雅感,如果阵列的数量大于2,它会变得非常毛茸茸(我偶尔也会这样做).有没有人有另一个更高级的成语?
Jon*_*eet 22
你的初始代码中有一个非明显的伪bug - IEnumerator<T>扩展IDisposable所以你应该处理它.这对于迭代器块来说非常重要!对于数组来说不是问题,但是与其他IEnumerable<T>实现一样.
我这样做:
public static IEnumerable<TResult> PairUp<TFirst,TSecond,TResult>
(this IEnumerable<TFirst> source, IEnumerable<TSecond> secondSequence,
Func<TFirst,TSecond,TResult> projection)
{
using (IEnumerator<TSecond> secondIter = secondSequence.GetEnumerator())
{
foreach (TFirst first in source)
{
if (!secondIter.MoveNext())
{
throw new ArgumentException
("First sequence longer than second");
}
yield return projection(first, secondIter.Current);
}
if (secondIter.MoveNext())
{
throw new ArgumentException
("Second sequence longer than first");
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,只要有需要,您就可以重复使用:
foreach (var pair in columnList.PairUp(currentRow.Split(separatorChar),
(column, value) => new { column, value })
{
// Do something
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以创建一个通用的Pair类型,并在PairUp方法中删除projection参数.
编辑:
使用Pair类型,调用代码如下所示:
foreach (var pair in columnList.PairUp(currentRow.Split(separatorChar))
{
// column = pair.First, value = pair.Second
}
Run Code Online (Sandbox Code Playgroud)
这看起来就像你能得到的一样简单.是的,您需要将实用程序方法放在某处,作为可重用的代码.在我看来,这几乎不成问题.现在有多个阵列......
如果数组的类型不同,我们就会遇到问题.您不能在泛型方法/类型声明中表达任意数量的类型参数 - 您可以根据需要为尽可能多的类型参数编写PairUp的版本,就像最多4个委托参数的委托Action和Func委托一样- 但是您可以不要随意.
但是,如果这些值都属于同一类型 - 并且如果你很乐意坚持使用数组 - 这很容易.(非阵列也没关系,但你不能提前做长度检查.)你可以这样做:
public static IEnumerable<T[]> Zip<T>(params T[][] sources)
{
// (Insert error checking code here for null or empty sources parameter)
int length = sources[0].Length;
if (!sources.All(array => array.Length == length))
{
throw new ArgumentException("Arrays must all be of the same length");
}
for (int i=0; i < length; i++)
{
// Could do this bit with LINQ if you wanted
T[] result = new T[sources.Length];
for (int j=0; j < result.Length; j++)
{
result[j] = sources[j][i];
}
yield return result;
}
}
Run Code Online (Sandbox Code Playgroud)
那么调用代码将是:
foreach (var array in Zip(columns, row, whatevers))
{
// column = array[0]
// value = array[1]
// whatever = array[2]
}
Run Code Online (Sandbox Code Playgroud)
这涉及到一定程度的复制,当然 - 每次都要创建一个数组.您可以通过引入另一种类型来改变它:
public struct Snapshot<T>
{
readonly T[][] sources;
readonly int index;
public Snapshot(T[][] sources, int index)
{
this.sources = sources;
this.index = index;
}
public T this[int element]
{
return sources[element][index];
}
}
Run Code Online (Sandbox Code Playgroud)
尽管如此,这可能被视为过度杀伤;)
我可以不断提出各种想法,说实话......但基本要素是:
ink*_*dmn 17
如果列名与每行中的元素数相同,那么你不能使用for循环吗?
var currentValues = currentRow.Split(separatorChar);
for(var i=0;i<columnList.Length;i++){
// use i to index both (or all) arrays and build your map
}
Run Code Online (Sandbox Code Playgroud)