今天我用C#的yield实现了一个无限流的Stack。Stack的输入是(operation, data)的流,输出是(data)流:
class Stack{
public static IEnumerable<int> Transform(IEnumerable<Command> aSourceStream){
LinkedList<int> list = new LinkedList<int>();
foreach(Command command in aSourceStream){
if(OperationType.PUSH== command.Operation){
list.AddLast(command.Data);
}
else if (OperationType.POP == command.Operation){
int data = list.Last.Value;
list.RemoveLast();
yield return data;
}
}
}
}