• R/O
  • HTTP
  • SSH
  • HTTPS

Thun: Commit

Interpreter and library.


Commit MetaInfo

Révisioncc8d15ed84ec4186aadfea15107e3f7aeafd3e6b (tree)
l'heure2021-12-24 12:12:17
AuteurSimon Forman <sforman@hush...>
CommiterSimon Forman

Message de Log

Functional-style remove function.

Change Summary

Modification

--- a/joy/library.py
+++ b/joy/library.py
@@ -416,7 +416,8 @@ def sum_(S):
416416 def remove(S):
417417 '''
418418 Expects an item on the stack and a quote under it and removes that item
419- from the the quote. The item is only removed once.
419+ from the the quote. The item is only removed once. If the list is
420+ empty or the item isn't in the list then the list is unchanged.
420421 ::
421422
422423 [1 2 3 1] 1 remove
@@ -424,10 +425,14 @@ def remove(S):
424425 [2 3 1]
425426
426427 '''
427- (tos, (second, stack)) = S
428- l = list(iter_stack(second))
429- l.remove(tos)
430- return list_to_stack(l), stack
428+ (item, (quote, stack)) = S
429+ return _remove(item, quote), stack
430+
431+
432+def _remove(item, quote):
433+ try: head, tail = quote
434+ except ValueError: return quote
435+ return tail if head == item else (head, _remove(item, tail))
431436
432437
433438 @inscribe
Afficher sur ancien navigateur de dépôt.