public static class IteratorExtensions{
public static IEnumerable
MakeCustomIterator(
this TCollection collection,
TCursor cursor,
Func getCurrent,
Func isFinished,
Func advanceCursor){
while(!isFinished(cursor)){
yield return getCursor(collection,cursor)
cursor = advanceCursor(cursor);
}
}
}
public class IteratorExample{
static void Main(){
var matrix = new List>{
new List {1.0,1.1,1.2},
new List {2.0,2.1,2.2},
new List {3.0,3.2,3.3}
};
var iter = matrix.MakeCustomIterator(
new int[]{0,0},
(coll,cur)=>coll[cur[0]][cur[1]],
(cur)=>cur[0] >2||cur[1]>2,
(cur)=>new int[]{cur[0]+1,cur[1]+1});
foreach(var item in iter){
Console.WriteLine(item);
}
}
}
我想说C#让我学过的stl和scheme的思想都回来了,甚至更多。
阅读(1340) | 评论(1) | 转发(0) |