Fix typos and minor wording issues in ELisp manual
[emacs.git] / doc / lispref / sequences.texi
blobf347cd9e980de3bf5d6b9448697e72a656b8db9b
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2018 Free Software
4 @c Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @node Sequences Arrays Vectors
7 @chapter Sequences, Arrays, and Vectors
8 @cindex sequence
10   The @dfn{sequence} type is the union of two other Lisp types: lists
11 and arrays.  In other words, any list is a sequence, and any array is
12 a sequence.  The common property that all sequences have is that each
13 is an ordered collection of elements.
15   An @dfn{array} is a fixed-length object with a slot for each of its
16 elements.  All the elements are accessible in constant time.  The four
17 types of arrays are strings, vectors, char-tables and bool-vectors.
19   A list is a sequence of elements, but it is not a single primitive
20 object; it is made of cons cells, one cell per element.  Finding the
21 @var{n}th element requires looking through @var{n} cons cells, so
22 elements farther from the beginning of the list take longer to access.
23 But it is possible to add elements to the list, or remove elements.
25   The following diagram shows the relationship between these types:
27 @example
28 @group
29           _____________________________________________
30          |                                             |
31          |          Sequence                           |
32          |  ______   ________________________________  |
33          | |      | |                                | |
34          | | List | |             Array              | |
35          | |      | |    ________       ________     | |
36          | |______| |   |        |     |        |    | |
37          |          |   | Vector |     | String |    | |
38          |          |   |________|     |________|    | |
39          |          |  ____________   _____________  | |
40          |          | |            | |             | | |
41          |          | | Char-table | | Bool-vector | | |
42          |          | |____________| |_____________| | |
43          |          |________________________________| |
44          |_____________________________________________|
45 @end group
46 @end example
48 @menu
49 * Sequence Functions::    Functions that accept any kind of sequence.
50 * Arrays::                Characteristics of arrays in Emacs Lisp.
51 * Array Functions::       Functions specifically for arrays.
52 * Vectors::               Special characteristics of Emacs Lisp vectors.
53 * Vector Functions::      Functions specifically for vectors.
54 * Char-Tables::           How to work with char-tables.
55 * Bool-Vectors::          How to work with bool-vectors.
56 * Rings::                 Managing a fixed-size ring of objects.
57 @end menu
59 @node Sequence Functions
60 @section Sequences
62   This section describes functions that accept any kind of sequence.
64 @defun sequencep object
65 This function returns @code{t} if @var{object} is a list, vector,
66 string, bool-vector, or char-table, @code{nil} otherwise.
67 @end defun
69 @defun length sequence
70 @cindex string length
71 @cindex list length
72 @cindex vector length
73 @cindex sequence length
74 @cindex char-table length
75 @anchor{Definition of length}
76 This function returns the number of elements in @var{sequence}.  If
77 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
78 signaled.  Circular lists may cause an infinite loop.  For a
79 char-table, the value returned is always one more than the maximum
80 Emacs character code.
82 @xref{Definition of safe-length}, for the related function @code{safe-length}.
84 @example
85 @group
86 (length '(1 2 3))
87     @result{} 3
88 @end group
89 @group
90 (length ())
91     @result{} 0
92 @end group
93 @group
94 (length "foobar")
95     @result{} 6
96 @end group
97 @group
98 (length [1 2 3])
99     @result{} 3
100 @end group
101 @group
102 (length (make-bool-vector 5 nil))
103     @result{} 5
104 @end group
105 @end example
106 @end defun
108 @noindent
109 See also @code{string-bytes}, in @ref{Text Representations}.
111 If you need to compute the width of a string on display, you should use
112 @code{string-width} (@pxref{Size of Displayed Text}), not @code{length},
113 since @code{length} only counts the number of characters, but does not
114 account for the display width of each character.
116 @defun elt sequence index
117 @anchor{Definition of elt}
118 @cindex elements of sequences
119 This function returns the element of @var{sequence} indexed by
120 @var{index}.  Legitimate values of @var{index} are integers ranging
121 from 0 up to one less than the length of @var{sequence}.  If
122 @var{sequence} is a list, out-of-range values behave as for
123 @code{nth}.  @xref{Definition of nth}.  Otherwise, out-of-range values
124 trigger an @code{args-out-of-range} error.
126 @example
127 @group
128 (elt [1 2 3 4] 2)
129      @result{} 3
130 @end group
131 @group
132 (elt '(1 2 3 4) 2)
133      @result{} 3
134 @end group
135 @group
136 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
137 (string (elt "1234" 2))
138      @result{} "3"
139 @end group
140 @group
141 (elt [1 2 3 4] 4)
142      @error{} Args out of range: [1 2 3 4], 4
143 @end group
144 @group
145 (elt [1 2 3 4] -1)
146      @error{} Args out of range: [1 2 3 4], -1
147 @end group
148 @end example
150 This function generalizes @code{aref} (@pxref{Array Functions}) and
151 @code{nth} (@pxref{Definition of nth}).
152 @end defun
154 @defun copy-sequence seqr
155 @cindex copying sequences
156 This function returns a copy of @var{seqr}, which should be either a
157 sequence or a record.  The copy is the same type of object as the
158 original, and it has the same elements in the same order.  However, if
159 @var{seqr} is empty, like a string or a vector of zero length, the
160 value returned by this function might not be a copy, but an empty
161 object of the same type and identical to @var{seqr}.
163 Storing a new element into the copy does not affect the original
164 @var{seqr}, and vice versa.  However, the elements of the copy
165 are not copies; they are identical (@code{eq}) to the elements
166 of the original.  Therefore, changes made within these elements, as
167 found via the copy, are also visible in the original.
169 If the argument is a string with text properties, the property list in
170 the copy is itself a copy, not shared with the original's property
171 list.  However, the actual values of the properties are shared.
172 @xref{Text Properties}.
174 This function does not work for dotted lists.  Trying to copy a
175 circular list may cause an infinite loop.
177 See also @code{append} in @ref{Building Lists}, @code{concat} in
178 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
179 for other ways to copy sequences.
181 @example
182 @group
183 (setq bar '(1 2))
184      @result{} (1 2)
185 @end group
186 @group
187 (setq x (vector 'foo bar))
188      @result{} [foo (1 2)]
189 @end group
190 @group
191 (setq y (copy-sequence x))
192      @result{} [foo (1 2)]
193 @end group
195 @group
196 (eq x y)
197      @result{} nil
198 @end group
199 @group
200 (equal x y)
201      @result{} t
202 @end group
203 @group
204 (eq (elt x 1) (elt y 1))
205      @result{} t
206 @end group
208 @group
209 ;; @r{Replacing an element of one sequence.}
210 (aset x 0 'quux)
211 x @result{} [quux (1 2)]
212 y @result{} [foo (1 2)]
213 @end group
215 @group
216 ;; @r{Modifying the inside of a shared element.}
217 (setcar (aref x 1) 69)
218 x @result{} [quux (69 2)]
219 y @result{} [foo (69 2)]
220 @end group
221 @end example
222 @end defun
224 @defun reverse sequence
225 @cindex string reverse
226 @cindex list reverse
227 @cindex vector reverse
228 @cindex sequence reverse
229 This function creates a new sequence whose elements are the elements
230 of @var{sequence}, but in reverse order.  The original argument @var{sequence}
231 is @emph{not} altered.  Note that char-tables cannot be reversed.
233 @example
234 @group
235 (setq x '(1 2 3 4))
236      @result{} (1 2 3 4)
237 @end group
238 @group
239 (reverse x)
240      @result{} (4 3 2 1)
242      @result{} (1 2 3 4)
243 @end group
244 @group
245 (setq x [1 2 3 4])
246      @result{} [1 2 3 4]
247 @end group
248 @group
249 (reverse x)
250      @result{} [4 3 2 1]
252      @result{} [1 2 3 4]
253 @end group
254 @group
255 (setq x "xyzzy")
256      @result{} "xyzzy"
257 @end group
258 @group
259 (reverse x)
260      @result{} "yzzyx"
262      @result{} "xyzzy"
263 @end group
264 @end example
265 @end defun
267 @defun nreverse sequence
268 @cindex reversing a string
269 @cindex reversing a list
270 @cindex reversing a vector
271   This function reverses the order of the elements of @var{sequence}.
272 Unlike @code{reverse} the original @var{sequence} may be modified.
274   For example:
276 @example
277 @group
278 (setq x '(a b c))
279      @result{} (a b c)
280 @end group
281 @group
283      @result{} (a b c)
284 (nreverse x)
285      @result{} (c b a)
286 @end group
287 @group
288 ;; @r{The cons cell that was first is now last.}
290      @result{} (a)
291 @end group
292 @end example
294   To avoid confusion, we usually store the result of @code{nreverse}
295 back in the same variable which held the original list:
297 @example
298 (setq x (nreverse x))
299 @end example
301   Here is the @code{nreverse} of our favorite example, @code{(a b c)},
302 presented graphically:
304 @smallexample
305 @group
306 @r{Original list head:}                       @r{Reversed list:}
307  -------------        -------------        ------------
308 | car  | cdr  |      | car  | cdr  |      | car | cdr  |
309 |   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
310 |      |      |   |  |      |   |  |   |  |     |   |  |
311  -------------    |   --------- | -    |   -------- | -
312                   |             |      |            |
313                    -------------        ------------
314 @end group
315 @end smallexample
317   For the vector, it is even simpler because you don't need setq:
319 @example
320 (setq x [1 2 3 4])
321      @result{} [1 2 3 4]
322 (nreverse x)
323      @result{} [4 3 2 1]
325      @result{} [4 3 2 1]
326 @end example
328 Note that unlike @code{reverse}, this function doesn't work with strings.
329 Although you can alter string data by using @code{aset}, it is strongly
330 encouraged to treat strings as immutable.
332 @end defun
334 @defun sort sequence predicate
335 @cindex stable sort
336 @cindex sorting lists
337 @cindex sorting vectors
338 This function sorts @var{sequence} stably.  Note that this function doesn't work
339 for all sequences; it may be used only for lists and vectors.  If @var{sequence}
340 is a list, it is modified destructively.  This functions returns the sorted
341 @var{sequence} and compares elements using @var{predicate}.  A stable sort is
342 one in which elements with equal sort keys maintain their relative order before
343 and after the sort.  Stability is important when successive sorts are used to
344 order elements according to different criteria.
346 The argument @var{predicate} must be a function that accepts two
347 arguments.  It is called with two elements of @var{sequence}.  To get an
348 increasing order sort, the @var{predicate} should return non-@code{nil} if the
349 first element is ``less'' than the second, or @code{nil} if not.
351 The comparison function @var{predicate} must give reliable results for
352 any given pair of arguments, at least within a single call to
353 @code{sort}.  It must be @dfn{antisymmetric}; that is, if @var{a} is
354 less than @var{b}, @var{b} must not be less than @var{a}.  It must be
355 @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
356 is less than @var{c}, then @var{a} must be less than @var{c}.  If you
357 use a comparison function which does not meet these requirements, the
358 result of @code{sort} is unpredictable.
360 The destructive aspect of @code{sort} for lists is that it rearranges the
361 cons cells forming @var{sequence} by changing @sc{cdr}s.  A nondestructive
362 sort function would create new cons cells to store the elements in their
363 sorted order.  If you wish to make a sorted copy without destroying the
364 original, copy it first with @code{copy-sequence} and then sort.
366 Sorting does not change the @sc{car}s of the cons cells in @var{sequence};
367 the cons cell that originally contained the element @code{a} in
368 @var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
369 appears in a different position in the list due to the change of
370 @sc{cdr}s.  For example:
372 @example
373 @group
374 (setq nums '(1 3 2 6 5 4 0))
375      @result{} (1 3 2 6 5 4 0)
376 @end group
377 @group
378 (sort nums '<)
379      @result{} (0 1 2 3 4 5 6)
380 @end group
381 @group
382 nums
383      @result{} (1 2 3 4 5 6)
384 @end group
385 @end example
387 @noindent
388 @strong{Warning}: Note that the list in @code{nums} no longer contains
389 0; this is the same cons cell that it was before, but it is no longer
390 the first one in the list.  Don't assume a variable that formerly held
391 the argument now holds the entire sorted list!  Instead, save the result
392 of @code{sort} and use that.  Most often we store the result back into
393 the variable that held the original list:
395 @example
396 (setq nums (sort nums '<))
397 @end example
399 For the better understanding of what stable sort is, consider the following
400 vector example.  After sorting, all items whose @code{car} is 8 are grouped
401 at the beginning of @code{vector}, but their relative order is preserved.
402 All items whose @code{car} is 9 are grouped at the end of @code{vector},
403 but their relative order is also preserved:
405 @example
406 @group
407 (setq
408   vector
409   (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
410           '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")))
411      @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz")
412          (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")]
413 @end group
414 @group
415 (sort vector (lambda (x y) (< (car x) (car y))))
416      @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
417          (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
418 @end group
419 @end example
421 @xref{Sorting}, for more functions that perform sorting.
422 See @code{documentation} in @ref{Accessing Documentation}, for a
423 useful example of @code{sort}.
424 @end defun
426 @cindex sequence functions in seq
427 @cindex seq library
428 @cindex sequences, generalized
429   The @file{seq.el} library provides the following additional sequence
430 manipulation macros and functions, prefixed with @code{seq-}.  To use
431 them, you must first load the @file{seq} library.
433   All functions defined in this library are free of side-effects;
434 i.e., they do not modify any sequence (list, vector, or string) that
435 you pass as an argument.  Unless otherwise stated, the result is a
436 sequence of the same type as the input.  For those functions that take
437 a predicate, this should be a function of one argument.
439   The @file{seq.el} library can be extended to work with additional
440 types of sequential data-structures.  For that purpose, all functions
441 are defined using @code{cl-defgeneric}.  @xref{Generic Functions}, for
442 more details about using @code{cl-defgeneric} for adding extensions.
444 @defun seq-elt sequence index
445   This function returns the element of @var{sequence} at the specified
446 @var{index}, which is an integer whose valid value range is zero to
447 one less than the length of @var{sequence}.  For out-of-range values
448 on built-in sequence types, @code{seq-elt} behaves like @code{elt}.
449 For the details, see @ref{Definition of elt}.
451 @example
452 @group
453 (seq-elt [1 2 3 4] 2)
454 @result{} 3
455 @end group
456 @end example
458   @code{seq-elt} returns places settable using @code{setf}
459 (@pxref{Setting Generalized Variables}).
461 @example
462 @group
463 (setq vec [1 2 3 4])
464 (setf (seq-elt vec 2) 5)
466 @result{} [1 2 5 4]
467 @end group
468 @end example
469 @end defun
471 @defun seq-length sequence
472   This function returns the number of elements in @var{sequence}.  For
473 built-in sequence types, @code{seq-length} behaves like @code{length}.
474 @xref{Definition of length}.
475 @end defun
477 @defun seqp sequence
478   This function returns non-@code{nil} if @var{sequence} is a sequence
479 (a list or array), or any additional type of sequence defined via
480 @file{seq.el} generic functions.
482 @example
483 @group
484 (seqp [1 2])
485 @result{} t
486 @end group
487 @group
488 (seqp 2)
489 @result{} nil
490 @end group
491 @end example
492 @end defun
494 @defun seq-drop sequence n
495   This function returns all but the first @var{n} (an integer)
496 elements of @var{sequence}.  If @var{n} is negative or zero,
497 the result is @var{sequence}.
499 @example
500 @group
501 (seq-drop [1 2 3 4 5 6] 3)
502 @result{} [4 5 6]
503 @end group
504 @group
505 (seq-drop "hello world" -4)
506 @result{} "hello world"
507 @end group
508 @end example
509 @end defun
511 @defun seq-take sequence n
512   This function returns the first @var{n} (an integer) elements of
513 @var{sequence}.  If @var{n} is negative or zero, the result
514 is @code{nil}.
516 @example
517 @group
518 (seq-take '(1 2 3 4) 3)
519 @result{} (1 2 3)
520 @end group
521 @group
522 (seq-take [1 2 3 4] 0)
523 @result{} []
524 @end group
525 @end example
526 @end defun
528 @defun seq-take-while predicate sequence
529   This function returns the members of @var{sequence} in order,
530 stopping before the first one for which @var{predicate} returns @code{nil}.
532 @example
533 @group
534 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
535 @result{} (1 2 3)
536 @end group
537 @group
538 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
539 @result{} []
540 @end group
541 @end example
542 @end defun
544 @defun seq-drop-while predicate sequence
545   This function returns the members of @var{sequence} in order,
546 starting from the first one for which @var{predicate} returns @code{nil}.
548 @example
549 @group
550 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
551 @result{} (-1 -2)
552 @end group
553 @group
554 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
555 @result{} [1 4 6]
556 @end group
557 @end example
558 @end defun
560 @defun seq-do function sequence
561   This function applies @var{function} to each element of
562 @var{sequence} in turn (presumably for side effects), and returns
563 @var{sequence}.
564 @end defun
566 @defun seq-map function sequence
567   This function returns the result of applying @var{function} to each
568 element of @var{sequence}.  The returned value is a list.
570 @example
571 @group
572 (seq-map #'1+ '(2 4 6))
573 @result{} (3 5 7)
574 @end group
575 @group
576 (seq-map #'symbol-name [foo bar])
577 @result{} ("foo" "bar")
578 @end group
579 @end example
580 @end defun
582 @defun seq-map-indexed function sequence
583   This function returns the result of applying @var{function} to each
584 element of @var{sequence} and its index within @var{seq}.  The
585 returned value is a list.
587 @example
588 @group
589 (seq-map-indexed (lambda (elt idx)
590                    (list idx elt))
591                  '(a b c))
592 @result{} ((0 a) (b 1) (c 2))
593 @end group
594 @end example
595 @end defun
597 @defun seq-mapn function &rest sequences
598   This function returns the result of applying @var{function} to each
599 element of @var{sequences}.  The arity (@pxref{What Is a Function,
600 sub-arity}) of @var{function} must match the number of sequences.
601 Mapping stops at the end of the shortest sequence, and the returned
602 value is a list.
604 @example
605 @group
606 (seq-mapn #'+ '(2 4 6) '(20 40 60))
607 @result{} (22 44 66)
608 @end group
609 @group
610 (seq-mapn #'concat '("moskito" "bite") ["bee" "sting"])
611 @result{} ("moskitobee" "bitesting")
612 @end group
613 @end example
614 @end defun
616 @defun seq-filter predicate sequence
617 @cindex filtering sequences
618   This function returns a list of all the elements in @var{sequence}
619 for which @var{predicate} returns non-@code{nil}.
621 @example
622 @group
623 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
624 @result{} (1 3 5)
625 @end group
626 @group
627 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
628 @result{} nil
629 @end group
630 @end example
631 @end defun
633 @defun seq-remove predicate sequence
634 @cindex removing from sequences
635   This function returns a list of all the elements in @var{sequence}
636 for which @var{predicate} returns @code{nil}.
638 @example
639 @group
640 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
641 @result{} (-1 -3)
642 @end group
643 @group
644 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
645 @result{} nil
646 @end group
647 @end example
648 @end defun
650 @defun seq-reduce function sequence initial-value
651 @cindex reducing sequences
652   This function returns the result of calling @var{function} with
653 @var{initial-value} and the first element of @var{sequence}, then calling
654 @var{function} with that result and the second element of @var{sequence},
655 then with that result and the third element of @var{sequence}, etc.
656 @var{function} should be a function of two arguments.  If
657 @var{sequence} is empty, this returns @var{initial-value} without
658 calling @var{function}.
660 @example
661 @group
662 (seq-reduce #'+ [1 2 3 4] 0)
663 @result{} 10
664 @end group
665 @group
666 (seq-reduce #'+ '(1 2 3 4) 5)
667 @result{} 15
668 @end group
669 @group
670 (seq-reduce #'+ '() 3)
671 @result{} 3
672 @end group
673 @end example
674 @end defun
676 @defun seq-some predicate sequence
677   This function returns the first non-@code{nil} value returned by
678 applying @var{predicate} to each element of @var{sequence} in turn.
680 @example
681 @group
682 (seq-some #'numberp ["abc" 1 nil])
683 @result{} t
684 @end group
685 @group
686 (seq-some #'numberp ["abc" "def"])
687 @result{} nil
688 @end group
689 @group
690 (seq-some #'null ["abc" 1 nil])
691 @result{} t
692 @end group
693 @group
694 (seq-some #'1+ [2 4 6])
695 @result{} 3
696 @end group
697 @end example
698 @end defun
700 @defun seq-find predicate sequence &optional default
701   This function returns the first element in @var{sequence} for which
702 @var{predicate} returns non-@code{nil}.  If no element matches
703 @var{predicate}, the function returns @var{default}.
705 Note that this function has an ambiguity if the found element is
706 identical to @var{default}, as in that case it cannot be known whether
707 an element was found or not.
709 @example
710 @group
711 (seq-find #'numberp ["abc" 1 nil])
712 @result{} 1
713 @end group
714 @group
715 (seq-find #'numberp ["abc" "def"])
716 @result{} nil
717 @end group
718 @end example
719 @end defun
721 @defun seq-every-p predicate sequence
722   This function returns non-@code{nil} if applying @var{predicate}
723 to every element of @var{sequence} returns non-@code{nil}.
725 @example
726 @group
727 (seq-every-p #'numberp [2 4 6])
728 @result{} t
729 @end group
730 @group
731 (seq-every-p #'numberp [2 4 "6"])
732 @result{} nil
733 @end group
734 @end example
735 @end defun
737 @defun seq-empty-p sequence
738   This function returns non-@code{nil} if @var{sequence} is empty.
740 @example
741 @group
742 (seq-empty-p "not empty")
743 @result{} nil
744 @end group
745 @group
746 (seq-empty-p "")
747 @result{} t
748 @end group
749 @end example
750 @end defun
752 @defun seq-count predicate sequence
753   This function returns the number of elements in @var{sequence} for which
754 @var{predicate} returns non-@code{nil}.
756 @example
757 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
758 @result{} 2
759 @end example
760 @end defun
762 @cindex sorting sequences
763 @defun seq-sort function sequence
764   This function returns a copy of @var{sequence} that is sorted
765 according to @var{function}, a function of two arguments that returns
766 non-@code{nil} if the first argument should sort before the second.
767 @end defun
769 @defun seq-sort-by function predicate sequence
770   This function is similar to @code{seq-sort}, but the elements of
771 @var{sequence} are transformed by applying @var{function} on them
772 before being sorted.  @var{function} is a function of one argument.
774 @example
775 (seq-sort-by #'seq-length #'> ["a" "ab" "abc"])
776 @result{} ["abc" "ab" "a"]
777 @end example
778 @end defun
781 @defun seq-contains sequence elt &optional function
782   This function returns the first element in @var{sequence} that is equal to
783 @var{elt}.  If the optional argument @var{function} is non-@code{nil},
784 it is a function of two arguments to use instead of the default @code{equal}.
786 @example
787 @group
788 (seq-contains '(symbol1 symbol2) 'symbol1)
789 @result{} symbol1
790 @end group
791 @group
792 (seq-contains '(symbol1 symbol2) 'symbol3)
793 @result{} nil
794 @end group
795 @end example
797 @end defun
799 @defun seq-set-equal-p sequence1 sequence2 &optional testfn
800 This function checks whether @var{sequence1} and @var{sequence2}
801 contain the same elements, regardless of the order. If the optional
802 argument @var{testfn} is non-@code{nil}, it is a function of two
803 arguments to use instead of the default @code{equal}.
805 @example
806 @group
807 (seq-set-equal-p '(a b c) '(c b a))
808 @result{} t
809 @end group
810 @group
811 (seq-set-equal-p '(a b c) '(c b))
812 @result{} nil
813 @end group
814 @group
815 (seq-set-equal-p '("a" "b" "c") '("c" "b" "a"))
816 @result{} t
817 @end group
818 @group
819 (seq-set-equal-p '("a" "b" "c") '("c" "b" "a") #'eq)
820 @result{} nil
821 @end group
822 @end example
824 @end defun
826 @defun seq-position sequence elt &optional function
827   This function returns the index of the first element in
828 @var{sequence} that is equal to @var{elt}.  If the optional argument
829 @var{function} is non-@code{nil}, it is a function of two arguments to
830 use instead of the default @code{equal}.
832 @example
833 @group
834 (seq-position '(a b c) 'b)
835 @result{} 1
836 @end group
837 @group
838 (seq-position '(a b c) 'd)
839 @result{} nil
840 @end group
841 @end example
842 @end defun
845 @defun seq-uniq sequence &optional function
846   This function returns a list of the elements of @var{sequence} with
847 duplicates removed.  If the optional argument @var{function} is non-@code{nil},
848 it is a function of two arguments to use instead of the default @code{equal}.
850 @example
851 @group
852 (seq-uniq '(1 2 2 1 3))
853 @result{} (1 2 3)
854 @end group
855 @group
856 (seq-uniq '(1 2 2.0 1.0) #'=)
857 @result{} (1 2)
858 @end group
859 @end example
860 @end defun
862 @defun seq-subseq sequence start &optional end
863 @cindex sub-sequence
864   This function returns a subset of @var{sequence} from @var{start}
865 to @var{end}, both integers (@var{end} defaults to the last element).
866 If @var{start} or @var{end} is negative, it counts from the end of
867 @var{sequence}.
869 @example
870 @group
871 (seq-subseq '(1 2 3 4 5) 1)
872 @result{} (2 3 4 5)
873 @end group
874 @group
875 (seq-subseq '[1 2 3 4 5] 1 3)
876 @result{} [2 3]
877 @end group
878 @group
879 (seq-subseq '[1 2 3 4 5] -3 -1)
880 @result{} [3 4]
881 @end group
882 @end example
883 @end defun
885 @defun seq-concatenate type &rest sequences
886   This function returns a sequence of type @var{type} made of the
887 concatenation of @var{sequences}.  @var{type} may be: @code{vector},
888 @code{list} or @code{string}.
890 @example
891 @group
892 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
893 @result{} (1 2 3 4 5 6)
894 @end group
895 @group
896 (seq-concatenate 'string "Hello " "world")
897 @result{} "Hello world"
898 @end group
899 @end example
900 @end defun
902 @defun seq-mapcat function sequence &optional type
903   This function returns the result of applying @code{seq-concatenate}
904 to the result of applying @var{function} to each element of
905 @var{sequence}.  The result is a sequence of type @var{type}, or a
906 list if @var{type} is @code{nil}.
908 @example
909 @group
910 (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
911 @result{} (1 2 3 4 5 6)
912 @end group
913 @end example
914 @end defun
916 @defun seq-partition sequence n
917   This function returns a list of the elements of @var{sequence}
918 grouped into sub-sequences of length @var{n}.  The last sequence may
919 contain less elements than @var{n}.  @var{n} must be an integer.  If
920 @var{n} is a negative integer or 0, the return value is @code{nil}.
922 @example
923 @group
924 (seq-partition '(0 1 2 3 4 5 6 7) 3)
925 @result{} ((0 1 2) (3 4 5) (6 7))
926 @end group
927 @end example
928 @end defun
930 @defun seq-intersection sequence1 sequence2 &optional function
931 @cindex sequences, intersection of
932 @cindex intersection of sequences
933   This function returns a list of the elements that appear both in
934 @var{sequence1} and @var{sequence2}.  If the optional argument
935 @var{function} is non-@code{nil}, it is a function of two arguments to
936 use to compare elements instead of the default @code{equal}.
938 @example
939 @group
940 (seq-intersection [2 3 4 5] [1 3 5 6 7])
941 @result{} (3 5)
942 @end group
943 @end example
944 @end defun
947 @defun seq-difference sequence1 sequence2 &optional function
948   This function returns a list of the elements that appear in
949 @var{sequence1} but not in @var{sequence2}.  If the optional argument
950 @var{function} is non-@code{nil}, it is a function of two arguments to
951 use to compare elements instead of the default @code{equal}.
953 @example
954 @group
955 (seq-difference '(2 3 4 5) [1 3 5 6 7])
956 @result{} (2 4)
957 @end group
958 @end example
959 @end defun
961 @defun seq-group-by function sequence
962   This function separates the elements of @var{sequence} into an alist
963 whose keys are the result of applying @var{function} to each element
964 of @var{sequence}.  Keys are compared using @code{equal}.
966 @example
967 @group
968 (seq-group-by #'integerp '(1 2.1 3 2 3.2))
969 @result{} ((t 1 3 2) (nil 2.1 3.2))
970 @end group
971 @group
972 (seq-group-by #'car '((a 1) (b 2) (a 3) (c 4)))
973 @result{} ((b (b 2)) (a (a 1) (a 3)) (c (c 4)))
974 @end group
975 @end example
976 @end defun
978 @defun seq-into sequence type
979 @cindex convert sequence to another type
980 @cindex list to vector
981 @cindex vector to list
982 @cindex string to vector
983   This function converts the sequence @var{sequence} into a sequence
984 of type @var{type}.  @var{type} can be one of the following symbols:
985 @code{vector}, @code{string} or @code{list}.
987 @example
988 @group
989 (seq-into [1 2 3] 'list)
990 @result{} (1 2 3)
991 @end group
992 @group
993 (seq-into nil 'vector)
994 @result{} []
995 @end group
996 @group
997 (seq-into "hello" 'vector)
998 @result{} [104 101 108 108 111]
999 @end group
1000 @end example
1001 @end defun
1003 @defun seq-min sequence
1004 @cindex minimum value of sequence
1005 @cindex sequence minimum
1006   This function returns the smallest element of @var{sequence}.  The
1007 elements of @var{sequence} must be numbers or markers
1008 (@pxref{Markers}).
1010 @example
1011 @group
1012 (seq-min [3 1 2])
1013 @result{} 1
1014 @end group
1015 @group
1016 (seq-min "Hello")
1017 @result{} 72
1018 @end group
1019 @end example
1020 @end defun
1022 @defun seq-max sequence
1023 @cindex maximum value of sequence
1024 @cindex sequence maximum
1025   This function returns the largest element of @var{sequence}.  The
1026 elements of @var{sequence} must be numbers or markers.
1028 @example
1029 @group
1030 (seq-max [1 3 2])
1031 @result{} 3
1032 @end group
1033 @group
1034 (seq-max "Hello")
1035 @result{} 111
1036 @end group
1037 @end example
1038 @end defun
1040 @defmac seq-doseq (var sequence) body@dots{}
1041 @cindex sequence iteration
1042 @cindex iteration over vector or string
1043   This macro is like @code{dolist} (@pxref{Iteration, dolist}), except
1044 that @var{sequence} can be a list, vector or string.  This is
1045 primarily useful for side-effects.
1046 @end defmac
1048 @defmac seq-let arguments sequence body@dots{}
1049 @cindex sequence destructuring
1050   This macro binds the variables defined in @var{arguments} to the
1051 elements of @var{sequence}.  @var{arguments} can themselves include
1052 sequences, allowing for nested destructuring.
1054 The @var{arguments} sequence can also include the @code{&rest} marker
1055 followed by a variable name to be bound to the rest of
1056 @code{sequence}.
1058 @example
1059 @group
1060 (seq-let [first second] [1 2 3 4]
1061   (list first second))
1062 @result{} (1 2)
1063 @end group
1064 @group
1065 (seq-let (_ a _ b) '(1 2 3 4)
1066   (list a b))
1067 @result{} (2 4)
1068 @end group
1069 @group
1070 (seq-let [a [b [c]]] [1 [2 [3]]]
1071   (list a b c))
1072 @result{} (1 2 3)
1073 @end group
1074 @group
1075 (seq-let [a b &rest others] [1 2 3 4]
1076   others)
1077 @end group
1078 @result{} [3 4]
1079 @end example
1080 @end defmac
1082 @defun seq-random-elt sequence
1083   This function returns an element of @var{sequence} taken at random.
1085 @example
1086 @group
1087 (seq-random-elt [1 2 3 4])
1088 @result{} 3
1089 (seq-random-elt [1 2 3 4])
1090 @result{} 2
1091 (seq-random-elt [1 2 3 4])
1092 @result{} 4
1093 (seq-random-elt [1 2 3 4])
1094 @result{} 2
1095 (seq-random-elt [1 2 3 4])
1096 @result{} 1
1097 @end group
1098 @end example
1100   If @var{sequence} is empty, this function signals an error.
1101 @end defun
1103 @node Arrays
1104 @section Arrays
1105 @cindex array
1107   An @dfn{array} object has slots that hold a number of other Lisp
1108 objects, called the elements of the array.  Any element of an array
1109 may be accessed in constant time.  In contrast, the time to access an
1110 element of a list is proportional to the position of that element in
1111 the list.
1113   Emacs defines four types of array, all one-dimensional:
1114 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
1115 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
1116 @dfn{char-tables} (@pxref{Char-Table Type}).  Vectors and char-tables
1117 can hold elements of any type, but strings can only hold characters,
1118 and bool-vectors can only hold @code{t} and @code{nil}.
1120   All four kinds of array share these characteristics:
1122 @itemize @bullet
1123 @item
1124 The first element of an array has index zero, the second element has
1125 index 1, and so on.  This is called @dfn{zero-origin} indexing.  For
1126 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
1128 @item
1129 The length of the array is fixed once you create it; you cannot
1130 change the length of an existing array.
1132 @item
1133 For purposes of evaluation, the array is a constant---i.e.,
1134 it evaluates to itself.
1136 @item
1137 The elements of an array may be referenced or changed with the functions
1138 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
1139 @end itemize
1141     When you create an array, other than a char-table, you must specify
1142 its length.  You cannot specify the length of a char-table, because that
1143 is determined by the range of character codes.
1145   In principle, if you want an array of text characters, you could use
1146 either a string or a vector.  In practice, we always choose strings for
1147 such applications, for four reasons:
1149 @itemize @bullet
1150 @item
1151 They occupy one-fourth the space of a vector of the same elements.
1153 @item
1154 Strings are printed in a way that shows the contents more clearly
1155 as text.
1157 @item
1158 Strings can hold text properties.  @xref{Text Properties}.
1160 @item
1161 Many of the specialized editing and I/O facilities of Emacs accept only
1162 strings.  For example, you cannot insert a vector of characters into a
1163 buffer the way you can insert a string.  @xref{Strings and Characters}.
1164 @end itemize
1166   By contrast, for an array of keyboard input characters (such as a key
1167 sequence), a vector may be necessary, because many keyboard input
1168 characters are outside the range that will fit in a string.  @xref{Key
1169 Sequence Input}.
1171 @node Array Functions
1172 @section Functions that Operate on Arrays
1174   In this section, we describe the functions that accept all types of
1175 arrays.
1177 @defun arrayp object
1178 This function returns @code{t} if @var{object} is an array (i.e., a
1179 vector, a string, a bool-vector or a char-table).
1181 @example
1182 @group
1183 (arrayp [a])
1184      @result{} t
1185 (arrayp "asdf")
1186      @result{} t
1187 (arrayp (syntax-table))    ;; @r{A char-table.}
1188      @result{} t
1189 @end group
1190 @end example
1191 @end defun
1193 @defun aref arr index
1194 @cindex array elements
1195 This function returns the @var{index}th element of the array or record
1196 @var{arr}.  The first element is at index zero.
1198 @example
1199 @group
1200 (setq primes [2 3 5 7 11 13])
1201      @result{} [2 3 5 7 11 13]
1202 (aref primes 4)
1203      @result{} 11
1204 @end group
1205 @group
1206 (aref "abcdefg" 1)
1207      @result{} 98           ; @r{@samp{b} is @acronym{ASCII} code 98.}
1208 @end group
1209 @end example
1211 See also the function @code{elt}, in @ref{Sequence Functions}.
1212 @end defun
1214 @defun aset array index object
1215 This function sets the @var{index}th element of @var{array} to be
1216 @var{object}.  It returns @var{object}.
1218 @example
1219 @group
1220 (setq w [foo bar baz])
1221      @result{} [foo bar baz]
1222 (aset w 0 'fu)
1223      @result{} fu
1225      @result{} [fu bar baz]
1226 @end group
1228 @group
1229 (setq x "asdfasfd")
1230      @result{} "asdfasfd"
1231 (aset x 3 ?Z)
1232      @result{} 90
1234      @result{} "asdZasfd"
1235 @end group
1236 @end example
1238 If @var{array} is a string and @var{object} is not a character, a
1239 @code{wrong-type-argument} error results.  The function converts a
1240 unibyte string to multibyte if necessary to insert a character.
1241 @end defun
1243 @defun fillarray array object
1244 This function fills the array @var{array} with @var{object}, so that
1245 each element of @var{array} is @var{object}.  It returns @var{array}.
1247 @example
1248 @group
1249 (setq a [a b c d e f g])
1250      @result{} [a b c d e f g]
1251 (fillarray a 0)
1252      @result{} [0 0 0 0 0 0 0]
1254      @result{} [0 0 0 0 0 0 0]
1255 @end group
1256 @group
1257 (setq s "When in the course")
1258      @result{} "When in the course"
1259 (fillarray s ?-)
1260      @result{} "------------------"
1261 @end group
1262 @end example
1264 If @var{array} is a string and @var{object} is not a character, a
1265 @code{wrong-type-argument} error results.
1266 @end defun
1268 The general sequence functions @code{copy-sequence} and @code{length}
1269 are often useful for objects known to be arrays.  @xref{Sequence Functions}.
1271 @node Vectors
1272 @section Vectors
1273 @cindex vector (type)
1275   A @dfn{vector} is a general-purpose array whose elements can be any
1276 Lisp objects.  (By contrast, the elements of a string can only be
1277 characters.  @xref{Strings and Characters}.)  Vectors are used in
1278 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
1279 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
1280 representation of a byte-compiled function (@pxref{Byte Compilation}),
1281 and more.
1283   Like other arrays, vectors use zero-origin indexing: the first
1284 element has index 0.
1286   Vectors are printed with square brackets surrounding the elements.
1287 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
1288 @code{a} is printed as @code{[a b a]}.  You can write vectors in the
1289 same way in Lisp input.
1291   A vector, like a string or a number, is considered a constant for
1292 evaluation: the result of evaluating it is the same vector.  This does
1293 not evaluate or even examine the elements of the vector.
1294 @xref{Self-Evaluating Forms}.
1296   Here are examples illustrating these principles:
1298 @example
1299 @group
1300 (setq avector [1 two '(three) "four" [five]])
1301      @result{} [1 two (quote (three)) "four" [five]]
1302 (eval avector)
1303      @result{} [1 two (quote (three)) "four" [five]]
1304 (eq avector (eval avector))
1305      @result{} t
1306 @end group
1307 @end example
1309 @node Vector Functions
1310 @section Functions for Vectors
1312   Here are some functions that relate to vectors:
1314 @defun vectorp object
1315 This function returns @code{t} if @var{object} is a vector.
1317 @example
1318 @group
1319 (vectorp [a])
1320      @result{} t
1321 (vectorp "asdf")
1322      @result{} nil
1323 @end group
1324 @end example
1325 @end defun
1327 @defun vector &rest objects
1328 This function creates and returns a vector whose elements are the
1329 arguments, @var{objects}.
1331 @example
1332 @group
1333 (vector 'foo 23 [bar baz] "rats")
1334      @result{} [foo 23 [bar baz] "rats"]
1335 (vector)
1336      @result{} []
1337 @end group
1338 @end example
1339 @end defun
1341 @defun make-vector length object
1342 This function returns a new vector consisting of @var{length} elements,
1343 each initialized to @var{object}.
1345 @example
1346 @group
1347 (setq sleepy (make-vector 9 'Z))
1348      @result{} [Z Z Z Z Z Z Z Z Z]
1349 @end group
1350 @end example
1351 @end defun
1353 @defun vconcat &rest sequences
1354 @cindex copying vectors
1355 This function returns a new vector containing all the elements of
1356 @var{sequences}.  The arguments @var{sequences} may be true lists,
1357 vectors, strings or bool-vectors.  If no @var{sequences} are given,
1358 the empty vector is returned.
1360 The value is either the empty vector, or is a newly constructed
1361 nonempty vector that is not @code{eq} to any existing vector.
1363 @example
1364 @group
1365 (setq a (vconcat '(A B C) '(D E F)))
1366      @result{} [A B C D E F]
1367 (eq a (vconcat a))
1368      @result{} nil
1369 @end group
1370 @group
1371 (vconcat)
1372      @result{} []
1373 (vconcat [A B C] "aa" '(foo (6 7)))
1374      @result{} [A B C 97 97 foo (6 7)]
1375 @end group
1376 @end example
1378 The @code{vconcat} function also allows byte-code function objects as
1379 arguments.  This is a special feature to make it easy to access the entire
1380 contents of a byte-code function object.  @xref{Byte-Code Objects}.
1382 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
1383 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
1384 in @ref{Building Lists}.
1385 @end defun
1387   The @code{append} function also provides a way to convert a vector into a
1388 list with the same elements:
1390 @example
1391 @group
1392 (setq avector [1 two (quote (three)) "four" [five]])
1393      @result{} [1 two (quote (three)) "four" [five]]
1394 (append avector nil)
1395      @result{} (1 two (quote (three)) "four" [five])
1396 @end group
1397 @end example
1399 @node Char-Tables
1400 @section Char-Tables
1401 @cindex char-tables
1402 @cindex extra slots of char-table
1404   A char-table is much like a vector, except that it is indexed by
1405 character codes.  Any valid character code, without modifiers, can be
1406 used as an index in a char-table.  You can access a char-table's
1407 elements with @code{aref} and @code{aset}, as with any array.  In
1408 addition, a char-table can have @dfn{extra slots} to hold additional
1409 data not associated with particular character codes.  Like vectors,
1410 char-tables are constants when evaluated, and can hold elements of any
1411 type.
1413 @cindex subtype of char-table
1414   Each char-table has a @dfn{subtype}, a symbol, which serves two
1415 purposes:
1417 @itemize @bullet
1418 @item
1419 The subtype provides an easy way to tell what the char-table is for.
1420 For instance, display tables are char-tables with @code{display-table}
1421 as the subtype, and syntax tables are char-tables with
1422 @code{syntax-table} as the subtype.  The subtype can be queried using
1423 the function @code{char-table-subtype}, described below.
1425 @item
1426 The subtype controls the number of @dfn{extra slots} in the
1427 char-table.  This number is specified by the subtype's
1428 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1429 Properties}), whose value should be an integer between 0 and 10.  If
1430 the subtype has no such symbol property, the char-table has no extra
1431 slots.
1432 @end itemize
1434 @cindex parent of char-table
1435   A char-table can have a @dfn{parent}, which is another char-table.  If
1436 it does, then whenever the char-table specifies @code{nil} for a
1437 particular character @var{c}, it inherits the value specified in the
1438 parent.  In other words, @code{(aref @var{char-table} @var{c})} returns
1439 the value from the parent of @var{char-table} if @var{char-table} itself
1440 specifies @code{nil}.
1442 @cindex default value of char-table
1443   A char-table can also have a @dfn{default value}.  If so, then
1444 @code{(aref @var{char-table} @var{c})} returns the default value
1445 whenever the char-table does not specify any other non-@code{nil} value.
1447 @defun make-char-table subtype &optional init
1448 Return a newly-created char-table, with subtype @var{subtype} (a
1449 symbol).  Each element is initialized to @var{init}, which defaults to
1450 @code{nil}.  You cannot alter the subtype of a char-table after the
1451 char-table is created.
1453 There is no argument to specify the length of the char-table, because
1454 all char-tables have room for any valid character code as an index.
1456 If @var{subtype} has the @code{char-table-extra-slots} symbol
1457 property, that specifies the number of extra slots in the char-table.
1458 This should be an integer between 0 and 10; otherwise,
1459 @code{make-char-table} raises an error.  If @var{subtype} has no
1460 @code{char-table-extra-slots} symbol property (@pxref{Property
1461 Lists}), the char-table has no extra slots.
1462 @end defun
1464 @defun char-table-p object
1465 This function returns @code{t} if @var{object} is a char-table, and
1466 @code{nil} otherwise.
1467 @end defun
1469 @defun char-table-subtype char-table
1470 This function returns the subtype symbol of @var{char-table}.
1471 @end defun
1473 There is no special function to access default values in a char-table.
1474 To do that, use @code{char-table-range} (see below).
1476 @defun char-table-parent char-table
1477 This function returns the parent of @var{char-table}.  The parent is
1478 always either @code{nil} or another char-table.
1479 @end defun
1481 @defun set-char-table-parent char-table new-parent
1482 This function sets the parent of @var{char-table} to @var{new-parent}.
1483 @end defun
1485 @defun char-table-extra-slot char-table n
1486 This function returns the contents of extra slot @var{n} (zero based)
1487 of @var{char-table}.  The number of extra slots in a char-table is
1488 determined by its subtype.
1489 @end defun
1491 @defun set-char-table-extra-slot char-table n value
1492 This function stores @var{value} in extra slot @var{n} (zero based) of
1493 @var{char-table}.
1494 @end defun
1496   A char-table can specify an element value for a single character code;
1497 it can also specify a value for an entire character set.
1499 @defun char-table-range char-table range
1500 This returns the value specified in @var{char-table} for a range of
1501 characters @var{range}.  Here are the possibilities for @var{range}:
1503 @table @asis
1504 @item @code{nil}
1505 Refers to the default value.
1507 @item @var{char}
1508 Refers to the element for character @var{char}
1509 (supposing @var{char} is a valid character code).
1511 @item @code{(@var{from} . @var{to})}
1512 A cons cell refers to all the characters in the inclusive range
1513 @samp{[@var{from}..@var{to}]}.
1514 @end table
1515 @end defun
1517 @defun set-char-table-range char-table range value
1518 This function sets the value in @var{char-table} for a range of
1519 characters @var{range}.  Here are the possibilities for @var{range}:
1521 @table @asis
1522 @item @code{nil}
1523 Refers to the default value.
1525 @item @code{t}
1526 Refers to the whole range of character codes.
1528 @item @var{char}
1529 Refers to the element for character @var{char}
1530 (supposing @var{char} is a valid character code).
1532 @item @code{(@var{from} . @var{to})}
1533 A cons cell refers to all the characters in the inclusive range
1534 @samp{[@var{from}..@var{to}]}.
1535 @end table
1536 @end defun
1538 @defun map-char-table function char-table
1539 This function calls its argument @var{function} for each element of
1540 @var{char-table} that has a non-@code{nil} value.  The call to
1541 @var{function} is with two arguments, a key and a value.  The key
1542 is a possible @var{range} argument for @code{char-table-range}---either
1543 a valid character or a cons cell @code{(@var{from} . @var{to})},
1544 specifying a range of characters that share the same value.  The value is
1545 what @code{(char-table-range @var{char-table} @var{key})} returns.
1547 Overall, the key-value pairs passed to @var{function} describe all the
1548 values stored in @var{char-table}.
1550 The return value is always @code{nil}; to make calls to
1551 @code{map-char-table} useful, @var{function} should have side effects.
1552 For example, here is how to examine the elements of the syntax table:
1554 @example
1555 (let (accumulator)
1556    (map-char-table
1557     #'(lambda (key value)
1558         (setq accumulator
1559               (cons (list
1560                      (if (consp key)
1561                          (list (car key) (cdr key))
1562                        key)
1563                      value)
1564                     accumulator)))
1565     (syntax-table))
1566    accumulator)
1567 @result{}
1568 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1569  ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1570  ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1571 @end example
1572 @end defun
1574 @node Bool-Vectors
1575 @section Bool-vectors
1576 @cindex Bool-vectors
1578   A bool-vector is much like a vector, except that it stores only the
1579 values @code{t} and @code{nil}.  If you try to store any non-@code{nil}
1580 value into an element of the bool-vector, the effect is to store
1581 @code{t} there.  As with all arrays, bool-vector indices start from 0,
1582 and the length cannot be changed once the bool-vector is created.
1583 Bool-vectors are constants when evaluated.
1585   Several functions work specifically with bool-vectors; aside
1586 from that, you manipulate them with same functions used for other kinds
1587 of arrays.
1589 @defun make-bool-vector length initial
1590 Return a new bool-vector of @var{length} elements,
1591 each one initialized to @var{initial}.
1592 @end defun
1594 @defun bool-vector &rest objects
1595 This function creates and returns a bool-vector whose elements are the
1596 arguments, @var{objects}.
1597 @end defun
1599 @defun bool-vector-p object
1600 This returns @code{t} if @var{object} is a bool-vector,
1601 and @code{nil} otherwise.
1602 @end defun
1604 There are also some bool-vector set operation functions, described below:
1606 @defun bool-vector-exclusive-or a b &optional c
1607 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1608 If optional argument @var{c} is given, the result of this operation is
1609 stored into @var{c}.  All arguments should be bool vectors of the same length.
1610 @end defun
1612 @defun bool-vector-union a b &optional c
1613 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}.  If
1614 optional argument @var{c} is given, the result of this operation is
1615 stored into @var{c}.  All arguments should be bool vectors of the same length.
1616 @end defun
1618 @defun bool-vector-intersection a b &optional c
1619 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}.  If
1620 optional argument @var{c} is given, the result of this operation is
1621 stored into @var{c}.  All arguments should be bool vectors of the same length.
1622 @end defun
1624 @defun bool-vector-set-difference a b &optional c
1625 Return @dfn{set difference} of bool vectors @var{a} and @var{b}.  If
1626 optional argument @var{c} is given, the result of this operation is
1627 stored into @var{c}.  All arguments should be bool vectors of the same length.
1628 @end defun
1630 @defun bool-vector-not a &optional b
1631 Return @dfn{set complement} of bool vector @var{a}.  If optional
1632 argument @var{b} is given, the result of this operation is stored into
1633 @var{b}.  All arguments should be bool vectors of the same length.
1634 @end defun
1636 @defun bool-vector-subsetp a b
1637 Return @code{t} if every @code{t} value in @var{a} is also @code{t} in
1638 @var{b}, @code{nil} otherwise.  All arguments should be bool vectors of the
1639 same length.
1640 @end defun
1642 @defun bool-vector-count-consecutive a b i
1643 Return the number of consecutive elements in @var{a} equal @var{b}
1644 starting at @var{i}.  @code{a} is a bool vector, @var{b} is @code{t}
1645 or @code{nil}, and @var{i} is an index into @code{a}.
1646 @end defun
1648 @defun bool-vector-count-population a
1649 Return the number of elements that are @code{t} in bool vector @var{a}.
1650 @end defun
1652   The printed form represents up to 8 boolean values as a single
1653 character:
1655 @example
1656 @group
1657 (bool-vector t nil t nil)
1658      @result{} #&4"^E"
1659 (bool-vector)
1660      @result{} #&0""
1661 @end group
1662 @end example
1664 You can use @code{vconcat} to print a bool-vector like other vectors:
1666 @example
1667 @group
1668 (vconcat (bool-vector nil t nil t))
1669      @result{} [nil t nil t]
1670 @end group
1671 @end example
1673   Here is another example of creating, examining, and updating a
1674 bool-vector:
1676 @example
1677 (setq bv (make-bool-vector 5 t))
1678      @result{} #&5"^_"
1679 (aref bv 1)
1680      @result{} t
1681 (aset bv 3 nil)
1682      @result{} nil
1684      @result{} #&5"^W"
1685 @end example
1687 @noindent
1688 These results make sense because the binary codes for control-_ and
1689 control-W are 11111 and 10111, respectively.
1691 @node Rings
1692 @section Managing a Fixed-Size Ring of Objects
1694 @cindex ring data structure
1695   A @dfn{ring} is a fixed-size data structure that supports insertion,
1696 deletion, rotation, and modulo-indexed reference and traversal.  An
1697 efficient ring data structure is implemented by the @code{ring}
1698 package.  It provides the functions listed in this section.
1700   Note that several rings in Emacs, like the kill ring and the
1701 mark ring, are actually implemented as simple lists, @emph{not} using
1702 the @code{ring} package; thus the following functions won't work on
1703 them.
1705 @defun make-ring size
1706 This returns a new ring capable of holding @var{size} objects.
1707 @var{size} should be an integer.
1708 @end defun
1710 @defun ring-p object
1711 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1712 @end defun
1714 @defun ring-size ring
1715 This returns the maximum capacity of the @var{ring}.
1716 @end defun
1718 @defun ring-length ring
1719 This returns the number of objects that @var{ring} currently contains.
1720 The value will never exceed that returned by @code{ring-size}.
1721 @end defun
1723 @defun ring-elements ring
1724 This returns a list of the objects in @var{ring}, in order, newest first.
1725 @end defun
1727 @defun ring-copy ring
1728 This returns a new ring which is a copy of @var{ring}.
1729 The new ring contains the same (@code{eq}) objects as @var{ring}.
1730 @end defun
1732 @defun ring-empty-p ring
1733 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1734 @end defun
1736   The newest element in the ring always has index 0.  Higher indices
1737 correspond to older elements.  Indices are computed modulo the ring
1738 length.  Index @minus{}1 corresponds to the oldest element, @minus{}2
1739 to the next-oldest, and so forth.
1741 @defun ring-ref ring index
1742 This returns the object in @var{ring} found at index @var{index}.
1743 @var{index} may be negative or greater than the ring length.  If
1744 @var{ring} is empty, @code{ring-ref} signals an error.
1745 @end defun
1747 @defun ring-insert ring object
1748 This inserts @var{object} into @var{ring}, making it the newest
1749 element, and returns @var{object}.
1751 If the ring is full, insertion removes the oldest element to
1752 make room for the new element.
1753 @end defun
1755 @defun ring-remove ring &optional index
1756 Remove an object from @var{ring}, and return that object.  The
1757 argument @var{index} specifies which item to remove; if it is
1758 @code{nil}, that means to remove the oldest item.  If @var{ring} is
1759 empty, @code{ring-remove} signals an error.
1760 @end defun
1762 @defun ring-insert-at-beginning ring object
1763 This inserts @var{object} into @var{ring}, treating it as the oldest
1764 element.  The return value is not significant.
1766 If the ring is full, this function removes the newest element to make
1767 room for the inserted element.
1768 @end defun
1770 @cindex fifo data structure
1771   If you are careful not to exceed the ring size, you can
1772 use the ring as a first-in-first-out queue.  For example:
1774 @lisp
1775 (let ((fifo (make-ring 5)))
1776   (mapc (lambda (obj) (ring-insert fifo obj))
1777         '(0 one "two"))
1778   (list (ring-remove fifo) t
1779         (ring-remove fifo) t
1780         (ring-remove fifo)))
1781      @result{} (0 t one t "two")
1782 @end lisp