all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tino Calancha <f92capac@gmail.com>
To: 21782@debbugs.gnu.org
Cc: Artur Malabarba <bruce.connor.am@gmail.com>
Subject: bug#21782: 25.0.50; New functions nfront/front
Date: Fri, 30 Oct 2015 10:25:31 +0900 (JST)	[thread overview]
Message-ID: <alpine.LRH.2.20.1510301017410.10242@calancha-ilc.kek.jp> (raw)
In-Reply-To: <CAAdUY-JKjfvQSXs4SqZK=6+A69H4b5g8GDiGvEmJoRURWUgWww@mail.gmail.com>

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


> I think this is already available in core as seq-take, from the seq package.

Thank you, you are right. Indeed, I found seq-take very nice:
its more general because apply to sequences.
There is one small difference: seq-take has no optional arguments.

In addition, seq.el need to be loaded by the user. Yeah, not a big
deal, but IMO such fundamental operation on a list as nfront 
implement, deserve to be available at the starting of the session.
Putting a dedicated function in subr.el we save to load many stuff from
seq.el that maybe we dont need.

I would like nfront/front would behave as nbutlast/butlast concerning
the N argument:
*) N nil giving same output as N = 1.
*) N <= 0 return the full list.
(I choose the name of the function to somehow reflect such symmetry).

I have modified my patch to accomplish such behaviour.

With the new patch

(let ((ltest '(1 2 3 4 5 6)))
   (nbutlast ltest nil))
(1 2 3 4 5)
(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest nil))
(1)
(require 'seq)
(let ((ltest '(1 2 3 4 5 6)))
   (seq-take ltest nil)); error: second argument is not optional

(let ((ltest '(1 2 3 4 5 6)))
   (nbutlast ltest 0))
(1 2 3 4 5 6)
(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest 0))
(1 2 3 4 5 6)
(let ((ltest '(1 2 3 4 5 6)))
   (seq-take ltest 0))
nil

(let ((ltest '(1 2 3 4 5 6)))
   (nbutlast ltest -1))
(1 2 3 4 5 6)
(let ((ltest '(1 2 3 4 5 6)))
   (nfront ltest -1))
(1 2 3 4 5 6)
(let ((ltest '(1 2 3 4 5 6)))
   (seq-take ltest -1))
nil

(let ((ltest '(1 2 3 4 5 6)))
   (nbutlast ltest 20))
nil
(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)))
   (seq-take ltest 20))
(1 2 3 4 5 6)

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

diff --git a/lisp/subr.el b/lisp/subr.el
index ea926ae..ef705eb 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -391,13 +391,15 @@ last
 (defun butlast (list &optional n)
   "Return a copy of LIST with the last N elements removed.
 If N is omitted or nil, the last element is removed from the
-copy."
+copy.
+When N is <= 0 LIST is returned."
   (if (and n (<= n 0)) list
     (nbutlast (copy-sequence list) n)))
 
 (defun nbutlast (list &optional n)
   "Modifies LIST to remove the last N elements.
-If N is omitted or nil, remove the last element."
+If N is omitted or nil, remove the last element.
+When N is <= 0 LIST is returned unmodified."
   (let ((m (length list)))
     (or n (setq n 1))
     (and (< n m)
@@ -405,6 +407,21 @@ 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 is omitted or nil copy just the first element
+When N is <= 0 the full list is copied."
+  (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 remove all but the first element
+When N is <= 0 LIST is returned unmodified."
+  (or n (setq n 1))
+  (if (<= 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

  reply	other threads:[~2015-10-30  1:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LRH.2.20.1510301017410.10242@calancha-ilc.kek.jp \
    --to=f92capac@gmail.com \
    --cc=21782@debbugs.gnu.org \
    --cc=bruce.connor.am@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.