Home › Forums › 《冒号课堂》讨论区 › object范式与stream范式 › Reply To: object范式与stream范式
2010年07月29日 at 1:27 pm
#1269
Member
今天我用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;
}
}
}
}