help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do lisp gurus truncate?


From: Pascal J. Bourguignon
Subject: Re: How do lisp gurus truncate?
Date: Thu, 23 Jul 2009 21:44:03 +0200
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

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__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]