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

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

Re: Partition for Emacs Lisp


From: DanL
Subject: Re: Partition for Emacs Lisp
Date: Wed, 1 Jul 2009 15:32:00 -0700 (PDT)
User-agent: G2/1.0

> After learning a bit of Haskell I was surprised that CL doesn't have
> functions like dropWhile, takeWhile etc. So naturally, I implemented
> them asap (curiously, the existence of cons proved no hindrance) - and
> then didn't really use them much.

Using series (not CL, but in CLTL2), until-if provides takeWhile's
functionality:

DHL> (until-if #'plusp #z(-1 -2 3 4 -5 6))
#Z(-1 -2)

I couldn't find a counterpart for dropWhile, but it could be
implemented (naively) like this:

(defun take-while (predicate series)
       (declare (optimizable-series-function))
       (subseries series 0
                  (collect-length (until-if (complement predicate)
                                           series))))

Or maybe:

(defun take-while (predicate series)
  (declare (optimizable-series-function)
           (off-line-port series))
  (producing (out) ((in series)
                    element)
     (loop
         (tagbody
            (setq element (next-in in (terminate-producing)))
            (unless (funcall predicate element)
              (terminate-producing))
            (next-out out element)))))

DHL> (take-while #'minusp #z(-1 -2 3 4 -5 6))
#Z(-1 -2)

Regards,

dhl


Disclaimer: I am by no means a series expert, so any tips, especially
concerning efficiency, are appreciated.



reply via email to

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