From a61dc4c5d9bdf3298b646c87fc61666a9b78c6ee Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 21 Apr 2018 11:41:20 -0700 Subject: [PATCH] Efficient and elegant recursive pushback() function. Can overflow recursion limit (typically 1000.) --- joy/utils/stack.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/joy/utils/stack.py b/joy/utils/stack.py index e3f8dd9..211bbd2 100644 --- a/joy/utils/stack.py +++ b/joy/utils/stack.py @@ -134,7 +134,26 @@ def pushback(quote, expression): 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):