Efficient and elegant recursive pushback() function.

Can overflow recursion limit (typically 1000.)
This commit is contained in:
Simon Forman 2018-04-21 11:41:20 -07:00
parent 4064ac1d23
commit a61dc4c5d9
1 changed files with 20 additions and 1 deletions

View File

@ -134,7 +134,26 @@ def pushback(quote, expression):
In joy [1 2] [3 4] would become [1 2 3 4]. In joy [1 2] [3 4] would become [1 2 3 4].
''' '''
return list_to_stack(list(iter_stack(quote)), expression)
# Original implementation.
## return list_to_stack(list(iter_stack(quote)), expression)
# This is slightly faster and won't break the
# recursion limit on long quotes.
## temp = []
## while quote:
## item, quote = quote
## temp.append(item)
## for item in reversed(temp):
## expression = item, expression
## return expression
# This is the fastest, but will trigger
# RuntimeError: maximum recursion depth exceeded
# on quotes longer than sys.getrecursionlimit().
return (quote[0], pushback(quote[1], expression)) if quote else expression
def pick(s, n): def pick(s, n):