How would one implement Lazy Evaluation in C?

后端 未结 9 1914
野的像风
野的像风 2021-02-02 13:24

Take for example,

The follow python code:

def multiples_of_2():
  i = 0
  while True:
    i = i + 2
    yield i

How do we translate thi

9条回答
  •  无人及你
    2021-02-02 13:53

    Check out setjmp/longjmp

    setjmp.h is a header defined in the C standard library to provide "non-local jumps," or control flow besides the usual subroutine call and return sequence. The paired functions setjmp and longjmp provide this functionality. First setjmp saves the environment of the calling function into a data structure, and then longjmp can use this structure to "jump" back to the point it was created, at the setjmp call.

    (Lua coroutines were implemented that way)

提交回复
热议问题