From cc8d15ed84ec4186aadfea15107e3f7aeafd3e6b Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Thu, 23 Dec 2021 19:12:17 -0800 Subject: [PATCH] Functional-style remove function. --- joy/library.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/joy/library.py b/joy/library.py index 800ff3b..93455ab 100644 --- a/joy/library.py +++ b/joy/library.py @@ -416,7 +416,8 @@ def sum_(S): def remove(S): ''' Expects an item on the stack and a quote under it and removes that item - from the the quote. The item is only removed once. + from the the quote. The item is only removed once. If the list is + empty or the item isn't in the list then the list is unchanged. :: [1 2 3 1] 1 remove @@ -424,10 +425,14 @@ def remove(S): [2 3 1] ''' - (tos, (second, stack)) = S - l = list(iter_stack(second)) - l.remove(tos) - return list_to_stack(l), stack + (item, (quote, stack)) = S + return _remove(item, quote), stack + + +def _remove(item, quote): + try: head, tail = quote + except ValueError: return quote + return tail if head == item else (head, _remove(item, tail)) @inscribe -- 2.11.0