Patent ID: 7937694

Claim:
A computer-readable storage medium storing computer-executable instructions for causing a computer system programmed thereby to perform a method comprising: receiving source code, wherein the source code comprises an iterator method for incrementally computing and yielding a sequence of values, the iterator method comprising a yield statement, the yield statement returning a recursive call to the iterator method; and compiling the source code, wherein: the compiling produces enumerator instructions for processing a plurality of nested streams on a stack to produce an unnested stream, the plurality of nested streams includes one or more values, the enumerator instructions implement the iterator method using an enumerator object including a next function operable to return a return value, the enumerator object being specified by the compiling converting the yield statement into at least a portion of the enumerator instructions, the enumerator object operable to call the enumerator instructions for the yield statement no more than once unless the enumerator object is re-initialized, the next function implements the recursive call to the iterator method, the next function operable to return a value associated with the enumerator object as the return value or to return a new instance of an enumerator object as the return value, and the processing comprises, for each currently active stream of the plurality of nested streams: a) pushing a stack record for the currently active stream onto the stack, wherein the stack record comprises an enumerator object associated with the currently active stream; and b) iterating across the currently active stream when the stack record for the currently active stream is at the top of the stack, wherein the iterating across the currently active stream comprises: 1) determining a next item of the currently active stream by executing the next function included with the enumerator object associated with the currently active stream and setting the next item to the return value of the next function, 2) if the next item is a new instance of an enumerator object associated with a nested stream in the currently active stream, pushing a stack record for the next item on the stack, 3) else if the next item is a value, producing the value as the next portion of a flattened unnested stream; and c) popping the stack record for the enumerator object associated with the currently active stream from the stack; and d) producing the flattened unnested stream.