Skip to content

Instantly share code, notes, and snippets.

@vrthra
Last active April 17, 2022 20:32
Show Gist options
  • Select an option

  • Save vrthra/61b30c9bfdfa6fb9f8efbd56b8239f7b to your computer and use it in GitHub Desktop.

Select an option

Save vrthra/61b30c9bfdfa6fb9f8efbd56b8239f7b to your computer and use it in GitHub Desktop.

Revisions

  1. vrthra revised this gist Apr 17, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions recursive_call.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # https://speakerdeck.com/dabeaz/generators-the-final-frontier?slide=163
    def cps(gen):
    stack = [gen]
    ret = None
  2. vrthra renamed this gist Apr 17, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. vrthra created this gist Apr 17, 2022.
    37 changes: 37 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    def cps(gen):
    stack = [gen]
    ret = None
    while stack:
    try:
    value, ret = ret, None
    res = stack[-1].send(value)
    stack.append(res)
    except StopIteration as e:
    stack.pop()
    ret = e.value
    return ret

    def factorial(n):
    if n <= 1:
    return 1
    value = factorial(n - 1)
    return value * n

    #print(factorial(1000))

    def factorial(n):
    if n <= 1:
    return 1
    value = yield factorial(n - 1)
    return value * n

    print(cps(factorial(10000)))

    def countdown(start):
    print start
    if start == 0:
    return 0
    else:
    return countdown(start - 1)

    print(cps(countdown(1000)))