[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 20:34:25 +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))))
>
> (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")
Thanks a lot!!
>> Is there something ready out from the box?
>
> Perhaps. But if you didn't search in the manual,
I did search in the manual and in internet but without success.
> it must be because it would take more time than to write partition
> yourself
Sure, but my lisp skills are not so brilliant, so after ca. one hour
searching I've capitulate and posted my question. I had already
suspected the loop as a possible solution but the loop syntax was so
harrowing that I've given up.
> so I didn't search in the manual either, and wrote it to steal you the
> fun of writing it yourself.
Oh my friend - it is no problem! :-) Thanks again!
regards
Marc