[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Coroutines



Dan Weinreb wrote:
> 
>....
> From time to time you find yourself in a situation where both parties
> really want stack state.  

Here's an example from Python 2.2.

import random
def randomwalk_generator():
    last, rand = 1, random.random() # initialize candidate elements
    while rand > 0.1:               # threshhold terminator
        print '*',                  # display the rejection
        if abs(last-rand) >= 0.4:   # accept the number
            last = rand             # update prior value
            yield rand              # return AT THIS POINT
        rand = random.random()      # new candidate
    yield rand                      # return the final small element

for num in randomwalk_generator():
    print_short(num)

Ruby also has a yield keyword and I think it can be used in this way but
is more general than Python's.

 Paul Prescod