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: Marc Tfardy
Subject: Re: Partition for Emacs Lisp
Date: Sun, 28 Jun 2009 22:16:56 +0200
User-agent: Thunderbird 2.0.0.22 (Windows/20090605)

Pascal J. Bourguignon schrieb:
Marc Tfardy <bum@cyk.cyk> writes:

Hi!

I looking for a ELISP function that do the job like Partition in
Mathematica. The simplest case:

(partition '(a b c d e f) 2)
should return:
((a b) (c d) (e f))

(defun partition-list (list length)
  (loop
     while list
     collect (subseq list 0 length)
     do (setf list (nthcdr length list))))

(defun partition-vector (vector length)
  (loop
     for i = 0 then (+ i length)
     while (< i (length vector))
     collect (subseq vector i (+ i length))))
(defun partition (sequence length)
   (etypecase sequence
      (list   (partition-list sequence length))
(string (partition-vector sequence length)) ; emacs lisp strings are not vectors! (vector (partition-vector sequence length))))

Is there some reason (performance?) to write partition as above
and not as:

(defun partition (sequence length)
  (cond ((listp sequence)
         (partition-list sequence length))
        ((stringp sequence)
         (partition-vector sequence length)) ; emacs lisp strings are not 
vectors!
        ((vectorp sequence)
          (partition-vector sequence length))))


regards
Marc


reply via email to

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