日历

September 2020
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
282930  

Reply To: object范式与stream范式

Home Forums 《冒号课堂》讨论区 object范式与stream范式 Reply To: object范式与stream范式

#1269
Todd
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;
}
}
}
}

 请您评分1星(很差)2星(不行)3星(一般)4星(不错)5星(很棒)