all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#21782: 25.0.50; New functions nfront/front
@ 2015-10-29  9:57 Tino Calancha
  2015-10-29 15:25 ` Artur Malabarba
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Tino Calancha @ 2015-10-29  9:57 UTC (permalink / raw)
  To: 21782

[-- Attachment #1: Type: text/plain, Size: 616 bytes --]


In GNU Emacs 25.0.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 2.24.23)
  of 2015-10-29
Repository revision: 07830c3d5c801d7d55622215b46ba692c6afa1d2


I found very useful to define one function returning:
(nreverse (last (nreverse list) N))
(for a given list and integer N)

Maybe its already defined somewhere, but i cannot find it.

Examples of the new function:
(let ((ltest '( 1 2 3 4 5 6)))
   (nfront ltest 3))
(1 2 3)

(let ((ltest '( 1 2 3 4 5 6)))
   (nfront ltest 1))
(1)

(let ((ltest '( 1 2 3 4 5 6)))
   (nfront ltest 20))
(1 2 3 4 5 6)

(let ((ltest '( 1 2 3 4 5 6)))
   (nfront ltest))
(1 2 3 4 5 6)

[-- Attachment #2: Type: text/plain, Size: 840 bytes --]

diff --git a/lisp/subr.el b/lisp/subr.el
index ea926ae..976e4fd 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -405,6 +405,19 @@ nbutlast
 	   (if (> n 0) (setcdr (nthcdr (- (1- m) n) list) nil))
 	   list))))
 
+(defun front(list &optional n)
+  "Return a copy of LIST with just the first N elements.
+If N is omitted or nil the full list is copied."
+(let ((m (length list)))
+  (if (and n (<= n 0)) list
+    (nfront (copy-sequence list) n))))
+
+(defun nfront(list &optional n)
+  "Modified LIST to remove all elements but the first N.
+If N is omitted or nil the full list is copied."
+  (if (or (null n) (and n (<= n 0))) list
+    (nreverse (last (nreverse list) n))))
+
 (defun zerop (number)
   "Return t if NUMBER is zero."
   ;; Used to be in C, but it's pointless since (= 0 n) is faster anyway because

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2017-05-18  4:08 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-29  9:57 bug#21782: 25.0.50; New functions nfront/front Tino Calancha
2015-10-29 15:25 ` Artur Malabarba
2015-10-30  1:25   ` Tino Calancha
2015-10-30  2:38     ` Michael Heerdegen
2015-10-30  2:53       ` Constantino Calancha
2015-10-30  9:45         ` Artur Malabarba
2015-10-30 10:10           ` Tino Calancha
2015-10-30 10:03         ` Nicolas Petton
2015-10-30 10:11           ` Tino Calancha
2015-10-30 14:09         ` Michael Heerdegen
2015-10-30 18:06           ` Tino Calancha
2015-10-30  2:10   ` Tino Calancha
2015-10-30  1:33 ` Richard Stallman
2015-10-30  1:41   ` Constantino Calancha
     [not found] ` <mailman.1293.1446168906.7904.bug-gnu-emacs@gnu.org>
2015-10-31 22:44   ` Alan Mackenzie
2017-05-18  4:08 ` Tino Calancha

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.