unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: How do lisp gurus truncate?
Date: Thu, 23 Jul 2009 21:44:03 +0200	[thread overview]
Message-ID: <87r5w7jibg.fsf@galatea.local> (raw)
In-Reply-To: mailman.2999.1248330784.2239.help-gnu-emacs@gnu.org

Lennart Borgman <lennart.borgman@gmail.com> writes:

> I want to truncate an ordered list if the rest of the values are
> bigger than some limit. I just wrote some code like this one to do
> that, but there must be some more standard way of doing that, or?
>
>           (when nxml-where-first-change-pos
>             (setq nxml-where-path 'dummy nxml-where-path)
>             (let ((path nxml-where-path))
>               (while (cdr path)
>                 (when (> (nth 1 (nth 1 path)) nxml-where-first-change-pos)
>                   (setcdr path nil))
>                 (setq path (cdr path))))
>             (setq nxml-where-path (cdr nxml-where-path)))


(require 'cl)

(defun* nsplit-list-if (predicate list)
  "Modifies the list cutting it just before the first element for which the predicate 
returns true. Returns the cut list and the rest."
  (loop
     for current on (cons nil list)
     while (cdr current)
     when (funcall predicate (cadr current))
     do (return-from nsplit-list-if
          (values list
                  (prog1 (cdr current) (setf (cdr current) nil)))))
  (values list nil))


(nsplit-list-if (lambda (x) (< 3 x)) (list 0 1 2 3 4 5 6 7))
--> ((0 1 2 3) (4 5 6 7))


(defun split-list-if (predicate list)
  "Returns a copy of the list up to the first element for which the predicate
returns true, and the rest of the list."
  (loop
     with result = '()
     for current on list
     do (if (funcall predicate (car current))
            (return (values (nreverse result) current))
            (push (car current) result))
     finally (return (values (nreverse result) nil))))

(split-list-if (lambda (x) (< 3 x)) '(0 1 2 3 4 5 6 7))
--> ((0 1 2 3) (4 5 6 7))


-- 
__Pascal Bourguignon__


  parent reply	other threads:[~2009-07-23 19:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2999.1248330784.2239.help-gnu-emacs@gnu.org>
2009-07-23 17:05 ` How do lisp gurus truncate? Giorgos Keramidas
2009-07-23 17:31   ` Pascal J. Bourguignon
2009-07-23 20:35     ` Giorgos Keramidas
2009-07-23 18:11   ` Lennart Borgman
2009-07-23 19:44 ` Pascal J. Bourguignon [this message]
2009-07-23  6:32 Lennart Borgman

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87r5w7jibg.fsf@galatea.local \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).