da26f3847ef083134ca9b77decb6d3abd7a2c9dd
1 ;;; help-gnu-emacs.el --- examples from help-gnu-emacs@gnu.org
3 ;; Copyright (C) 2009 contributors to help-gnu-emacs@gnu.org
5 ;; Author: authors of the contributions
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; This file contains stuff found at
24 ;; help-gnu-emacs@gnu.org, which was seems of interest
25 ;; to preserve that way
27 ;; Please note: code here does not reflect fully
28 ;; discussions but delivers an extract from it. As
29 ;; functions may be defined differently, its not
30 ;; suitable to compile it.
36 ;; Partition for Emacs Lisp
37 ;; Marc Tfardy, bum@cyk.cyk, 28.06.2009 15:26
41 ;; I looking for a ELISP function that do the job like Partition in
42 ;; Mathematica. The simplest case:
44 ;; (partition '(a b c d e f) 2)
47 ;; ((a b) (c d) (e f))
49 ;; Is there something ready out from the box?
51 ;; Re: Partition for Emacs Lisp
52 ;; Pascal J. Bourguignon, pjb@informatimago.com, 28.06.2009 19:52
54 (defun partition-list (list length
)
57 collect
(subseq list
0 length
)
58 do
(setf list
(nthcdr length list
))))
60 (defun partition-vector (vector length
)
62 for i
= 0 then
(+ i length
)
63 while
(< i
(length vector
))
64 collect
(subseq vector i
(+ i length
))))
66 (defun partition (sequence length
)
68 (list (partition-list sequence length
))
69 (string (partition-vector sequence length
)) ; emacs lisp strings are not vectors!
70 (vector (partition-vector sequence length
))))
72 ;; (partition '[a b c d e f] 2) -> ([a b] [c d] [e f])
73 ;; (partition '(a b c d e f) 2) -> ((a b) (c d) (e f))
74 ;; (partition '"abcdef" 2) -> ("ab" "cd" "ef")
76 ;; Re: Partition for Emacs Lisp
77 ;; Vassil Nikolov, vnikolov@pobox.com, 02:46
79 (when (and (fboundp 'featurep
) (featurep 'emacs
)) ;seen in comp.lang.lisp
82 (defun partition (l n
) ;"demo" grade, cursorily tested
83 "Return a list of L's consecutive sublists of length N."
84 (assert (zerop (mod (length l
) n
)))
85 (loop for l on l by
#'(lambda (l) (nthcdr n l
)) collect
(subseq l
0 n
)))
87 ;; (partition '(a b c d e f) 2)
88 ;; => ((a b) (c d) (e f))
90 (provide 'help-gnu-emacs
)
91 ;;; help-gnu-emacs.el ends here