From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Nordl=F6w?= Newsgroups: gmane.emacs.help Subject: Re: Extract sublists Date: Wed, 18 Nov 2009 03:16:35 -0800 (PST) Organization: http://groups.google.com Message-ID: <83d9ac73-7e7f-4562-837a-f81b154ea232@m38g2000yqd.googlegroups.com> References: <87skcdf9gx.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1258544588 2353 80.91.229.12 (18 Nov 2009 11:43:08 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 18 Nov 2009 11:43:08 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Nov 18 12:43:01 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NAiw8-0001yO-Lc for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Nov 2009 12:43:00 +0100 Original-Received: from localhost ([127.0.0.1]:51298 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NAiw8-00087N-8x for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Nov 2009 06:43:00 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!postnews.google.com!m38g2000yqd.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 49 Original-NNTP-Posting-Host: 150.227.15.253 Original-X-Trace: posting.google.com 1258542995 16435 127.0.0.1 (18 Nov 2009 11:16:35 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Wed, 18 Nov 2009 11:16:35 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: m38g2000yqd.googlegroups.com; posting-host=150.227.15.253; posting-account=ytJKAgoAAAA1tg4ScoRszebXiIldA5vg User-Agent: G2/1.0 X-HTTP-Via: 1.1 ip1-w.foi.se:8080 (IronPort-WSA/6.3.0-604) X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.251.0 Safari/532.5,gzip(gfe),gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:174798 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:69871 Archived-At: On Nov 17, 4:37=A0pm, Ted Zlatanov wrote: > On Tue, 17 Nov 2009 05:22:51 -0800 (PST) Nordl=F6w 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> =A0 "Return a sublist of LIST, from FROM to TO. > N> Counting starts at 0. Like `substring' but for lists." > N> =A0 (let (rtn (c from)) > N> =A0 =A0 (setq list (nthcdr from list)) > N> =A0 =A0 (while (and list (< c to)) > N> =A0 =A0 =A0 (push (pop list) rtn) > N> =A0 =A0 =A0 (setq c (1+ c))) > N> =A0 =A0 (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." =A0This is not a filter becaus= e > you look for the first place the function fails. =A0Unfortunately 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=F6w