all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Extract sublists
@ 2009-11-17 13:22 Nordlöw
  2009-11-17 13:24 ` Nordlöw
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Nordlöw @ 2009-11-17 13:22 UTC (permalink / raw)
  To: help-gnu-emacs

Is there a function for extracting sublists of lists?

If not here is my suggestion for inclusion in Emacs.

(defun sublist (list from to)
  "Return a sublist of LIST, from FROM to TO.
Counting starts at 0. Like `substring' but for lists."
  (let (rtn (c from))
    (setq list (nthcdr from list))
    (while (and list (< c to))
      (push (pop list) rtn)
      (setq c (1+ c)))
    (nreverse rtn)))
;; Use: (sublist '(a b) 0 0)
;; Use: (sublist '(a b) 0 1)
;; Use: (sublist '(a b) 1 2)
;; Use: (sublist '(a b) 0 2)

Thanks,
Nordlöw


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

* Re: Extract sublists
  2009-11-17 13:22 Extract sublists Nordlöw
@ 2009-11-17 13:24 ` Nordlöw
  2009-11-17 14:01 ` LanX
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Nordlöw @ 2009-11-17 13:24 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 17, 2:22 pm, Nordlöw <per.nord...@gmail.com> wrote:
> Is there a function for extracting sublists of lists?
>
> If not here is my suggestion for inclusion in Emacs.
>
> (defun sublist (list from to)
>   "Return a sublist of LIST, from FROM to TO.
> Counting starts at 0. Like `substring' but for lists."
>   (let (rtn (c from))
>     (setq list (nthcdr from list))
>     (while (and list (< c to))
>       (push (pop list) rtn)
>       (setq c (1+ c)))
>     (nreverse rtn)))
> ;; Use: (sublist '(a b) 0 0)
> ;; Use: (sublist '(a b) 0 1)
> ;; Use: (sublist '(a b) 1 2)
> ;; Use: (sublist '(a b) 0 2)
>
> Thanks,
> Nordlöw

Modified from org-sublist() but with 0-begin-indexing analogously with
substring.

/Nordlöw


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

* Re: Extract sublists
  2009-11-17 13:22 Extract sublists Nordlöw
  2009-11-17 13:24 ` Nordlöw
@ 2009-11-17 14:01 ` LanX
  2009-11-17 14:09   ` LanX
  2009-11-17 15:37 ` Ted Zlatanov
  2009-11-18 22:19 ` Pascal J. Bourguignon
  3 siblings, 1 reply; 7+ messages in thread
From: LanX @ 2009-11-17 14:01 UTC (permalink / raw)
  To: help-gnu-emacs

searching for (pop) led me to (butlast)

----------------ielm
ELISP> (defun splice (LIST OFFSET LENGTH)
	 (butlast
	  (nthcdr OFFSET LIST)
	  (- (length LIST) LENGTH OFFSET)
	  )
	 )
splice
ELISP> (setq list '(a b c d e f g h i j))
(a b c d e f g h i j)

ELISP> (splice list 3 4)
(d e f g)
------------------


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

* Re: Extract sublists
  2009-11-17 14:01 ` LanX
@ 2009-11-17 14:09   ` LanX
  0 siblings, 0 replies; 7+ messages in thread
From: LanX @ 2009-11-17 14:09 UTC (permalink / raw)
  To: help-gnu-emacs

---------------- M-x ielm
ELISP> (defun sublist (LIST FROM TO)
	 (butlast
	  (nthcdr FROM LIST)
	  (- (length LIST) TO)
	  )
	 )
sublist
ELISP> (sublist list 3 7)
(d e f g)
-----------------


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

* Re: Extract sublists
  2009-11-17 13:22 Extract sublists Nordlöw
  2009-11-17 13:24 ` Nordlöw
  2009-11-17 14:01 ` LanX
@ 2009-11-17 15:37 ` Ted Zlatanov
  2009-11-18 11:16   ` Nordlöw
  2009-11-18 22:19 ` Pascal J. Bourguignon
  3 siblings, 1 reply; 7+ messages in thread
From: Ted Zlatanov @ 2009-11-17 15:37 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, 17 Nov 2009 05:22:51 -0800 (PST) Nordlöw <per.nordlow@gmail.com> wrote: 

N> Is there a function for extracting sublists of lists?
N> If not here is my suggestion for inclusion in Emacs.

N> (defun sublist (list from to)
N>   "Return a sublist of LIST, from FROM to TO.
N> Counting starts at 0. Like `substring' but for lists."
N>   (let (rtn (c from))
N>     (setq list (nthcdr from list))
N>     (while (and list (< c to))
N>       (push (pop list) rtn)
N>       (setq c (1+ c)))
N>     (nreverse rtn)))
N> ;; Use: (sublist '(a b) 0 0)
N> ;; Use: (sublist '(a b) 0 1)
N> ;; Use: (sublist '(a b) 1 2)
N> ;; Use: (sublist '(a b) 0 2)

It would be really nice if either FROM or TO could be a function or a
number, so you can say "from 2 to the first place where the element
passed to this function doesn't return t."  This is not a filter because
you look for the first place the function fails.  Unfortunately it's
efficient only if you walk through the list yourself so nthcdr won't be
so useful.

Ted


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

* Re: Extract sublists
  2009-11-17 15:37 ` Ted Zlatanov
@ 2009-11-18 11:16   ` Nordlöw
  0 siblings, 0 replies; 7+ messages in thread
From: Nordlöw @ 2009-11-18 11:16 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 17, 4:37 pm, Ted Zlatanov <t...@lifelogs.com> wrote:
> On Tue, 17 Nov 2009 05:22:51 -0800 (PST) Nordlöw <per.nord...@gmail.com> wrote:
>
> N> Is there a function for extracting sublists of lists?
> N> If not here is my suggestion for inclusion in Emacs.
>
> N> (defun sublist (list from to)
> N>   "Return a sublist of LIST, from FROM to TO.
> N> Counting starts at 0. Like `substring' but for lists."
> N>   (let (rtn (c from))
> N>     (setq list (nthcdr from list))
> N>     (while (and list (< c to))
> N>       (push (pop list) rtn)
> N>       (setq c (1+ c)))
> N>     (nreverse rtn)))
> N> ;; Use: (sublist '(a b) 0 0)
> N> ;; Use: (sublist '(a b) 0 1)
> N> ;; Use: (sublist '(a b) 1 2)
> N> ;; Use: (sublist '(a b) 0 2)
>
> It would be really nice if either FROM or TO could be a function or a
> number, so you can say "from 2 to the first place where the element
> passed to this function doesn't return t."  This is not a filter because
> you look for the first place the function fails.  Unfortunately it's
> efficient only if you walk through the list yourself so nthcdr won't be
> so useful.
>
> Ted

This gives superior performance in my benchmarks, thanks to very
little garbage collection.
For the case when to is nil it just returns (nthcdr from list) which I
hope is alright...

(defun sublist (list from &optional to)
  "Return a sublist of LIST, from FROM to TO.
If END is omitted, it defaults to the length of the sequence.
Counting starts at 0. Like `subseq' and `substring' but solely for
lists."
  (let ((start (nthcdr from list)))     ;start reference
    (if to (butlast start
                    (- (+ from (length start)) ;if extract list at the
end this makes it much faster
                       to))
      start)))

/Nordlöw


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

* Re: Extract sublists
  2009-11-17 13:22 Extract sublists Nordlöw
                   ` (2 preceding siblings ...)
  2009-11-17 15:37 ` Ted Zlatanov
@ 2009-11-18 22:19 ` Pascal J. Bourguignon
  3 siblings, 0 replies; 7+ messages in thread
From: Pascal J. Bourguignon @ 2009-11-18 22:19 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> Is there a function for extracting sublists of lists?
>
> If not here is my suggestion for inclusion in Emacs.
>
> (defun sublist (list from to)
>   "Return a sublist of LIST, from FROM to TO.
> Counting starts at 0. Like `substring' but for lists."
>   (let (rtn (c from))
>     (setq list (nthcdr from list))
>     (while (and list (< c to))
>       (push (pop list) rtn)
>       (setq c (1+ c)))
>     (nreverse rtn)))

(require 'cl)
(list
 (subseq '(a b) 0 0)
 (subseq '(a b) 0 1)
 (subseq '(a b) 1 2)
 (subseq '(a b) 0 2))
--> (nil (a) (b) (a b))


-- 
__Pascal Bourguignon__


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

end of thread, other threads:[~2009-11-18 22:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-17 13:22 Extract sublists Nordlöw
2009-11-17 13:24 ` Nordlöw
2009-11-17 14:01 ` LanX
2009-11-17 14:09   ` LanX
2009-11-17 15:37 ` Ted Zlatanov
2009-11-18 11:16   ` Nordlöw
2009-11-18 22:19 ` Pascal J. Bourguignon

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.