sequences.texi 49 KB

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