summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorMattias EngdegÄrd <mattiase@acm.org>2022-07-13 13:46:52 +0200
committerMattias EngdegÄrd <mattiase@acm.org>2022-07-17 17:35:49 +0200
commitd62766305ad8fe6ca1695341c34b9836d051e3cb (patch)
tree3bb041d82a695bc4746d7c8f0fe79939ec80d0bc /doc
parent637436970f34f860d50f73a514b3bafd0c5cace7 (diff)
downloademacs-d62766305ad8fe6ca1695341c34b9836d051e3cb.tar.gz
Add `take` and `ntake` (bug#56521)
These are useful list primitives, complementary to `nthcdr`. * src/fns.c (Ftake, Fntake): New. (syms_of_fns): Defsubr them. * doc/lispref/lists.texi (List Elements): * lisp/emacs-lisp/shortdoc.el (list): Document. * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns, pure-fns): Declare `take` pure and side-effect-free. * test/src/fns-tests.el (fns-tests--take-ref, fns--take-ntake): New test. * etc/NEWS: Announce.
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/lists.texi29
1 files changed, 29 insertions, 0 deletions
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi
index a4f0ba815b1..2a9ad1d5e00 100644
--- a/doc/lispref/lists.texi
+++ b/doc/lispref/lists.texi
@@ -340,6 +340,35 @@ If @var{n} is zero, @code{nthcdr} returns all of
@end example
@end defun
+@defun take n list
+This function returns the @var{n} first elements of @var{list}. Essentially,
+it returns the part of @var{list} that @code{nthcdr} skips.
+
+@code{take} returns @var{list} if it is shorter than @var{n} elements;
+it returns @code{nil} if @var{n} is zero or negative.
+
+@example
+@group
+(take 3 '(a b c d))
+ @result{} (a b c)
+@end group
+@group
+(take 10 '(a b c d))
+ @result{} (a b c d)
+@end group
+@group
+(take 0 '(a b c d))
+ @result{} nil
+@end group
+@end example
+@end defun
+
+@defun ntake n list
+This is a version of @code{take} that works by destructively modifying
+the list structure of the argument. That makes it faster, but the
+original value of @var{list} is lost.
+@end defun
+
@defun last list &optional n
This function returns the last link of @var{list}. The @code{car} of
this link is the list's last element. If @var{list} is null,