Subject: bug#21782: closed (Re: bug#21782: 25.0.50; New functions nfront/front)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 18 May 2017 04:10:02 +0000

Your bug report

#21782: 25.0.50; New functions nfront/front

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
21782: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=21782
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#21782: 25.0.50; New functions nfront/front
From: Tino Calancha <[email-masked]>
To: [email-masked]
Date: Thu, 18 May 2017 13:08:57 +0900
List-Id: <debbugs-submit.debbugs.gnu.org>

Tino Calancha <[email-masked]> writes:

> 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.
seq-take does the same thing, so i am closing this report.

Subject: 25.0.50; New functions nfront/front
From: Tino Calancha <[email-masked]>
To: [email-masked]
Date: Thu, 29 Oct 2015 18:57:59 +0900 (JST)
List-Id: <debbugs-submit.debbugs.gnu.org>

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)

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