summaryrefslogtreecommitdiff
path: root/module/srfi/srfi-1.scm
diff options
context:
space:
mode:
authorRob Browning <rlb@defaultvalue.org>2024-07-16 23:19:15 -0500
committerRob Browning <rlb@defaultvalue.org>2024-07-30 18:54:45 -0500
commita816b2484bae69edaf78ae2b0cb0c8f6005e0a8b (patch)
tree0545b3b4070936ec81059249f294c7d6f9e4080c /module/srfi/srfi-1.scm
parentc5f26d4c27cd72252291935026a5a0946bd38e4a (diff)
downloadguile-a816b2484bae69edaf78ae2b0cb0c8f6005e0a8b.tar.gz
srfi-1 delete delete!: move from C to Scheme
* libguile/srfi-1.c (scm_srfi1_delete, scm_srfi1_delete_x): delete. * libguile/srfi-1.h (scm_srfi1_delete, scm_srfi1_delete_x): delete. * module/srfi/srfi-1.scm: add delete and delete!.
Diffstat (limited to 'module/srfi/srfi-1.scm')
-rw-r--r--module/srfi/srfi-1.scm35
1 files changed, 35 insertions, 0 deletions
diff --git a/module/srfi/srfi-1.scm b/module/srfi/srfi-1.scm
index 3411b7e3c..8d0a603cd 100644
--- a/module/srfi/srfi-1.scm
+++ b/module/srfi/srfi-1.scm
@@ -982,6 +982,41 @@ CLIST1 ... CLISTN, that satisfies PRED."
(else
(lp (map cdr lists) (+ i 1)))))))
+;;; Deletion
+
+(define* (delete x lst #:optional (pred equal?))
+ "Return a list containing the elements of @var{lst} but with
+those equal to @var{x} deleted. The returned elements will be in the
+same order as they were in @var{lst}.
+
+Equality is determined by @var{pred}, or @code{equal?} if not given. An
+equality call is made just once for each element, but the order in which
+the calls are made on the elements is unspecified.
+
+The equality calls are always @code{(pred x elem)}, ie.@: the given
+@var{x} is first. This means for instance elements greater than 5 can
+be deleted with @code{(delete 5 lst <)}.
+
+@var{lst} is not modified, but the returned list might share a common
+tail with @var{lst}."
+ (remove (lambda (elem) (pred x elem)) lst))
+
+(define* (delete! x lst #:optional (pred equal?))
+ "Return a list containing the elements of @var{lst} but with
+those equal to @var{x} deleted. The returned elements will be in the
+same order as they were in @var{lst}.
+
+Equality is determined by @var{pred}, or @code{equal?} if not given. An
+equality call is made just once for each element, but the order in which
+the calls are made on the elements is unspecified.
+
+The equality calls are always @code{(pred x elem)}, ie.@: the given
+@var{x} is first. This means for instance elements greater than 5 can
+be deleted with @code{(delete 5 lst <)}.
+
+@var{lst} may be modified to construct the returned list."
+ (remove! (lambda (elem) (pred x elem)) lst))
+
;;; Association lists
(define alist-cons acons)