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: Pascal J. Bourguignon
Subject: Re: Partition for Emacs Lisp
Date: Sun, 28 Jun 2009 19:52:30 +0200
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

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))))

(partition '[a b c d e f] 2)  -> ([a b] [c d] [e f])
(partition '(a b c d e f) 2)  -> ((a b) (c d) (e f))
(partition '"abcdef" 2)       -> ("ab" "cd" "ef")


> Is there something ready out from the box?

Perhaps.  But if you didn't search in the manual, it must be because
it would take more time than to write partition yourself, so I didn't
search in the manual either, and wrote it to steal you the fun of
writing it yourself.


-- 
__Pascal Bourguignon__


reply via email to

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