Skip to main content
deleted 40 characters in body
Source Link
Claudiu
  • 227.4k
  • 169
  • 499
  • 692

There's one extra thing to mention: a function that yields doesn't actually have to terminate. I've written code like this:

def fib():
    yield 1
 last, cur = yield0, 1
    curwhile =True: 1
    last = 1
    whileyield True:cur
        cur, last = cur+last, cur
     = cur, last yield+ cur

Then I can use it in other code like this:

for f in fib():
    if some_condition: break
    coolfuncs(f);

It really helps simplify some problems, and makes some things easier to work with.

There's one extra thing to mention: a function that yields doesn't actually have to terminate. I've written code like this:

def fib():
    yield 1
    yield 1
    cur = 1
    last = 1
    while True:
        cur, last = cur+last, cur
        yield cur

Then I can use it in other code like this:

for f in fib():
    if some_condition: break
    coolfuncs(f);

It really helps simplify some problems, and makes some things easier to work with.

There's one extra thing to mention: a function that yields doesn't actually have to terminate. I've written code like this:

def fib():
    last, cur = 0, 1
    while True: 
        yield cur
        last, cur = cur, last + cur

Then I can use it in other code like this:

for f in fib():
    if some_condition: break
    coolfuncs(f);

It really helps simplify some problems, and makes some things easier to work with.

Source Link
Claudiu
  • 227.4k
  • 169
  • 499
  • 692

There's one extra thing to mention: a function that yields doesn't actually have to terminate. I've written code like this:

def fib():
    yield 1
    yield 1
    cur = 1
    last = 1
    while True:
        cur, last = cur+last, cur
        yield cur

Then I can use it in other code like this:

for f in fib():
    if some_condition: break
    coolfuncs(f);

It really helps simplify some problems, and makes some things easier to work with.