]> code.delx.au - gnu-emacs/blob - doc/lispref/sequences.texi
Improve the semantic of seq-some
[gnu-emacs] / doc / lispref / sequences.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2015 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
9
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.
14
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.
18
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.
24
25 The following diagram shows the relationship between these types:
26
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
47
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
58
59 @node Sequence Functions
60 @section Sequences
61
62 This section describes functions that accept any kind of sequence.
63
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
68
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 This function returns the number of elements in @var{sequence}. If
76 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
77 signaled. Circular lists may cause an infinite loop. For a
78 char-table, the value returned is always one more than the maximum
79 Emacs character code.
80
81 @xref{Definition of safe-length}, for the related function @code{safe-length}.
82
83 @example
84 @group
85 (length '(1 2 3))
86 @result{} 3
87 @end group
88 @group
89 (length ())
90 @result{} 0
91 @end group
92 @group
93 (length "foobar")
94 @result{} 6
95 @end group
96 @group
97 (length [1 2 3])
98 @result{} 3
99 @end group
100 @group
101 (length (make-bool-vector 5 nil))
102 @result{} 5
103 @end group
104 @end example
105 @end defun
106
107 @noindent
108 See also @code{string-bytes}, in @ref{Text Representations}.
109
110 If you need to compute the width of a string on display, you should use
111 @code{string-width} (@pxref{Size of Displayed Text}), not @code{length},
112 since @code{length} only counts the number of characters, but does not
113 account for the display width of each character.
114
115 @defun elt sequence index
116 @cindex elements of sequences
117 This function returns the element of @var{sequence} indexed by
118 @var{index}. Legitimate values of @var{index} are integers ranging
119 from 0 up to one less than the length of @var{sequence}. If
120 @var{sequence} is a list, out-of-range values behave as for
121 @code{nth}. @xref{Definition of nth}. Otherwise, out-of-range values
122 trigger an @code{args-out-of-range} error.
123
124 @example
125 @group
126 (elt [1 2 3 4] 2)
127 @result{} 3
128 @end group
129 @group
130 (elt '(1 2 3 4) 2)
131 @result{} 3
132 @end group
133 @group
134 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
135 (string (elt "1234" 2))
136 @result{} "3"
137 @end group
138 @group
139 (elt [1 2 3 4] 4)
140 @error{} Args out of range: [1 2 3 4], 4
141 @end group
142 @group
143 (elt [1 2 3 4] -1)
144 @error{} Args out of range: [1 2 3 4], -1
145 @end group
146 @end example
147
148 This function generalizes @code{aref} (@pxref{Array Functions}) and
149 @code{nth} (@pxref{Definition of nth}).
150 @end defun
151
152 @defun copy-sequence sequence
153 @cindex copying sequences
154 This function returns a copy of @var{sequence}. The copy is the same
155 type of object as the original sequence, and it has the same elements
156 in the same order.
157
158 Storing a new element into the copy does not affect the original
159 @var{sequence}, and vice versa. However, the elements of the new
160 sequence are not copies; they are identical (@code{eq}) to the elements
161 of the original. Therefore, changes made within these elements, as
162 found via the copied sequence, are also visible in the original
163 sequence.
164
165 If the sequence is a string with text properties, the property list in
166 the copy is itself a copy, not shared with the original's property
167 list. However, the actual values of the properties are shared.
168 @xref{Text Properties}.
169
170 This function does not work for dotted lists. Trying to copy a
171 circular list may cause an infinite loop.
172
173 See also @code{append} in @ref{Building Lists}, @code{concat} in
174 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
175 for other ways to copy sequences.
176
177 @example
178 @group
179 (setq bar '(1 2))
180 @result{} (1 2)
181 @end group
182 @group
183 (setq x (vector 'foo bar))
184 @result{} [foo (1 2)]
185 @end group
186 @group
187 (setq y (copy-sequence x))
188 @result{} [foo (1 2)]
189 @end group
190
191 @group
192 (eq x y)
193 @result{} nil
194 @end group
195 @group
196 (equal x y)
197 @result{} t
198 @end group
199 @group
200 (eq (elt x 1) (elt y 1))
201 @result{} t
202 @end group
203
204 @group
205 ;; @r{Replacing an element of one sequence.}
206 (aset x 0 'quux)
207 x @result{} [quux (1 2)]
208 y @result{} [foo (1 2)]
209 @end group
210
211 @group
212 ;; @r{Modifying the inside of a shared element.}
213 (setcar (aref x 1) 69)
214 x @result{} [quux (69 2)]
215 y @result{} [foo (69 2)]
216 @end group
217 @end example
218 @end defun
219
220 @defun reverse sequence
221 @cindex string reverse
222 @cindex list reverse
223 @cindex vector reverse
224 @cindex sequence reverse
225 This function creates a new sequence whose elements are the elements
226 of @var{sequence}, but in reverse order. The original argument @var{sequence}
227 is @emph{not} altered. Note that char-tables cannot be reversed.
228
229 @example
230 @group
231 (setq x '(1 2 3 4))
232 @result{} (1 2 3 4)
233 @end group
234 @group
235 (reverse x)
236 @result{} (4 3 2 1)
237 x
238 @result{} (1 2 3 4)
239 @end group
240 @group
241 (setq x [1 2 3 4])
242 @result{} [1 2 3 4]
243 @end group
244 @group
245 (reverse x)
246 @result{} [4 3 2 1]
247 x
248 @result{} [1 2 3 4]
249 @end group
250 @group
251 (setq x "xyzzy")
252 @result{} "xyzzy"
253 @end group
254 @group
255 (reverse x)
256 @result{} "yzzyx"
257 x
258 @result{} "xyzzy"
259 @end group
260 @end example
261 @end defun
262
263 @defun nreverse sequence
264 @cindex reversing a string
265 @cindex reversing a list
266 @cindex reversing a vector
267 This function reverses the order of the elements of @var{sequence}.
268 Unlike @code{reverse} the original @var{sequence} may be modified.
269
270 For example:
271
272 @example
273 @group
274 (setq x '(a b c))
275 @result{} (a b c)
276 @end group
277 @group
278 x
279 @result{} (a b c)
280 (nreverse x)
281 @result{} (c b a)
282 @end group
283 @group
284 ;; @r{The cons cell that was first is now last.}
285 x
286 @result{} (a)
287 @end group
288 @end example
289
290 To avoid confusion, we usually store the result of @code{nreverse}
291 back in the same variable which held the original list:
292
293 @example
294 (setq x (nreverse x))
295 @end example
296
297 Here is the @code{nreverse} of our favorite example, @code{(a b c)},
298 presented graphically:
299
300 @smallexample
301 @group
302 @r{Original list head:} @r{Reversed list:}
303 ------------- ------------- ------------
304 | car | cdr | | car | cdr | | car | cdr |
305 | a | nil |<-- | b | o |<-- | c | o |
306 | | | | | | | | | | | | |
307 ------------- | --------- | - | -------- | -
308 | | | |
309 ------------- ------------
310 @end group
311 @end smallexample
312
313 For the vector, it is even simpler because you don't need setq:
314
315 @example
316 (setq x [1 2 3 4])
317 @result{} [1 2 3 4]
318 (nreverse x)
319 @result{} [4 3 2 1]
320 x
321 @result{} [4 3 2 1]
322 @end example
323
324 Note that unlike @code{reverse}, this function doesn't work with strings.
325 Although you can alter string data by using @code{aset}, it is strongly
326 encouraged to treat strings as immutable.
327
328 @end defun
329
330 @defun sort sequence predicate
331 @cindex stable sort
332 @cindex sorting lists
333 @cindex sorting vectors
334 This function sorts @var{sequence} stably. Note that this function doesn't work
335 for all sequences; it may be used only for lists and vectors. If @var{sequence}
336 is a list, it is modified destructively. This functions returns the sorted
337 @var{sequence} and compares elements using @var{predicate}. A stable sort is
338 one in which elements with equal sort keys maintain their relative order before
339 and after the sort. Stability is important when successive sorts are used to
340 order elements according to different criteria.
341
342 The argument @var{predicate} must be a function that accepts two
343 arguments. It is called with two elements of @var{sequence}. To get an
344 increasing order sort, the @var{predicate} should return non-@code{nil} if the
345 first element is ``less than'' the second, or @code{nil} if not.
346
347 The comparison function @var{predicate} must give reliable results for
348 any given pair of arguments, at least within a single call to
349 @code{sort}. It must be @dfn{antisymmetric}; that is, if @var{a} is
350 less than @var{b}, @var{b} must not be less than @var{a}. It must be
351 @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
352 is less than @var{c}, then @var{a} must be less than @var{c}. If you
353 use a comparison function which does not meet these requirements, the
354 result of @code{sort} is unpredictable.
355
356 The destructive aspect of @code{sort} for lists is that it rearranges the
357 cons cells forming @var{sequence} by changing @sc{cdr}s. A nondestructive
358 sort function would create new cons cells to store the elements in their
359 sorted order. If you wish to make a sorted copy without destroying the
360 original, copy it first with @code{copy-sequence} and then sort.
361
362 Sorting does not change the @sc{car}s of the cons cells in @var{sequence};
363 the cons cell that originally contained the element @code{a} in
364 @var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
365 appears in a different position in the list due to the change of
366 @sc{cdr}s. For example:
367
368 @example
369 @group
370 (setq nums '(1 3 2 6 5 4 0))
371 @result{} (1 3 2 6 5 4 0)
372 @end group
373 @group
374 (sort nums '<)
375 @result{} (0 1 2 3 4 5 6)
376 @end group
377 @group
378 nums
379 @result{} (1 2 3 4 5 6)
380 @end group
381 @end example
382
383 @noindent
384 @strong{Warning}: Note that the list in @code{nums} no longer contains
385 0; this is the same cons cell that it was before, but it is no longer
386 the first one in the list. Don't assume a variable that formerly held
387 the argument now holds the entire sorted list! Instead, save the result
388 of @code{sort} and use that. Most often we store the result back into
389 the variable that held the original list:
390
391 @example
392 (setq nums (sort nums '<))
393 @end example
394
395 For the better understanding of what stable sort is, consider the following
396 vector example. After sorting, all items whose @code{car} is 8 are grouped
397 at the beginning of @code{vector}, but their relative order is preserved.
398 All items whose @code{car} is 9 are grouped at the end of @code{vector},
399 but their relative order is also preserved:
400
401 @example
402 @group
403 (setq
404 vector
405 (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
406 '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")))
407 @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz")
408 (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")]
409 @end group
410 @group
411 (sort vector (lambda (x y) (< (car x) (car y))))
412 @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
413 (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
414 @end group
415 @end example
416
417 @xref{Sorting}, for more functions that perform sorting.
418 See @code{documentation} in @ref{Accessing Documentation}, for a
419 useful example of @code{sort}.
420 @end defun
421
422 @cindex sequence functions in seq
423 @cindex seq library
424 The @file{seq.el} library provides the following additional sequence
425 manipulation macros and functions, prefixed with @code{seq-}. To use
426 them, you must first load the @file{seq} library.
427
428 All functions defined in this library are free of side-effects;
429 i.e., they do not modify any sequence (list, vector, or string) that
430 you pass as an argument. Unless otherwise stated, the result is a
431 sequence of the same type as the input. For those functions that take
432 a predicate, this should be a function of one argument.
433
434 @defun seq-drop sequence n
435 This function returns all but the first @var{n} (an integer)
436 elements of @var{sequence}. If @var{n} is negative or zero,
437 the result is @var{sequence}.
438
439 @example
440 @group
441 (seq-drop [1 2 3 4 5 6] 3)
442 @result{} [4 5 6]
443 @end group
444 @group
445 (seq-drop "hello world" -4)
446 @result{} "hello world"
447 @end group
448 @end example
449 @end defun
450
451 @defun seq-take sequence n
452 This function returns the first @var{n} (an integer) elements of
453 @var{sequence}. If @var{n} is negative or zero, the result
454 is @code{nil}.
455
456 @example
457 @group
458 (seq-take '(1 2 3 4) 3)
459 @result{} (1 2 3)
460 @end group
461 @group
462 (seq-take [1 2 3 4] 0)
463 @result{} []
464 @end group
465 @end example
466 @end defun
467
468 @defun seq-take-while predicate sequence
469 This function returns the members of @var{sequence} in order,
470 stopping before the first one for which @var{predicate} returns @code{nil}.
471
472 @example
473 @group
474 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
475 @result{} (1 2 3)
476 @end group
477 @group
478 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
479 @result{} []
480 @end group
481 @end example
482 @end defun
483
484 @defun seq-drop-while predicate sequence
485 This function returns the members of @var{sequence} in order,
486 starting from the first one for which @var{predicate} returns @code{nil}.
487
488 @example
489 @group
490 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
491 @result{} (-1 -2)
492 @end group
493 @group
494 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
495 @result{} [1 4 6]
496 @end group
497 @end example
498 @end defun
499
500 @defun seq-filter predicate sequence
501 @cindex filtering sequences
502 This function returns a list of all the elements in @var{sequence}
503 for which @var{predicate} returns non-@code{nil}.
504
505 @example
506 @group
507 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
508 @result{} (1 3 5)
509 @end group
510 @group
511 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
512 @result{} nil
513 @end group
514 @end example
515 @end defun
516
517 @defun seq-remove predicate sequence
518 @cindex removing from sequences
519 This function returns a list of all the elements in @var{sequence}
520 for which @var{predicate} returns @code{nil}.
521
522 @example
523 @group
524 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
525 @result{} (-1 -3)
526 @end group
527 @group
528 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
529 @result{} nil
530 @end group
531 @end example
532 @end defun
533
534 @defun seq-reduce function sequence initial-value
535 @cindex reducing sequences
536 This function returns the result of calling @var{function} with
537 @var{initial-value} and the first element of @var{sequence}, then calling
538 @var{function} with that result and the second element of @var{sequence},
539 then with that result and the third element of @var{sequence}, etc.
540 @var{function} should be a function of two arguments. If
541 @var{sequence} is empty, this returns @var{initial-value} without
542 calling @var{function}.
543
544 @example
545 @group
546 (seq-reduce #'+ [1 2 3 4] 0)
547 @result{} 10
548 @end group
549 @group
550 (seq-reduce #'+ '(1 2 3 4) 5)
551 @result{} 15
552 @end group
553 @group
554 (seq-reduce #'+ '() 3)
555 @result{} 3
556 @end group
557 @end example
558 @end defun
559
560 @defun seq-some predicate sequence
561 This function returns non-@code{nil} if @var{predicate} returns
562 non-@code{nil} for any element of @var{sequence}. If so, the returned
563 value is the value returned by @var{predicate}.
564
565 @example
566 @group
567 (seq-some #'numberp ["abc" 1 nil])
568 @result{} t
569 @end group
570 @group
571 (seq-some #'numberp ["abc" "def"])
572 @result{} nil
573 @end group
574 @group
575 (seq-some #'null ["abc" 1 nil])
576 @result{} t
577 @end group
578 @end example
579 @end defun
580
581 @defun seq-every-p predicate sequence
582 This function returns non-@code{nil} if applying @var{predicate}
583 to every element of @var{sequence} returns non-@code{nil}.
584
585 @example
586 @group
587 (seq-every-p #'numberp [2 4 6])
588 @result{} t
589 @end group
590 @group
591 (seq-some #'numberp [2 4 "6"])
592 @result{} nil
593 @end group
594 @end example
595 @end defun
596
597 @defun seq-empty-p sequence
598 This function returns non-@code{nil} if @var{sequence} is empty.
599
600 @example
601 @group
602 (seq-empty-p "not empty")
603 @result{} nil
604 @end group
605 @group
606 (seq-empty-p "")
607 @result{} t
608 @end group
609 @end example
610 @end defun
611
612 @defun seq-count predicate sequence
613 This function returns the number of elements in @var{sequence} for which
614 @var{predicate} returns non-@code{nil}.
615
616 @example
617 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
618 @result{} 2
619 @end example
620 @end defun
621
622 @cindex sorting sequences
623 @defun seq-sort function sequence
624 This function returns a copy of @var{sequence} that is sorted
625 according to @var{function}, a function of two arguments that returns
626 non-@code{nil} if the first argument should sort before the second.
627 @end defun
628
629 @defun seq-contains sequence elt &optional function
630 This function returns the first element in @var{sequence} that is equal to
631 @var{elt}. If the optional argument @var{function} is non-@code{nil},
632 it is a function of two arguments to use instead of the default @code{equal}.
633
634 @example
635 @group
636 (seq-contains '(symbol1 symbol2) 'symbol1)
637 @result{} symbol1
638 @end group
639 @group
640 (seq-contains '(symbol1 symbol2) 'symbol3)
641 @result{} nil
642 @end group
643 @end example
644
645 @end defun
646
647 @defun seq-uniq sequence &optional function
648 This function returns a list of the elements of @var{sequence} with
649 duplicates removed. If the optional argument @var{function} is non-@code{nil},
650 it is a function of two arguments to use instead of the default @code{equal}.
651
652 @example
653 @group
654 (seq-uniq '(1 2 2 1 3))
655 @result{} (1 2 3)
656 @end group
657 @group
658 (seq-uniq '(1 2 2.0 1.0) #'=)
659 @result{} [3 4]
660 @end group
661 @end example
662 @end defun
663
664 @defun seq-subseq sequence start &optional end
665 This function returns a subset of @var{sequence} from @var{start}
666 to @var{end}, both integers (@var{end} defaults to the last element).
667 If @var{start} or @var{end} is negative, it counts from the end of
668 @var{sequence}.
669
670 @example
671 @group
672 (seq-subseq '(1 2 3 4 5) 1)
673 @result{} (2 3 4 5)
674 @end group
675 @group
676 (seq-subseq '[1 2 3 4 5] 1 3)
677 @result{} [2 3]
678 @end group
679 @group
680 (seq-subseq '[1 2 3 4 5] -3 -1)
681 @result{} [3 4]
682 @end group
683 @end example
684 @end defun
685
686 @defun seq-concatenate type &rest sequences
687 This function returns a sequence of type @var{type} made of the
688 concatenation of @var{sequences}. @var{type} may be: @code{vector},
689 @code{list} or @code{string}.
690
691 @example
692 @group
693 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
694 @result{} (1 2 3 5 6)
695 @end group
696 @group
697 (seq-concatenate 'string "Hello " "world")
698 @result{} "Hello world"
699 @end group
700 @end example
701 @end defun
702
703 @defun seq-mapcat function sequence &optional type
704 This function returns the result of applying @code{seq-concatenate}
705 to the result of applying @var{function} to each element of
706 @var{sequence}. The result is a sequence of type @var{type}, or a
707 list if @var{type} is @code{nil}.
708
709 @example
710 @group
711 (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
712 @result{} (1 2 3 4 5 6)
713 @end group
714 @end example
715 @end defun
716
717 @defun seq-partition sequence n
718 This function returns a list of the elements of @var{sequence}
719 grouped into sub-sequences of length @var{n}. The last sequence may
720 contain less elements than @var{n}. @var{n} must be an integer. If
721 @var{n} is a negative integer or 0, nil is returned.
722
723 @example
724 @group
725 (seq-partition '(0 1 2 3 4 5 6 7) 3)
726 @result{} ((0 1 2) (3 4 5) (6 7))
727 @end group
728 @end example
729 @end defun
730
731 @defun seq-intersection sequence1 sequence2 &optional function
732 This function returns a list of the elements that appear both in
733 @var{sequence1} and @var{sequence2}. If the optional argument
734 @var{function} is non-@code{nil}, it is a function of two arguments to
735 use to compare elements instead of the default @code{equal}.
736
737 @example
738 @group
739 (seq-intersection [2 3 4 5] [1 3 5 6 7])
740 @result{} (3 5)
741 @end group
742 @end example
743 @end defun
744
745
746 @defun seq-difference sequence1 sequence2 &optional function
747 This function returns a list of the elements that appear in
748 @var{sequence1} but not in @var{sequence2}. If the optional argument
749 @var{function} is non-@code{nil}, it is a function of two arguments to
750 use to compare elements instead of the default @code{equal}.
751
752 @example
753 @group
754 (seq-difference '(2 3 4 5) [1 3 5 6 7])
755 @result{} (2 4)
756 @end group
757 @end example
758 @end defun
759
760 @defun seq-group-by function sequence
761 This function separates the elements of @var{sequence} into an alist
762 whose keys are the result of applying @var{function} to each element
763 of @var{sequence}. Keys are compared using @code{equal}.
764
765 @example
766 @group
767 (seq-group-by #'integerp '(1 2.1 3 2 3.2))
768 @result{} ((t 1 3 2) (nil 2.1 3.2))
769 @end group
770 @group
771 (seq-group-by #'car '((a 1) (b 2) (a 3) (c 4)))
772 @result{} ((b (b 2)) (a (a 1) (a 3)) (c (c 4)))
773 @end group
774 @end example
775 @end defun
776
777 @defun seq-into sequence type
778 This function converts the sequence @var{sequence} into a sequence
779 of type @var{type}. @var{type} can be one of the following symbols:
780 @code{vector}, @code{string} or @code{list}.
781
782 @example
783 @group
784 (seq-into [1 2 3] 'list)
785 @result{} (1 2 3)
786 @end group
787 @group
788 (seq-into nil 'vector)
789 @result{} []
790 @end group
791 @group
792 (seq-into "hello" 'vector)
793 @result{} [104 101 108 108 111]
794 @end group
795 @end example
796 @end defun
797
798 @defun seq-min sequence
799 This function returns the smallest element of
800 @var{sequence}. @var{sequence} must be a sequence of numbers or
801 markers.
802
803 @example
804 @group
805 (seq-min [3 1 2])
806 @result{} 1
807 @end group
808 @group
809 (seq-min "Hello")
810 @result{} 72
811 @end group
812 @end example
813 @end defun
814
815 @defun seq-max sequence
816 This function returns the largest element of
817 @var{sequence}. @var{sequence} must be a sequence of numbers or
818 markers.
819
820 @example
821 @group
822 (seq-max [1 3 2])
823 @result{} 3
824 @end group
825 @group
826 (seq-max "Hello")
827 @result{} 111
828 @end group
829 @end example
830 @end defun
831
832 @defmac seq-doseq (var sequence) body@dots{}
833 @cindex sequence iteration
834 This macro is like @code{dolist}, except that @var{sequence} can be a list,
835 vector or string (@pxref{Iteration} for more information about the
836 @code{dolist} macro). This is primarily useful for side-effects.
837 @end defmac
838
839 @defmac seq-let arguments sequence body@dots{}
840 @cindex sequence destructuring
841 This macro binds the variables defined in @var{arguments} to the
842 elements of the sequence @var{sequence}. @var{arguments} can itself
843 include sequences allowing for nested destructuring.
844
845 The @var{arguments} sequence can also include the @code{&rest} marker
846 followed by a variable name to be bound to the rest of
847 @code{sequence}.
848
849 @example
850 @group
851 (seq-let [first second] [1 2 3 4]
852 (list first second))
853 @result{} (1 2)
854 @end group
855 @group
856 (seq-let (_ a _ b) '(1 2 3 4)
857 (list a b))
858 @result{} (2 4)
859 @end group
860 @group
861 (seq-let [a [b [c]]] [1 [2 [3]]]
862 (list a b c))
863 @result{} (1 2 3)
864 @end group
865 @group
866 (seq-let [a b &rest others] [1 2 3 4]
867 others)
868 @end group
869 @result{} [3 4]
870 @end example
871 @end defmac
872
873
874 @node Arrays
875 @section Arrays
876 @cindex array
877
878 An @dfn{array} object has slots that hold a number of other Lisp
879 objects, called the elements of the array. Any element of an array
880 may be accessed in constant time. In contrast, the time to access an
881 element of a list is proportional to the position of that element in
882 the list.
883
884 Emacs defines four types of array, all one-dimensional:
885 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
886 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
887 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
888 can hold elements of any type, but strings can only hold characters,
889 and bool-vectors can only hold @code{t} and @code{nil}.
890
891 All four kinds of array share these characteristics:
892
893 @itemize @bullet
894 @item
895 The first element of an array has index zero, the second element has
896 index 1, and so on. This is called @dfn{zero-origin} indexing. For
897 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
898
899 @item
900 The length of the array is fixed once you create it; you cannot
901 change the length of an existing array.
902
903 @item
904 For purposes of evaluation, the array is a constant---i.e.,
905 it evaluates to itself.
906
907 @item
908 The elements of an array may be referenced or changed with the functions
909 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
910 @end itemize
911
912 When you create an array, other than a char-table, you must specify
913 its length. You cannot specify the length of a char-table, because that
914 is determined by the range of character codes.
915
916 In principle, if you want an array of text characters, you could use
917 either a string or a vector. In practice, we always choose strings for
918 such applications, for four reasons:
919
920 @itemize @bullet
921 @item
922 They occupy one-fourth the space of a vector of the same elements.
923
924 @item
925 Strings are printed in a way that shows the contents more clearly
926 as text.
927
928 @item
929 Strings can hold text properties. @xref{Text Properties}.
930
931 @item
932 Many of the specialized editing and I/O facilities of Emacs accept only
933 strings. For example, you cannot insert a vector of characters into a
934 buffer the way you can insert a string. @xref{Strings and Characters}.
935 @end itemize
936
937 By contrast, for an array of keyboard input characters (such as a key
938 sequence), a vector may be necessary, because many keyboard input
939 characters are outside the range that will fit in a string. @xref{Key
940 Sequence Input}.
941
942 @node Array Functions
943 @section Functions that Operate on Arrays
944
945 In this section, we describe the functions that accept all types of
946 arrays.
947
948 @defun arrayp object
949 This function returns @code{t} if @var{object} is an array (i.e., a
950 vector, a string, a bool-vector or a char-table).
951
952 @example
953 @group
954 (arrayp [a])
955 @result{} t
956 (arrayp "asdf")
957 @result{} t
958 (arrayp (syntax-table)) ;; @r{A char-table.}
959 @result{} t
960 @end group
961 @end example
962 @end defun
963
964 @defun aref array index
965 @cindex array elements
966 This function returns the @var{index}th element of @var{array}. The
967 first element is at index zero.
968
969 @example
970 @group
971 (setq primes [2 3 5 7 11 13])
972 @result{} [2 3 5 7 11 13]
973 (aref primes 4)
974 @result{} 11
975 @end group
976 @group
977 (aref "abcdefg" 1)
978 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
979 @end group
980 @end example
981
982 See also the function @code{elt}, in @ref{Sequence Functions}.
983 @end defun
984
985 @defun aset array index object
986 This function sets the @var{index}th element of @var{array} to be
987 @var{object}. It returns @var{object}.
988
989 @example
990 @group
991 (setq w [foo bar baz])
992 @result{} [foo bar baz]
993 (aset w 0 'fu)
994 @result{} fu
995 w
996 @result{} [fu bar baz]
997 @end group
998
999 @group
1000 (setq x "asdfasfd")
1001 @result{} "asdfasfd"
1002 (aset x 3 ?Z)
1003 @result{} 90
1004 x
1005 @result{} "asdZasfd"
1006 @end group
1007 @end example
1008
1009 If @var{array} is a string and @var{object} is not a character, a
1010 @code{wrong-type-argument} error results. The function converts a
1011 unibyte string to multibyte if necessary to insert a character.
1012 @end defun
1013
1014 @defun fillarray array object
1015 This function fills the array @var{array} with @var{object}, so that
1016 each element of @var{array} is @var{object}. It returns @var{array}.
1017
1018 @example
1019 @group
1020 (setq a [a b c d e f g])
1021 @result{} [a b c d e f g]
1022 (fillarray a 0)
1023 @result{} [0 0 0 0 0 0 0]
1024 a
1025 @result{} [0 0 0 0 0 0 0]
1026 @end group
1027 @group
1028 (setq s "When in the course")
1029 @result{} "When in the course"
1030 (fillarray s ?-)
1031 @result{} "------------------"
1032 @end group
1033 @end example
1034
1035 If @var{array} is a string and @var{object} is not a character, a
1036 @code{wrong-type-argument} error results.
1037 @end defun
1038
1039 The general sequence functions @code{copy-sequence} and @code{length}
1040 are often useful for objects known to be arrays. @xref{Sequence Functions}.
1041
1042 @node Vectors
1043 @section Vectors
1044 @cindex vector (type)
1045
1046 A @dfn{vector} is a general-purpose array whose elements can be any
1047 Lisp objects. (By contrast, the elements of a string can only be
1048 characters. @xref{Strings and Characters}.) Vectors are used in
1049 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
1050 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
1051 representation of a byte-compiled function (@pxref{Byte Compilation}),
1052 and more.
1053
1054 Like other arrays, vectors use zero-origin indexing: the first
1055 element has index 0.
1056
1057 Vectors are printed with square brackets surrounding the elements.
1058 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
1059 @code{a} is printed as @code{[a b a]}. You can write vectors in the
1060 same way in Lisp input.
1061
1062 A vector, like a string or a number, is considered a constant for
1063 evaluation: the result of evaluating it is the same vector. This does
1064 not evaluate or even examine the elements of the vector.
1065 @xref{Self-Evaluating Forms}.
1066
1067 Here are examples illustrating these principles:
1068
1069 @example
1070 @group
1071 (setq avector [1 two '(three) "four" [five]])
1072 @result{} [1 two (quote (three)) "four" [five]]
1073 (eval avector)
1074 @result{} [1 two (quote (three)) "four" [five]]
1075 (eq avector (eval avector))
1076 @result{} t
1077 @end group
1078 @end example
1079
1080 @node Vector Functions
1081 @section Functions for Vectors
1082
1083 Here are some functions that relate to vectors:
1084
1085 @defun vectorp object
1086 This function returns @code{t} if @var{object} is a vector.
1087
1088 @example
1089 @group
1090 (vectorp [a])
1091 @result{} t
1092 (vectorp "asdf")
1093 @result{} nil
1094 @end group
1095 @end example
1096 @end defun
1097
1098 @defun vector &rest objects
1099 This function creates and returns a vector whose elements are the
1100 arguments, @var{objects}.
1101
1102 @example
1103 @group
1104 (vector 'foo 23 [bar baz] "rats")
1105 @result{} [foo 23 [bar baz] "rats"]
1106 (vector)
1107 @result{} []
1108 @end group
1109 @end example
1110 @end defun
1111
1112 @defun make-vector length object
1113 This function returns a new vector consisting of @var{length} elements,
1114 each initialized to @var{object}.
1115
1116 @example
1117 @group
1118 (setq sleepy (make-vector 9 'Z))
1119 @result{} [Z Z Z Z Z Z Z Z Z]
1120 @end group
1121 @end example
1122 @end defun
1123
1124 @defun vconcat &rest sequences
1125 @cindex copying vectors
1126 This function returns a new vector containing all the elements of
1127 @var{sequences}. The arguments @var{sequences} may be true lists,
1128 vectors, strings or bool-vectors. If no @var{sequences} are given,
1129 the empty vector is returned.
1130
1131 The value is either the empty vector, or is a newly constructed
1132 nonempty vector that is not @code{eq} to any existing vector.
1133
1134 @example
1135 @group
1136 (setq a (vconcat '(A B C) '(D E F)))
1137 @result{} [A B C D E F]
1138 (eq a (vconcat a))
1139 @result{} nil
1140 @end group
1141 @group
1142 (vconcat)
1143 @result{} []
1144 (vconcat [A B C] "aa" '(foo (6 7)))
1145 @result{} [A B C 97 97 foo (6 7)]
1146 @end group
1147 @end example
1148
1149 The @code{vconcat} function also allows byte-code function objects as
1150 arguments. This is a special feature to make it easy to access the entire
1151 contents of a byte-code function object. @xref{Byte-Code Objects}.
1152
1153 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
1154 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
1155 in @ref{Building Lists}.
1156 @end defun
1157
1158 The @code{append} function also provides a way to convert a vector into a
1159 list with the same elements:
1160
1161 @example
1162 @group
1163 (setq avector [1 two (quote (three)) "four" [five]])
1164 @result{} [1 two (quote (three)) "four" [five]]
1165 (append avector nil)
1166 @result{} (1 two (quote (three)) "four" [five])
1167 @end group
1168 @end example
1169
1170 @node Char-Tables
1171 @section Char-Tables
1172 @cindex char-tables
1173 @cindex extra slots of char-table
1174
1175 A char-table is much like a vector, except that it is indexed by
1176 character codes. Any valid character code, without modifiers, can be
1177 used as an index in a char-table. You can access a char-table's
1178 elements with @code{aref} and @code{aset}, as with any array. In
1179 addition, a char-table can have @dfn{extra slots} to hold additional
1180 data not associated with particular character codes. Like vectors,
1181 char-tables are constants when evaluated, and can hold elements of any
1182 type.
1183
1184 @cindex subtype of char-table
1185 Each char-table has a @dfn{subtype}, a symbol, which serves two
1186 purposes:
1187
1188 @itemize @bullet
1189 @item
1190 The subtype provides an easy way to tell what the char-table is for.
1191 For instance, display tables are char-tables with @code{display-table}
1192 as the subtype, and syntax tables are char-tables with
1193 @code{syntax-table} as the subtype. The subtype can be queried using
1194 the function @code{char-table-subtype}, described below.
1195
1196 @item
1197 The subtype controls the number of @dfn{extra slots} in the
1198 char-table. This number is specified by the subtype's
1199 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1200 Properties}), whose value should be an integer between 0 and 10. If
1201 the subtype has no such symbol property, the char-table has no extra
1202 slots.
1203 @end itemize
1204
1205 @cindex parent of char-table
1206 A char-table can have a @dfn{parent}, which is another char-table. If
1207 it does, then whenever the char-table specifies @code{nil} for a
1208 particular character @var{c}, it inherits the value specified in the
1209 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
1210 the value from the parent of @var{char-table} if @var{char-table} itself
1211 specifies @code{nil}.
1212
1213 @cindex default value of char-table
1214 A char-table can also have a @dfn{default value}. If so, then
1215 @code{(aref @var{char-table} @var{c})} returns the default value
1216 whenever the char-table does not specify any other non-@code{nil} value.
1217
1218 @defun make-char-table subtype &optional init
1219 Return a newly-created char-table, with subtype @var{subtype} (a
1220 symbol). Each element is initialized to @var{init}, which defaults to
1221 @code{nil}. You cannot alter the subtype of a char-table after the
1222 char-table is created.
1223
1224 There is no argument to specify the length of the char-table, because
1225 all char-tables have room for any valid character code as an index.
1226
1227 If @var{subtype} has the @code{char-table-extra-slots} symbol
1228 property, that specifies the number of extra slots in the char-table.
1229 This should be an integer between 0 and 10; otherwise,
1230 @code{make-char-table} raises an error. If @var{subtype} has no
1231 @code{char-table-extra-slots} symbol property (@pxref{Property
1232 Lists}), the char-table has no extra slots.
1233 @end defun
1234
1235 @defun char-table-p object
1236 This function returns @code{t} if @var{object} is a char-table, and
1237 @code{nil} otherwise.
1238 @end defun
1239
1240 @defun char-table-subtype char-table
1241 This function returns the subtype symbol of @var{char-table}.
1242 @end defun
1243
1244 There is no special function to access default values in a char-table.
1245 To do that, use @code{char-table-range} (see below).
1246
1247 @defun char-table-parent char-table
1248 This function returns the parent of @var{char-table}. The parent is
1249 always either @code{nil} or another char-table.
1250 @end defun
1251
1252 @defun set-char-table-parent char-table new-parent
1253 This function sets the parent of @var{char-table} to @var{new-parent}.
1254 @end defun
1255
1256 @defun char-table-extra-slot char-table n
1257 This function returns the contents of extra slot @var{n} of
1258 @var{char-table}. The number of extra slots in a char-table is
1259 determined by its subtype.
1260 @end defun
1261
1262 @defun set-char-table-extra-slot char-table n value
1263 This function stores @var{value} in extra slot @var{n} of
1264 @var{char-table}.
1265 @end defun
1266
1267 A char-table can specify an element value for a single character code;
1268 it can also specify a value for an entire character set.
1269
1270 @defun char-table-range char-table range
1271 This returns the value specified in @var{char-table} for a range of
1272 characters @var{range}. Here are the possibilities for @var{range}:
1273
1274 @table @asis
1275 @item @code{nil}
1276 Refers to the default value.
1277
1278 @item @var{char}
1279 Refers to the element for character @var{char}
1280 (supposing @var{char} is a valid character code).
1281
1282 @item @code{(@var{from} . @var{to})}
1283 A cons cell refers to all the characters in the inclusive range
1284 @samp{[@var{from}..@var{to}]}.
1285 @end table
1286 @end defun
1287
1288 @defun set-char-table-range char-table range value
1289 This function sets the value in @var{char-table} for a range of
1290 characters @var{range}. Here are the possibilities for @var{range}:
1291
1292 @table @asis
1293 @item @code{nil}
1294 Refers to the default value.
1295
1296 @item @code{t}
1297 Refers to the whole range of character codes.
1298
1299 @item @var{char}
1300 Refers to the element for character @var{char}
1301 (supposing @var{char} is a valid character code).
1302
1303 @item @code{(@var{from} . @var{to})}
1304 A cons cell refers to all the characters in the inclusive range
1305 @samp{[@var{from}..@var{to}]}.
1306 @end table
1307 @end defun
1308
1309 @defun map-char-table function char-table
1310 This function calls its argument @var{function} for each element of
1311 @var{char-table} that has a non-@code{nil} value. The call to
1312 @var{function} is with two arguments, a key and a value. The key
1313 is a possible @var{range} argument for @code{char-table-range}---either
1314 a valid character or a cons cell @code{(@var{from} . @var{to})},
1315 specifying a range of characters that share the same value. The value is
1316 what @code{(char-table-range @var{char-table} @var{key})} returns.
1317
1318 Overall, the key-value pairs passed to @var{function} describe all the
1319 values stored in @var{char-table}.
1320
1321 The return value is always @code{nil}; to make calls to
1322 @code{map-char-table} useful, @var{function} should have side effects.
1323 For example, here is how to examine the elements of the syntax table:
1324
1325 @example
1326 (let (accumulator)
1327 (map-char-table
1328 #'(lambda (key value)
1329 (setq accumulator
1330 (cons (list
1331 (if (consp key)
1332 (list (car key) (cdr key))
1333 key)
1334 value)
1335 accumulator)))
1336 (syntax-table))
1337 accumulator)
1338 @result{}
1339 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1340 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1341 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1342 @end example
1343 @end defun
1344
1345 @node Bool-Vectors
1346 @section Bool-vectors
1347 @cindex Bool-vectors
1348
1349 A bool-vector is much like a vector, except that it stores only the
1350 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
1351 value into an element of the bool-vector, the effect is to store
1352 @code{t} there. As with all arrays, bool-vector indices start from 0,
1353 and the length cannot be changed once the bool-vector is created.
1354 Bool-vectors are constants when evaluated.
1355
1356 Several functions work specifically with bool-vectors; aside
1357 from that, you manipulate them with same functions used for other kinds
1358 of arrays.
1359
1360 @defun make-bool-vector length initial
1361 Return a new bool-vector of @var{length} elements,
1362 each one initialized to @var{initial}.
1363 @end defun
1364
1365 @defun bool-vector &rest objects
1366 This function creates and returns a bool-vector whose elements are the
1367 arguments, @var{objects}.
1368 @end defun
1369
1370 @defun bool-vector-p object
1371 This returns @code{t} if @var{object} is a bool-vector,
1372 and @code{nil} otherwise.
1373 @end defun
1374
1375 There are also some bool-vector set operation functions, described below:
1376
1377 @defun bool-vector-exclusive-or a b &optional c
1378 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1379 If optional argument @var{c} is given, the result of this operation is
1380 stored into @var{c}. All arguments should be bool vectors of the same length.
1381 @end defun
1382
1383 @defun bool-vector-union a b &optional c
1384 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}. If
1385 optional argument @var{c} is given, the result of this operation is
1386 stored into @var{c}. All arguments should be bool vectors of the same length.
1387 @end defun
1388
1389 @defun bool-vector-intersection a b &optional c
1390 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}. If
1391 optional argument @var{c} is given, the result of this operation is
1392 stored into @var{c}. All arguments should be bool vectors of the same length.
1393 @end defun
1394
1395 @defun bool-vector-set-difference a b &optional c
1396 Return @dfn{set difference} of bool vectors @var{a} and @var{b}. If
1397 optional argument @var{c} is given, the result of this operation is
1398 stored into @var{c}. All arguments should be bool vectors of the same length.
1399 @end defun
1400
1401 @defun bool-vector-not a &optional b
1402 Return @dfn{set complement} of bool vector @var{a}. If optional
1403 argument @var{b} is given, the result of this operation is stored into
1404 @var{b}. All arguments should be bool vectors of the same length.
1405 @end defun
1406
1407 @defun bool-vector-subsetp a b
1408 Return @code{t} if every @code{t} value in @var{a} is also t in
1409 @var{b}, @code{nil} otherwise. All arguments should be bool vectors of the
1410 same length.
1411 @end defun
1412
1413 @defun bool-vector-count-consecutive a b i
1414 Return the number of consecutive elements in @var{a} equal @var{b}
1415 starting at @var{i}. @code{a} is a bool vector, @var{b} is @code{t}
1416 or @code{nil}, and @var{i} is an index into @code{a}.
1417 @end defun
1418
1419 @defun bool-vector-count-population a
1420 Return the number of elements that are @code{t} in bool vector @var{a}.
1421 @end defun
1422
1423 The printed form represents up to 8 boolean values as a single
1424 character:
1425
1426 @example
1427 @group
1428 (bool-vector t nil t nil)
1429 @result{} #&4"^E"
1430 (bool-vector)
1431 @result{} #&0""
1432 @end group
1433 @end example
1434
1435 You can use @code{vconcat} to print a bool-vector like other vectors:
1436
1437 @example
1438 @group
1439 (vconcat (bool-vector nil t nil t))
1440 @result{} [nil t nil t]
1441 @end group
1442 @end example
1443
1444 Here is another example of creating, examining, and updating a
1445 bool-vector:
1446
1447 @example
1448 (setq bv (make-bool-vector 5 t))
1449 @result{} #&5"^_"
1450 (aref bv 1)
1451 @result{} t
1452 (aset bv 3 nil)
1453 @result{} nil
1454 bv
1455 @result{} #&5"^W"
1456 @end example
1457
1458 @noindent
1459 These results make sense because the binary codes for control-_ and
1460 control-W are 11111 and 10111, respectively.
1461
1462 @node Rings
1463 @section Managing a Fixed-Size Ring of Objects
1464
1465 @cindex ring data structure
1466 A @dfn{ring} is a fixed-size data structure that supports insertion,
1467 deletion, rotation, and modulo-indexed reference and traversal. An
1468 efficient ring data structure is implemented by the @code{ring}
1469 package. It provides the functions listed in this section.
1470
1471 Note that several ``rings'' in Emacs, like the kill ring and the
1472 mark ring, are actually implemented as simple lists, @emph{not} using
1473 the @code{ring} package; thus the following functions won't work on
1474 them.
1475
1476 @defun make-ring size
1477 This returns a new ring capable of holding @var{size} objects.
1478 @var{size} should be an integer.
1479 @end defun
1480
1481 @defun ring-p object
1482 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1483 @end defun
1484
1485 @defun ring-size ring
1486 This returns the maximum capacity of the @var{ring}.
1487 @end defun
1488
1489 @defun ring-length ring
1490 This returns the number of objects that @var{ring} currently contains.
1491 The value will never exceed that returned by @code{ring-size}.
1492 @end defun
1493
1494 @defun ring-elements ring
1495 This returns a list of the objects in @var{ring}, in order, newest first.
1496 @end defun
1497
1498 @defun ring-copy ring
1499 This returns a new ring which is a copy of @var{ring}.
1500 The new ring contains the same (@code{eq}) objects as @var{ring}.
1501 @end defun
1502
1503 @defun ring-empty-p ring
1504 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1505 @end defun
1506
1507 The newest element in the ring always has index 0. Higher indices
1508 correspond to older elements. Indices are computed modulo the ring
1509 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
1510 to the next-oldest, and so forth.
1511
1512 @defun ring-ref ring index
1513 This returns the object in @var{ring} found at index @var{index}.
1514 @var{index} may be negative or greater than the ring length. If
1515 @var{ring} is empty, @code{ring-ref} signals an error.
1516 @end defun
1517
1518 @defun ring-insert ring object
1519 This inserts @var{object} into @var{ring}, making it the newest
1520 element, and returns @var{object}.
1521
1522 If the ring is full, insertion removes the oldest element to
1523 make room for the new element.
1524 @end defun
1525
1526 @defun ring-remove ring &optional index
1527 Remove an object from @var{ring}, and return that object. The
1528 argument @var{index} specifies which item to remove; if it is
1529 @code{nil}, that means to remove the oldest item. If @var{ring} is
1530 empty, @code{ring-remove} signals an error.
1531 @end defun
1532
1533 @defun ring-insert-at-beginning ring object
1534 This inserts @var{object} into @var{ring}, treating it as the oldest
1535 element. The return value is not significant.
1536
1537 If the ring is full, this function removes the newest element to make
1538 room for the inserted element.
1539 @end defun
1540
1541 @cindex fifo data structure
1542 If you are careful not to exceed the ring size, you can
1543 use the ring as a first-in-first-out queue. For example:
1544
1545 @lisp
1546 (let ((fifo (make-ring 5)))
1547 (mapc (lambda (obj) (ring-insert fifo obj))
1548 '(0 one "two"))
1549 (list (ring-remove fifo) t
1550 (ring-remove fifo) t
1551 (ring-remove fifo)))
1552 @result{} (0 t one t "two")
1553 @end lisp