-
Notifications
You must be signed in to change notification settings - Fork 97
/
index.html
1550 lines (1268 loc) · 209 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>集合和数据结构 · Julia中文文档</title><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-28835595-9', 'auto');
ga('send', 'pageview', {'page': location.pathname + location.search + location.hash});
</script><link rel="canonical" href="https://juliacn.github.io/JuliaZH.jl/latest/base/collections/"/><link href="https://fonts.googleapis.com/css?family=Lato|Roboto+Mono" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.0/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.0/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.0/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="../.."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../../assets/documenter.js"></script><script src="../../siteinfo.js"></script><script src="../../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-dark.css" data-theme-name="documenter-dark"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../../assets/themeswap.js"></script><link href="../../assets/julia-manual.css" rel="stylesheet" type="text/css"/></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href="../../"><img src="../../assets/logo.png" alt="Julia中文文档 logo"/></a><div class="docs-package-name"><span class="docs-autofit">Julia中文文档</span></div><form class="docs-search" action="../../search/"><input class="docs-search-query" id="documenter-search-query" name="q" type="text" placeholder="Search docs"/></form><ul class="docs-menu"><li><a class="tocitem" href="../../">主页</a></li><li><span class="tocitem">手册</span><ul><li><a class="tocitem" href="../../manual/getting-started/">入门</a></li><li><a class="tocitem" href="../../manual/variables/">变量</a></li><li><a class="tocitem" href="../../manual/integers-and-floating-point-numbers/">整数和浮点数</a></li><li><a class="tocitem" href="../../manual/mathematical-operations/">数学运算和初等函数</a></li><li><a class="tocitem" href="../../manual/complex-and-rational-numbers/">复数和有理数</a></li><li><a class="tocitem" href="../../manual/strings/">字符串</a></li><li><a class="tocitem" href="../../manual/functions/">函数</a></li><li><a class="tocitem" href="../../manual/control-flow/">流程控制</a></li><li><a class="tocitem" href="../../manual/variables-and-scoping/">变量作用域</a></li><li><a class="tocitem" href="../../manual/types/">类型</a></li><li><a class="tocitem" href="../../manual/methods/">方法</a></li><li><a class="tocitem" href="../../manual/constructors/">构造函数</a></li><li><a class="tocitem" href="../../manual/conversion-and-promotion/">类型转换和类型提升</a></li><li><a class="tocitem" href="../../manual/interfaces/">接口</a></li><li><a class="tocitem" href="../../manual/modules/">模块</a></li><li><a class="tocitem" href="../../manual/documentation/">文档</a></li><li><a class="tocitem" href="../../manual/metaprogramming/">元编程</a></li><li><a class="tocitem" href="../../manual/arrays/">多维数组</a></li><li><a class="tocitem" href="../../manual/missing/">缺失值</a></li><li><a class="tocitem" href="../../manual/networking-and-streams/">网络和流</a></li><li><a class="tocitem" href="../../manual/parallel-computing/">并行计算</a></li><li><a class="tocitem" href="../../manual/asynchronous-programming/">异步编程</a></li><li><a class="tocitem" href="../../manual/multi-threading/">多线程</a></li><li><a class="tocitem" href="../../manual/distributed-computing/">多进程和分布式计算</a></li><li><a class="tocitem" href="../../manual/running-external-programs/">运行外部程序</a></li><li><a class="tocitem" href="../../manual/calling-c-and-fortran-code/">调用 C 和 Fortran 代码</a></li><li><a class="tocitem" href="../../manual/handling-operating-system-variation/">处理操作系统差异</a></li><li><a class="tocitem" href="../../manual/environment-variables/">环境变量</a></li><li><a class="tocitem" href="../../manual/embedding/">嵌入 Julia</a></li><li><a class="tocitem" href="../../manual/code-loading/">代码加载</a></li><li><a class="tocitem" href="../../manual/profile/">性能分析</a></li><li><a class="tocitem" href="../../manual/stacktraces/">栈跟踪</a></li><li><a class="tocitem" href="../../manual/performance-tips/">性能建议</a></li><li><a class="tocitem" href="../../manual/workflow-tips/">工作流程建议</a></li><li><a class="tocitem" href="../../manual/style-guide/">代码风格指南</a></li><li><a class="tocitem" href="../../manual/faq/">常见问题</a></li><li><a class="tocitem" href="../../manual/noteworthy-differences/">与其他语言的显著差异</a></li><li><a class="tocitem" href="../../manual/unicode-input/">Unicode 输入表</a></li><li><a class="tocitem" href="../../manual/command-line-options/">命令行选项</a></li></ul></li><li><span class="tocitem">Base</span><ul><li><a class="tocitem" href="../base/">基本功能</a></li><li class="is-active"><a class="tocitem" href>集合和数据结构</a><ul class="internal"><li><a class="tocitem" href="#lib-collections-iteration"><span>迭代</span></a></li><li><a class="tocitem" href="#构造函数和类型"><span>构造函数和类型</span></a></li><li><a class="tocitem" href="#通用集合"><span>通用集合</span></a></li><li><a class="tocitem" href="#可迭代集合"><span>可迭代集合</span></a></li><li><a class="tocitem" href="#可索引集合"><span>可索引集合</span></a></li><li><a class="tocitem" href="#字典"><span>字典</span></a></li><li><a class="tocitem" href="#类似-Set-的集合"><span>类似 Set 的集合</span></a></li><li><a class="tocitem" href="#双端队列"><span>双端队列</span></a></li><li><a class="tocitem" href="#集合相关的实用工具"><span>集合相关的实用工具</span></a></li></ul></li><li><a class="tocitem" href="../math/">数学相关</a></li><li><a class="tocitem" href="../numbers/">Numbers</a></li><li><a class="tocitem" href="../strings/">字符串</a></li><li><a class="tocitem" href="../arrays/">数组</a></li><li><a class="tocitem" href="../parallel/">Tasks</a></li><li><a class="tocitem" href="../multi-threading/">Multi-Threading</a></li><li><a class="tocitem" href="../constants/">常量</a></li><li><a class="tocitem" href="../file/">文件系统</a></li><li><a class="tocitem" href="../io-network/">I/O 与网络</a></li><li><a class="tocitem" href="../punctuation/">运算符与记号</a></li><li><a class="tocitem" href="../sort/">排序及相关函数</a></li><li><a class="tocitem" href="../iterators/">迭代相关</a></li><li><a class="tocitem" href="../c/">C 接口</a></li><li><a class="tocitem" href="../libc/">C 标准库</a></li><li><a class="tocitem" href="../stacktraces/">堆栈跟踪</a></li><li><a class="tocitem" href="../simd-types/">SIMD 支持</a></li></ul></li><li><span class="tocitem">Standard Library</span><ul><li><a class="tocitem" href="../../stdlib/Artifacts/">Artifacts</a></li><li><a class="tocitem" href="../../stdlib/Base64/">Base64</a></li><li><a class="tocitem" href="../../stdlib/CRC32c/">CRC32c</a></li><li><a class="tocitem" href="../../stdlib/Dates/">日期</a></li><li><a class="tocitem" href="../../stdlib/DelimitedFiles/">分隔符文件</a></li><li><a class="tocitem" href="../../stdlib/Distributed/">Distributed Computing</a></li><li><a class="tocitem" href="../../stdlib/FileWatching/">文件相关事件</a></li><li><a class="tocitem" href="../../stdlib/Future/">Future</a></li><li><a class="tocitem" href="../../stdlib/InteractiveUtils/">Interactive Utilities</a></li><li><a class="tocitem" href="../../stdlib/LazyArtifacts/">Lazy Artifacts</a></li><li><a class="tocitem" href="../../stdlib/LibGit2/">LibGit2</a></li><li><a class="tocitem" href="../../stdlib/Libdl/">动态链接器</a></li><li><a class="tocitem" href="../../stdlib/LinearAlgebra/">Linear Algebra</a></li><li><a class="tocitem" href="../../stdlib/Logging/">日志记录</a></li><li><a class="tocitem" href="../../stdlib/Markdown/">Markdown</a></li><li><a class="tocitem" href="../../stdlib/Mmap/">内存映射 I/O</a></li><li><a class="tocitem" href="../../stdlib/Printf/">Printf</a></li><li><a class="tocitem" href="../../stdlib/Profile/">性能分析</a></li><li><a class="tocitem" href="../../stdlib/REPL/">Julia REPL</a></li><li><a class="tocitem" href="../../stdlib/Random/">随机数</a></li><li><a class="tocitem" href="../../stdlib/SHA/">SHA</a></li><li><a class="tocitem" href="../../stdlib/Serialization/">序列化</a></li><li><a class="tocitem" href="../../stdlib/SharedArrays/">共享数组</a></li><li><a class="tocitem" href="../../stdlib/Sockets/">套接字</a></li><li><a class="tocitem" href="../../stdlib/SparseArrays/">稀疏数组</a></li><li><a class="tocitem" href="../../stdlib/Statistics/">统计</a></li><li><a class="tocitem" href="../../stdlib/TOML/">TOML</a></li><li><a class="tocitem" href="../../stdlib/Test/">单元测试</a></li><li><a class="tocitem" href="../../stdlib/UUIDs/">UUIDs</a></li><li><a class="tocitem" href="../../stdlib/Unicode/">Unicode</a></li></ul></li><li><span class="tocitem">Developer Documentation</span><ul><li><a class="tocitem" href="../../devdocs/reflection/">反射 与 自我检查</a></li><li><input class="collapse-toggle" id="menuitem-5-2" type="checkbox"/><label class="tocitem" for="menuitem-5-2"><span class="docs-label">Documentation of Julia's Internals</span><i class="docs-chevron"></i></label><ul class="collapsed"><li><a class="tocitem" href="../../devdocs/init/">Julia 运行时的初始化</a></li><li><a class="tocitem" href="../../devdocs/ast/">Julia 的 AST</a></li><li><a class="tocitem" href="../../devdocs/types/">More about types</a></li><li><a class="tocitem" href="../../devdocs/object/">Memory layout of Julia Objects</a></li><li><a class="tocitem" href="../../devdocs/eval/">Julia 代码的 eval</a></li><li><a class="tocitem" href="../../devdocs/callconv/">Calling Conventions</a></li><li><a class="tocitem" href="../../devdocs/compiler/">本机代码生成过程的高级概述</a></li><li><a class="tocitem" href="../../devdocs/functions/">Julia 函数</a></li><li><a class="tocitem" href="../../devdocs/cartesian/">笛卡尔</a></li><li><a class="tocitem" href="../../devdocs/meta/">Talking to the compiler (the <code>:meta</code> mechanism)</a></li><li><a class="tocitem" href="../../devdocs/subarrays/">子数组</a></li><li><a class="tocitem" href="../../devdocs/isbitsunionarrays/">isbits Union Optimizations</a></li><li><a class="tocitem" href="../../devdocs/sysimg/">System Image Building</a></li><li><a class="tocitem" href="../../devdocs/llvm/">Working with LLVM</a></li><li><a class="tocitem" href="../../devdocs/stdio/">printf() and stdio in the Julia runtime</a></li><li><a class="tocitem" href="../../devdocs/boundscheck/">边界检查</a></li><li><a class="tocitem" href="../../devdocs/locks/">Proper maintenance and care of multi-threading locks</a></li><li><a class="tocitem" href="../../devdocs/offset-arrays/">Arrays with custom indices</a></li><li><a class="tocitem" href="../../devdocs/require/">Module loading</a></li><li><a class="tocitem" href="../../devdocs/inference/">类型推导</a></li><li><a class="tocitem" href="../../devdocs/ssair/">Julia SSA-form IR</a></li><li><a class="tocitem" href="../../devdocs/gc-sa/">Static analyzer annotations for GC correctness in C code</a></li></ul></li><li><input class="collapse-toggle" id="menuitem-5-3" type="checkbox"/><label class="tocitem" for="menuitem-5-3"><span class="docs-label">Developing/debugging Julia's C code</span><i class="docs-chevron"></i></label><ul class="collapsed"><li><a class="tocitem" href="../../devdocs/backtraces/">报告和分析崩溃(段错误)</a></li><li><a class="tocitem" href="../../devdocs/debuggingtips/">gdb 调试提示</a></li><li><a class="tocitem" href="../../devdocs/valgrind/">在Julia中使用Valgrind</a></li><li><a class="tocitem" href="../../devdocs/sanitizers/">Sanitizer support</a></li></ul></li></ul></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><nav class="breadcrumb"><ul class="is-hidden-mobile"><li><a class="is-disabled">Base</a></li><li class="is-active"><a href>集合和数据结构</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>集合和数据结构</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://www.transifex.com/juliacn/base-zh_cn/translate/#zh_CN/collectionsmd" title=" 完善 Transifex 上的翻译"><span class="docs-icon fab"></span><span class="docs-label is-hidden-touch"> 完善 Transifex 上的翻译</span></a><a class="docs-settings-button fas fa-cog" id="documenter-settings-button" href="#" title="设置"></a><a class="docs-sidebar-button fa fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a></div></header><article class="content" id="documenter-page"><h1 id="集合和数据结构"><a class="docs-heading-anchor" href="#集合和数据结构">集合和数据结构</a><a id="集合和数据结构-1"></a><a class="docs-heading-anchor-permalink" href="#集合和数据结构" title="Permalink"></a></h1><h2 id="lib-collections-iteration"><a class="docs-heading-anchor" href="#lib-collections-iteration">迭代</a><a id="lib-collections-iteration-1"></a><a class="docs-heading-anchor-permalink" href="#lib-collections-iteration" title="Permalink"></a></h2><p>序列迭代由 <a href="#Base.iterate"><code>iterate</code></a> 实现 广义的 <code>for</code> 循环</p><pre><code class="language-julia">for i in iter # or "for i = iter"
# body
end</code></pre><p>被转换成</p><pre><code class="language-julia">next = iterate(iter)
while next !== nothing
(i, state) = next
# body
next = iterate(iter, state)
end</code></pre><p><code>state</code> 对象可以是任何对象,并且对于每个可迭代类型应该选择合适的 <code>state</code> 对象。 请参照 <a href="../../manual/interfaces/#man-interface-iteration">帮助文档接口的迭代小节</a> 来获取关于定义一个常见迭代类型的更多细节。</p><article class="docstring"><header><a class="docstring-binding" id="Base.iterate" href="#Base.iterate"><code>Base.iterate</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">iterate(iter [, state]) -> Union{Nothing, Tuple{Any, Any}}</code></pre><p>Advance the iterator to obtain the next element. If no elements remain, <code>nothing</code> should be returned. Otherwise, a 2-tuple of the next element and the new iteration state should be returned.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/essentials.jl#L897-L903">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.IteratorSize" href="#Base.IteratorSize"><code>Base.IteratorSize</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">IteratorSize(itertype::Type) -> IteratorSize</code></pre><p>Given the type of an iterator, return one of the following values:</p><ul><li><code>SizeUnknown()</code> if the length (number of elements) cannot be determined in advance.</li><li><code>HasLength()</code> if there is a fixed, finite length.</li><li><code>HasShape{N}()</code> if there is a known length plus a notion of multidimensional shape (as for an array). In this case <code>N</code> should give the number of dimensions, and the <a href="../arrays/#Base.axes-Tuple{Any}"><code>axes</code></a> function is valid for the iterator.</li><li><code>IsInfinite()</code> if the iterator yields values forever.</li></ul><p>The default value (for iterators that do not define this function) is <code>HasLength()</code>. This means that most iterators are assumed to implement <a href="#Base.length"><code>length</code></a>.</p><p>This trait is generally used to select between algorithms that pre-allocate space for their result, and algorithms that resize their result incrementally.</p><pre><code class="language-julia-repl">julia> Base.IteratorSize(1:5)
Base.HasShape{1}()
julia> Base.IteratorSize((2,3))
Base.HasLength()</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/generator.jl#L66-L91">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.IteratorEltype" href="#Base.IteratorEltype"><code>Base.IteratorEltype</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">IteratorEltype(itertype::Type) -> IteratorEltype</code></pre><p>Given the type of an iterator, return one of the following values:</p><ul><li><code>EltypeUnknown()</code> if the type of elements yielded by the iterator is not known in advance.</li><li><code>HasEltype()</code> if the element type is known, and <a href="#Base.eltype"><code>eltype</code></a> would return a meaningful value.</li></ul><p><code>HasEltype()</code> is the default, since iterators are assumed to implement <a href="#Base.eltype"><code>eltype</code></a>.</p><p>This trait is generally used to select between algorithms that pre-allocate a specific type of result, and algorithms that pick a result type based on the types of yielded values.</p><pre><code class="language-julia-repl">julia> Base.IteratorEltype(1:5)
Base.HasEltype()</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/generator.jl#L107-L125">source</a></section></article><p>以下类型均完全实现了上述函数:</p><ul><li><a href="#Base.AbstractRange"><code>AbstractRange</code></a></li><li><a href="#Base.UnitRange"><code>UnitRange</code></a></li><li><code>Tuple</code></li><li><code>Number</code></li><li><a href="../arrays/#Core.AbstractArray"><code>AbstractArray</code></a></li><li><a href="#Base.BitSet"><code>BitSet</code></a></li><li><a href="#Base.IdDict"><code>IdDict</code></a></li><li><a href="#Base.Dict"><code>Dict</code></a></li><li><a href="#Base.WeakKeyDict"><code>WeakKeyDict</code></a></li><li><code>EachLine</code></li><li><code>AbstractString</code></li><li><a href="#Base.Set"><code>Set</code></a></li><li><a href="#Core.Pair"><code>Pair</code></a></li><li><a href="../base/#Core.NamedTuple"><code>NamedTuple</code></a></li></ul><h2 id="构造函数和类型"><a class="docs-heading-anchor" href="#构造函数和类型">构造函数和类型</a><a id="构造函数和类型-1"></a><a class="docs-heading-anchor-permalink" href="#构造函数和类型" title="Permalink"></a></h2><article class="docstring"><header><a class="docstring-binding" id="Base.AbstractRange" href="#Base.AbstractRange"><code>Base.AbstractRange</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">AbstractRange{T}</code></pre><p>Supertype for ranges with elements of type <code>T</code>. <a href="#Base.UnitRange"><code>UnitRange</code></a> and other types are subtypes of this.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L245-L250">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.OrdinalRange" href="#Base.OrdinalRange"><code>Base.OrdinalRange</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">OrdinalRange{T, S} <: AbstractRange{T}</code></pre><p>Supertype for ordinal ranges with elements of type <code>T</code> with spacing(s) of type <code>S</code>. The steps should be always-exact multiples of <a href="../numbers/#Base.oneunit"><code>oneunit</code></a>, and <code>T</code> should be a "discrete" type, which cannot have values smaller than <code>oneunit</code>. For example, <code>Integer</code> or <code>Date</code> types would qualify, whereas <code>Float64</code> would not (since this type can represent values smaller than <code>oneunit(Float64)</code>. <a href="#Base.UnitRange"><code>UnitRange</code></a>, <a href="#Base.StepRange"><code>StepRange</code></a>, and other types are subtypes of this.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L260-L270">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.AbstractUnitRange" href="#Base.AbstractUnitRange"><code>Base.AbstractUnitRange</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">AbstractUnitRange{T} <: OrdinalRange{T, T}</code></pre><p>Supertype for ranges with a step size of <a href="../numbers/#Base.oneunit"><code>oneunit(T)</code></a> with elements of type <code>T</code>. <a href="#Base.UnitRange"><code>UnitRange</code></a> and other types are subtypes of this.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L273-L278">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.StepRange" href="#Base.StepRange"><code>Base.StepRange</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">StepRange{T, S} <: OrdinalRange{T, S}</code></pre><p>Ranges with elements of type <code>T</code> with spacing of type <code>S</code>. The step between each element is constant, and the range is defined in terms of a <code>start</code> and <code>stop</code> of type <code>T</code> and a <code>step</code> of type <code>S</code>. Neither <code>T</code> nor <code>S</code> should be floating point types. The syntax <code>a:b:c</code> with <code>b > 1</code> and <code>a</code>, <code>b</code>, and <code>c</code> all integers creates a <code>StepRange</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> collect(StepRange(1, Int8(2), 10))
5-element Vector{Int64}:
1
3
5
7
9
julia> typeof(StepRange(1, Int8(2), 10))
StepRange{Int64, Int8}
julia> typeof(1:3:6)
StepRange{Int64, Int64}</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L281-L306">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.UnitRange" href="#Base.UnitRange"><code>Base.UnitRange</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">UnitRange{T<:Real}</code></pre><p>A range parameterized by a <code>start</code> and <code>stop</code> of type <code>T</code>, filled with elements spaced by <code>1</code> from <code>start</code> until <code>stop</code> is exceeded. The syntax <code>a:b</code> with <code>a</code> and <code>b</code> both <code>Integer</code>s creates a <code>UnitRange</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> collect(UnitRange(2.3, 5.2))
3-element Vector{Float64}:
2.3
3.3
4.3
julia> typeof(1:10)
UnitRange{Int64}</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L371-L389">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.LinRange" href="#Base.LinRange"><code>Base.LinRange</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">LinRange{T,L}</code></pre><p>A range with <code>len</code> linearly spaced elements between its <code>start</code> and <code>stop</code>. The size of the spacing is controlled by <code>len</code>, which must be an <code>Integer</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> LinRange(1.5, 5.5, 9)
9-element LinRange{Float64, Int64}:
1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5</code></pre><p>Compared to using <a href="../math/#Base.range"><code>range</code></a>, directly constructing a <code>LinRange</code> should have less overhead but won't try to correct for floating point errors:</p><pre><code class="language-julia-repl">julia> collect(range(-0.1, 0.3, length=5))
5-element Vector{Float64}:
-0.1
0.0
0.1
0.2
0.3
julia> collect(LinRange(-0.1, 0.3, 5))
5-element Vector{Float64}:
-0.1
-1.3877787807814457e-17
0.09999999999999999
0.19999999999999998
0.3</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L500-L533">source</a></section></article><h2 id="通用集合"><a class="docs-heading-anchor" href="#通用集合">通用集合</a><a id="通用集合-1"></a><a class="docs-heading-anchor-permalink" href="#通用集合" title="Permalink"></a></h2><article class="docstring"><header><a class="docstring-binding" id="Base.isempty" href="#Base.isempty"><code>Base.isempty</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">isempty(collection) -> Bool</code></pre><p>Determine whether a collection is empty (has no elements).</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> isempty([])
true
julia> isempty([1 2 3])
false</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/essentials.jl#L771-L784">source</a></section><section><div><pre><code class="language-none">isempty(condition)</code></pre><p>Return <code>true</code> if no tasks are waiting on the condition, <code>false</code> otherwise.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/condition.jl#L159-L163">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.empty!" href="#Base.empty!"><code>Base.empty!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">empty!(collection) -> collection</code></pre><p>Remove all elements from a <code>collection</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = Dict("a" => 1, "b" => 2)
Dict{String, Int64} with 2 entries:
"b" => 2
"a" => 1
julia> empty!(A);
julia> A
Dict{String, Int64}()</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/dict.jl#L247-L264">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.length" href="#Base.length"><code>Base.length</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">length(collection) -> Integer</code></pre><p>Return the number of elements in the collection.</p><p>Use <a href="#Base.lastindex"><code>lastindex</code></a> to get the last valid index of an indexable collection.</p><p>See also: <a href="../arrays/#Base.size"><code>size</code></a>, <a href="../arrays/#Base.ndims"><code>ndims</code></a>, <a href="../arrays/#Base.eachindex"><code>eachindex</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> length(1:5)
5
julia> length([1, 2, 3, 4])
4
julia> length([1 2; 3 4])
4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L242-L262">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.checked_length" href="#Base.checked_length"><code>Base.checked_length</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">Base.checked_length(r)</code></pre><p>Calculates <code>length(r)</code>, but may check for overflow errors where applicable when the result doesn't fit into <code>Union{Integer(eltype(r)),Int}</code>.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/checked.jl#L353-L358">source</a></section></article><p>以下类型均完全实现了上述函数:</p><ul><li><a href="#Base.AbstractRange"><code>AbstractRange</code></a></li><li><a href="#Base.UnitRange"><code>UnitRange</code></a></li><li><code>Tuple</code></li><li><code>Number</code></li><li><a href="../arrays/#Core.AbstractArray"><code>AbstractArray</code></a></li><li><a href="#Base.BitSet"><code>BitSet</code></a></li><li><a href="#Base.IdDict"><code>IdDict</code></a></li><li><a href="#Base.Dict"><code>Dict</code></a></li><li><a href="#Base.WeakKeyDict"><code>WeakKeyDict</code></a></li><li><code>AbstractString</code></li><li><a href="#Base.Set"><code>Set</code></a></li><li><a href="../base/#Core.NamedTuple"><code>NamedTuple</code></a></li></ul><h2 id="可迭代集合"><a class="docs-heading-anchor" href="#可迭代集合">可迭代集合</a><a id="可迭代集合-1"></a><a class="docs-heading-anchor-permalink" href="#可迭代集合" title="Permalink"></a></h2><article class="docstring"><header><a class="docstring-binding" id="Base.in" href="#Base.in"><code>Base.in</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">in(item, collection) -> Bool
∈(item, collection) -> Bool</code></pre><p>Determine whether an item is in the given collection, in the sense that it is <a href="../math/#Base.:=="><code>==</code></a> to one of the values generated by iterating over the collection. Returns a <code>Bool</code> value, except if <code>item</code> is <a href="../base/#Base.missing"><code>missing</code></a> or <code>collection</code> contains <code>missing</code> but not <code>item</code>, in which case <code>missing</code> is returned (<a href="https://en.wikipedia.org/wiki/Three-valued_logic">three-valued logic</a>, matching the behavior of <a href="#Base.any-Tuple{Any}"><code>any</code></a> and <a href="../math/#Base.:=="><code>==</code></a>).</p><p>Some collections follow a slightly different definition. For example, <a href="#Base.Set"><code>Set</code></a>s check whether the item <a href="../base/#Base.isequal"><code>isequal</code></a> to one of the elements. <a href="#Base.Dict"><code>Dict</code></a>s look for <code>key=>value</code> pairs, and the key is compared using <a href="../base/#Base.isequal"><code>isequal</code></a>. To test for the presence of a key in a dictionary, use <a href="#Base.haskey"><code>haskey</code></a> or <code>k in keys(dict)</code>. For these collections, the result is always a <code>Bool</code> and never <code>missing</code>.</p><p>To determine whether an item is not in a given collection, see <a href="#Base.:∉"><code>:∉</code></a>. You may also negate the <code>in</code> by doing <code>!(a in b)</code> which is logically similar to "not in".</p><p>When broadcasting with <code>in.(items, collection)</code> or <code>items .∈ collection</code>, both <code>item</code> and <code>collection</code> are broadcasted over, which is often not what is intended. For example, if both arguments are vectors (and the dimensions match), the result is a vector indicating whether each value in collection <code>items</code> is <code>in</code> the value at the corresponding position in <code>collection</code>. To get a vector indicating whether each value in <code>items</code> is in <code>collection</code>, wrap <code>collection</code> in a tuple or a <code>Ref</code> like this: <code>in.(items, Ref(collection))</code> or <code>items .∈ Ref(collection)</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = 1:3:20
1:3:19
julia> 4 in a
true
julia> 5 in a
false
julia> missing in [1, 2]
missing
julia> 1 in [2, missing]
missing
julia> 1 in [1, missing]
true
julia> missing in Set([1, 2])
false
julia> !(21 in a)
true
julia> !(19 in a)
false
julia> [1, 2] .∈ [2, 3]
2-element BitVector:
0
0
julia> [1, 2] .∈ ([2, 3],)
2-element BitVector:
0
1</code></pre><p>See also: <a href="../sort/#Base.Sort.insorted"><code>insorted</code></a>, <a href="../strings/#Base.contains"><code>contains</code></a>, <a href="../strings/#Base.occursin"><code>occursin</code></a>, <a href="#Base.issubset"><code>issubset</code></a>.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/operators.jl#L1279-L1349">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.:∉" href="#Base.:∉"><code>Base.:∉</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">∉(item, collection) -> Bool
∌(collection, item) -> Bool</code></pre><p>Negation of <code>∈</code> and <code>∋</code>, i.e. checks that <code>item</code> is not in <code>collection</code>.</p><p>When broadcasting with <code>items .∉ collection</code>, both <code>item</code> and <code>collection</code> are broadcasted over, which is often not what is intended. For example, if both arguments are vectors (and the dimensions match), the result is a vector indicating whether each value in collection <code>items</code> is not in the value at the corresponding position in <code>collection</code>. To get a vector indicating whether each value in <code>items</code> is not in <code>collection</code>, wrap <code>collection</code> in a tuple or a <code>Ref</code> like this: <code>items .∉ Ref(collection)</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> 1 ∉ 2:4
true
julia> 1 ∉ 1:3
false
julia> [1, 2] .∉ [2, 3]
2-element BitVector:
1
1
julia> [1, 2] .∉ ([2, 3],)
2-element BitVector:
1
0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/operators.jl#L1352-L1384">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.eltype" href="#Base.eltype"><code>Base.eltype</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">eltype(type)</code></pre><p>Determine the type of the elements generated by iterating a collection of the given <code>type</code>. For dictionary types, this will be a <code>Pair{KeyType,ValType}</code>. The definition <code>eltype(x) = eltype(typeof(x))</code> is provided for convenience so that instances can be passed instead of types. However the form that accepts a type argument should be defined for new types.</p><p>See also: <a href="#Base.keytype"><code>keytype</code></a>, <a href="../base/#Core.typeof"><code>typeof</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> eltype(fill(1f0, (2,2)))
Float32
julia> eltype(fill(0x1, (2,2)))
UInt8</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L184-L203">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.indexin" href="#Base.indexin"><code>Base.indexin</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">indexin(a, b)</code></pre><p>Return an array containing the first index in <code>b</code> for each value in <code>a</code> that is a member of <code>b</code>. The output array contains <code>nothing</code> wherever <code>a</code> is not a member of <code>b</code>.</p><p>See also: <a href="../sort/#Base.sortperm"><code>sortperm</code></a>, <a href="../arrays/#Base.findfirst-Tuple{Any}"><code>findfirst</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];
julia> b = ['a', 'b', 'c'];
julia> indexin(a, b)
6-element Vector{Union{Nothing, Int64}}:
1
2
3
2
nothing
1
julia> indexin(b, a)
3-element Vector{Union{Nothing, Int64}}:
1
2
3</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/array.jl#L2376-L2406">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.unique" href="#Base.unique"><code>Base.unique</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">unique(itr)</code></pre><p>Return an array containing only the unique elements of collection <code>itr</code>, as determined by <a href="../base/#Base.isequal"><code>isequal</code></a>, in the order that the first of each set of equivalent elements originally appears. The element type of the input is preserved.</p><p>See also: <a href="#Base.unique!"><code>unique!</code></a>, <a href="#Base.allunique"><code>allunique</code></a>, <a href="base/@ref"><code>allequal</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> unique([1, 2, 6, 2])
3-element Vector{Int64}:
1
2
6
julia> unique(Real[1, 1.0, 2])
2-element Vector{Real}:
1
2</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L110-L133">source</a></section><section><div><pre><code class="language-none">unique(f, itr)</code></pre><p>Returns an array containing one value from <code>itr</code> for each unique value produced by <code>f</code> applied to elements of <code>itr</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> unique(x -> x^2, [1, -1, 3, -3, 4])
3-element Vector{Int64}:
1
3
4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L183-L197">source</a></section><section><div><pre><code class="language-none">unique(A::AbstractArray; dims::Int)</code></pre><p>Return unique regions of <code>A</code> along dimension <code>dims</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = map(isodd, reshape(Vector(1:8), (2,2,2)))
2×2×2 Array{Bool, 3}:
[:, :, 1] =
1 1
0 0
[:, :, 2] =
1 1
0 0
julia> unique(A)
2-element Vector{Bool}:
1
0
julia> unique(A, dims=2)
2×1×2 Array{Bool, 3}:
[:, :, 1] =
1
0
[:, :, 2] =
1
0
julia> unique(A, dims=3)
2×2×1 Array{Bool, 3}:
[:, :, 1] =
1 1
0 0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/multidimensional.jl#L1612-L1650">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.unique!" href="#Base.unique!"><code>Base.unique!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">unique!(f, A::AbstractVector)</code></pre><p>Selects one value from <code>A</code> for each unique value produced by <code>f</code> applied to elements of <code>A</code>, then return the modified A.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.1</header><div class="admonition-body"><p>This method is available as of Julia 1.1.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> unique!(x -> x^2, [1, -1, 3, -3, 4])
3-element Vector{Int64}:
1
3
4
julia> unique!(n -> n%3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6])
3-element Vector{Int64}:
5
1
9
julia> unique!(iseven, [2, 3, 5, 7, 9])
2-element Vector{Int64}:
2
3</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L245-L273">source</a></section><section><div><pre><code class="language-none">unique!(A::AbstractVector)</code></pre><p>Remove duplicate items as determined by <a href="../base/#Base.isequal"><code>isequal</code></a>, then return the modified <code>A</code>. <code>unique!</code> will return the elements of <code>A</code> in the order that they occur. If you do not care about the order of the returned data, then calling <code>(sort!(A); unique!(A))</code> will be much more efficient as long as the elements of <code>A</code> can be sorted.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> unique!([1, 1, 1])
1-element Vector{Int64}:
1
julia> A = [7, 3, 2, 3, 7, 5];
julia> unique!(A)
4-element Vector{Int64}:
7
3
2
5
julia> B = [7, 6, 42, 6, 7, 42];
julia> sort!(B); # unique! is able to process sorted data much more efficiently.
julia> unique!(B)
3-element Vector{Int64}:
6
7
42</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L337-L370">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.allunique" href="#Base.allunique"><code>Base.allunique</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">allunique(itr) -> Bool</code></pre><p>Return <code>true</code> if all values from <code>itr</code> are distinct when compared with <a href="../base/#Base.isequal"><code>isequal</code></a>.</p><p>See also: <a href="#Base.unique"><code>unique</code></a>, <a href="../sort/#Base.issorted"><code>issorted</code></a>, <a href="base/@ref"><code>allequal</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = [1; 2; 3]
3-element Vector{Int64}:
1
2
3
julia> allunique(a)
true
julia> allunique([a, a])
false</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L381-L402">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.reduce-Tuple{Any, Any}" href="#Base.reduce-Tuple{Any, Any}"><code>Base.reduce</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">reduce(op, itr; [init])</code></pre><p>Reduce the given collection <code>itr</code> with the given binary operator <code>op</code>. If provided, the initial value <code>init</code> must be a neutral element for <code>op</code> that will be returned for empty collections. It is unspecified whether <code>init</code> is used for non-empty collections.</p><p>For empty collections, providing <code>init</code> will be necessary, except for some special cases (e.g. when <code>op</code> is one of <code>+</code>, <code>*</code>, <code>max</code>, <code>min</code>, <code>&</code>, <code>|</code>) when Julia can determine the neutral element of <code>op</code>.</p><p>Reductions for certain commonly-used operators may have special implementations, and should be used instead: <code>maximum(itr)</code>, <code>minimum(itr)</code>, <code>sum(itr)</code>, <code>prod(itr)</code>, <code>any(itr)</code>, <code>all(itr)</code>.</p><p>The associativity of the reduction is implementation dependent. This means that you can't use non-associative operations like <code>-</code> because it is undefined whether <code>reduce(-,[1,2,3])</code> should be evaluated as <code>(1-2)-3</code> or <code>1-(2-3)</code>. Use <a href="#Base.foldl-Tuple{Any, Any}"><code>foldl</code></a> or <a href="#Base.foldr-Tuple{Any, Any}"><code>foldr</code></a> instead for guaranteed left or right associativity.</p><p>Some operations accumulate error. Parallelism will be easier if the reduction can be executed in groups. Future versions of Julia might change the algorithm. Note that the elements are not reordered if you use an ordered collection.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> reduce(*, [2; 3; 4])
24
julia> reduce(*, [2; 3; 4]; init=-1)
-24</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L450-L482">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.foldl-Tuple{Any, Any}" href="#Base.foldl-Tuple{Any, Any}"><code>Base.foldl</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">foldl(op, itr; [init])</code></pre><p>Like <a href="#Base.reduce-Tuple{Any, Any}"><code>reduce</code></a>, but with guaranteed left associativity. If provided, the keyword argument <code>init</code> will be used exactly once. In general, it will be necessary to provide <code>init</code> to work with empty collections.</p><p>See also <a href="#Base.mapfoldl-Tuple{Any, Any, Any}"><code>mapfoldl</code></a>, <a href="#Base.foldr-Tuple{Any, Any}"><code>foldr</code></a>, <a href="../arrays/#Base.accumulate"><code>accumulate</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> foldl(=>, 1:4)
((1 => 2) => 3) => 4
julia> foldl(=>, 1:4; init=0)
(((0 => 1) => 2) => 3) => 4
julia> accumulate(=>, (1,2,3,4))
(1, 1 => 2, (1 => 2) => 3, ((1 => 2) => 3) => 4)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L172-L192">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.foldr-Tuple{Any, Any}" href="#Base.foldr-Tuple{Any, Any}"><code>Base.foldr</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">foldr(op, itr; [init])</code></pre><p>Like <a href="#Base.reduce-Tuple{Any, Any}"><code>reduce</code></a>, but with guaranteed right associativity. If provided, the keyword argument <code>init</code> will be used exactly once. In general, it will be necessary to provide <code>init</code> to work with empty collections.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> foldr(=>, 1:4)
1 => (2 => (3 => 4))
julia> foldr(=>, 1:4; init=0)
1 => (2 => (3 => (4 => 0)))</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L221-L236">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.maximum" href="#Base.maximum"><code>Base.maximum</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">maximum(f, itr; [init])</code></pre><p>Returns the largest result of calling function <code>f</code> on each element of <code>itr</code>.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be a neutral element for <code>max</code> (i.e. which is less than or equal to any other element) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> maximum(length, ["Julion", "Julia", "Jule"])
6
julia> maximum(length, []; init=-1)
-1
julia> maximum(sin, Real[]; init=-1.0) # good, since output of sin is >= -1
-1.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L673-L697">source</a></section><section><div><pre><code class="language-none">maximum(itr; [init])</code></pre><p>Returns the largest element in a collection.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be a neutral element for <code>max</code> (i.e. which is less than or equal to any other element) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> maximum(-20.5:10)
9.5
julia> maximum([1,2,3])
3
julia> maximum(())
ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
[...]
julia> maximum((); init=-Inf)
-Inf</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L727-L756">source</a></section><section><div><pre><code class="language-none">maximum(A::AbstractArray; dims)</code></pre><p>Compute the maximum value of an array over the given dimensions. See also the <a href="../math/#Base.max"><code>max(a,b)</code></a> function to take the maximum of two or more arguments, which can be applied elementwise to arrays via <code>max.(a,b)</code>.</p><p>See also: <a href="#Base.maximum!"><code>maximum!</code></a>, <a href="#Base.extrema"><code>extrema</code></a>, <a href="#Base.findmax"><code>findmax</code></a>, <a href="#Base.argmax"><code>argmax</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> maximum(A, dims=1)
1×2 Matrix{Int64}:
3 4
julia> maximum(A, dims=2)
2×1 Matrix{Int64}:
2
4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L621-L646">source</a></section><section><div><pre><code class="language-none">maximum(f, A::AbstractArray; dims)</code></pre><p>Compute the maximum value by calling the function <code>f</code> on each element of an array over the given dimensions.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> maximum(abs2, A, dims=1)
1×2 Matrix{Int64}:
9 16
julia> maximum(abs2, A, dims=2)
2×1 Matrix{Int64}:
4
16</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L649-L671">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.maximum!" href="#Base.maximum!"><code>Base.maximum!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">maximum!(r, A)</code></pre><p>Compute the maximum value of <code>A</code> over the singleton dimensions of <code>r</code>, and write results to <code>r</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> maximum!([1; 1], A)
2-element Vector{Int64}:
2
4
julia> maximum!([1 1], A)
1×2 Matrix{Int64}:
3 4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L674-L695">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.minimum" href="#Base.minimum"><code>Base.minimum</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">minimum(f, itr; [init])</code></pre><p>Returns the smallest result of calling function <code>f</code> on each element of <code>itr</code>.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be a neutral element for <code>min</code> (i.e. which is greater than or equal to any other element) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> minimum(length, ["Julion", "Julia", "Jule"])
4
julia> minimum(length, []; init=typemax(Int64))
9223372036854775807
julia> minimum(sin, Real[]; init=1.0) # good, since output of sin is <= 1
1.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L700-L724">source</a></section><section><div><pre><code class="language-none">minimum(itr; [init])</code></pre><p>Returns the smallest element in a collection.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be a neutral element for <code>min</code> (i.e. which is greater than or equal to any other element) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> minimum(-20.5:10)
-20.5
julia> minimum([1,2,3])
1
julia> minimum([])
ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
[...]
julia> minimum([]; init=Inf)
Inf</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L759-L788">source</a></section><section><div><pre><code class="language-none">minimum(A::AbstractArray; dims)</code></pre><p>Compute the minimum value of an array over the given dimensions. See also the <a href="../math/#Base.min"><code>min(a,b)</code></a> function to take the minimum of two or more arguments, which can be applied elementwise to arrays via <code>min.(a,b)</code>.</p><p>See also: <a href="#Base.minimum!"><code>minimum!</code></a>, <a href="#Base.extrema"><code>extrema</code></a>, <a href="#Base.findmin"><code>findmin</code></a>, <a href="#Base.argmin"><code>argmin</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> minimum(A, dims=1)
1×2 Matrix{Int64}:
1 2
julia> minimum(A, dims=2)
2×1 Matrix{Int64}:
1
3</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L698-L723">source</a></section><section><div><pre><code class="language-none">minimum(f, A::AbstractArray; dims)</code></pre><p>Compute the minimum value by calling the function <code>f</code> on each element of an array over the given dimensions.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> minimum(abs2, A, dims=1)
1×2 Matrix{Int64}:
1 4
julia> minimum(abs2, A, dims=2)
2×1 Matrix{Int64}:
1
9</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L726-L748">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.minimum!" href="#Base.minimum!"><code>Base.minimum!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">minimum!(r, A)</code></pre><p>Compute the minimum value of <code>A</code> over the singleton dimensions of <code>r</code>, and write results to <code>r</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> minimum!([1; 1], A)
2-element Vector{Int64}:
1
3
julia> minimum!([1 1], A)
1×2 Matrix{Int64}:
1 2</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L751-L772">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.extrema" href="#Base.extrema"><code>Base.extrema</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">extrema(itr; [init]) -> (mn, mx)</code></pre><p>Compute both the minimum <code>mn</code> and maximum <code>mx</code> element in a single pass, and return them as a 2-tuple.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be a 2-tuple whose first and second elements are neutral elements for <code>min</code> and <code>max</code> respectively (i.e. which are greater/less than or equal to any other element). As a consequence, when <code>itr</code> is empty the returned <code>(mn, mx)</code> tuple will satisfy <code>mn ≥ mx</code>. When <code>init</code> is specified it may be used even for non-empty <code>itr</code>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.8</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.8 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> extrema(2:10)
(2, 10)
julia> extrema([9,pi,4.5])
(3.141592653589793, 9.0)
julia> extrema([]; init = (Inf, -Inf))
(Inf, -Inf)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L791-L817">source</a></section><section><div><pre><code class="language-none">extrema(f, itr; [init]) -> (mn, mx)</code></pre><p>Compute both the minimum <code>mn</code> and maximum <code>mx</code> of <code>f</code> applied to each element in <code>itr</code> and return them as a 2-tuple. Only one pass is made over <code>itr</code>.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be a 2-tuple whose first and second elements are neutral elements for <code>min</code> and <code>max</code> respectively (i.e. which are greater/less than or equal to any other element). It is used for non-empty collections. Note: it implies that, for empty <code>itr</code>, the returned value <code>(mn, mx)</code> satisfies <code>mn ≥ mx</code> even though for non-empty <code>itr</code> it satisfies <code>mn ≤ mx</code>. This is a "paradoxical" but yet expected result.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.2</header><div class="admonition-body"><p>This method requires Julia 1.2 or later.</p></div></div><div class="admonition is-compat"><header class="admonition-header">Julia 1.8</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.8 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> extrema(sin, 0:π)
(0.0, 0.9092974268256817)
julia> extrema(sin, Real[]; init = (1.0, -1.0)) # good, since -1 ≤ sin(::Real) ≤ 1
(1.0, -1.0)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L820-L847">source</a></section><section><div><pre><code class="language-none">extrema(A::AbstractArray; dims) -> Array{Tuple}</code></pre><p>Compute the minimum and maximum elements of an array over the given dimensions.</p><p>See also: <a href="#Base.minimum"><code>minimum</code></a>, <a href="#Base.maximum"><code>maximum</code></a>, <a href="base/@ref"><code>extrema!</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = reshape(Vector(1:2:16), (2,2,2))
2×2×2 Array{Int64, 3}:
[:, :, 1] =
1 5
3 7
[:, :, 2] =
9 13
11 15
julia> extrema(A, dims = (1,2))
1×1×2 Array{Tuple{Int64, Int64}, 3}:
[:, :, 1] =
(1, 7)
[:, :, 2] =
(9, 15)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L775-L802">source</a></section><section><div><pre><code class="language-none">extrema(f, A::AbstractArray; dims) -> Array{Tuple}</code></pre><p>Compute the minimum and maximum of <code>f</code> applied to each element in the given dimensions of <code>A</code>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.2</header><div class="admonition-body"><p>This method requires Julia 1.2 or later.</p></div></div></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L805-L813">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.argmax" href="#Base.argmax"><code>Base.argmax</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">argmax(r::AbstractRange)</code></pre><p>Ranges can have multiple maximal elements. In that case <code>argmax</code> will return a maximal index, but not necessarily the first one.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L847-L853">source</a></section><section><div><pre><code class="language-none">argmax(f, domain)</code></pre><p>Return a value <code>x</code> in the domain of <code>f</code> for which <code>f(x)</code> is maximised. If there are multiple maximal values for <code>f(x)</code> then the first one will be found.</p><p><code>domain</code> must be a non-empty iterable.</p><p>Values are compared with <code>isless</code>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>This method requires Julia 1.7 or later.</p></div></div><p>See also <a href="#Base.argmin"><code>argmin</code></a>, <a href="#Base.findmax"><code>findmax</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> argmax(abs, -10:5)
-10
julia> argmax(cos, 0:π/2:2π)
0.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L980-L1003">source</a></section><section><div><pre><code class="language-none">argmax(itr)</code></pre><p>Return the index or key of the maximal element in a collection. If there are multiple maximal elements, then the first one will be returned.</p><p>The collection must not be empty.</p><p>Values are compared with <code>isless</code>.</p><p>See also: <a href="#Base.argmin"><code>argmin</code></a>, <a href="#Base.findmax"><code>findmax</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> argmax([8, 0.1, -9, pi])
1
julia> argmax([1, 7, 7, 6])
2
julia> argmax([1, 7, 7, NaN])
4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1006-L1029">source</a></section><section><div><pre><code class="language-none">argmax(A; dims) -> indices</code></pre><p>For an array input, return the indices of the maximum elements over the given dimensions. <code>NaN</code> is treated as greater than all other values except <code>missing</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1.0 2; 3 4]
2×2 Matrix{Float64}:
1.0 2.0
3.0 4.0
julia> argmax(A, dims=1)
1×2 Matrix{CartesianIndex{2}}:
CartesianIndex(2, 1) CartesianIndex(2, 2)
julia> argmax(A, dims=2)
2×1 Matrix{CartesianIndex{2}}:
CartesianIndex(1, 2)
CartesianIndex(2, 2)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L1201-L1223">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.argmin" href="#Base.argmin"><code>Base.argmin</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">argmin(r::AbstractRange)</code></pre><p>Ranges can have multiple minimal elements. In that case <code>argmin</code> will return a minimal index, but not necessarily the first one.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L830-L836">source</a></section><section><div><pre><code class="language-none">argmin(f, domain)</code></pre><p>Return a value <code>x</code> in the domain of <code>f</code> for which <code>f(x)</code> is minimised. If there are multiple minimal values for <code>f(x)</code> then the first one will be found.</p><p><code>domain</code> must be a non-empty iterable.</p><p><code>NaN</code> is treated as less than all other values except <code>missing</code>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>This method requires Julia 1.7 or later.</p></div></div><p>See also <a href="#Base.argmax"><code>argmax</code></a>, <a href="#Base.findmin"><code>findmin</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> argmin(sign, -10:5)
-10
julia> argmin(x -> -x^3 + x^2 - 10, -5:5)
5
julia> argmin(acos, 0:0.1:1)
1.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1032-L1058">source</a></section><section><div><pre><code class="language-none">argmin(itr)</code></pre><p>Return the index or key of the minimal element in a collection. If there are multiple minimal elements, then the first one will be returned.</p><p>The collection must not be empty.</p><p><code>NaN</code> is treated as less than all other values except <code>missing</code>.</p><p>See also: <a href="#Base.argmax"><code>argmax</code></a>, <a href="#Base.findmin"><code>findmin</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> argmin([8, 0.1, -9, pi])
3
julia> argmin([7, 1, 1, 6])
2
julia> argmin([7, 1, 1, NaN])
4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1061-L1084">source</a></section><section><div><pre><code class="language-none">argmin(A; dims) -> indices</code></pre><p>For an array input, return the indices of the minimum elements over the given dimensions. <code>NaN</code> is treated as less than all other values except <code>missing</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1.0 2; 3 4]
2×2 Matrix{Float64}:
1.0 2.0
3.0 4.0
julia> argmin(A, dims=1)
1×2 Matrix{CartesianIndex{2}}:
CartesianIndex(1, 1) CartesianIndex(1, 2)
julia> argmin(A, dims=2)
2×1 Matrix{CartesianIndex{2}}:
CartesianIndex(1, 1)
CartesianIndex(2, 1)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L1176-L1198">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.findmax" href="#Base.findmax"><code>Base.findmax</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">findmax(f, domain) -> (f(x), index)</code></pre><p>Returns a pair of a value in the codomain (outputs of <code>f</code>) and the index of the corresponding value in the <code>domain</code> (inputs to <code>f</code>) such that <code>f(x)</code> is maximised. If there are multiple maximal points, then the first one will be returned.</p><p><code>domain</code> must be a non-empty iterable.</p><p>Values are compared with <code>isless</code>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>This method requires Julia 1.7 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> findmax(identity, 5:9)
(9, 5)
julia> findmax(-, 1:10)
(-1, 1)
julia> findmax(first, [(1, :a), (3, :b), (3, :c)])
(3, 2)
julia> findmax(cos, 0:π/2:2π)
(1.0, 1)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L863-L892">source</a></section><section><div><pre><code class="language-none">findmax(itr) -> (x, index)</code></pre><p>Return the maximal element of the collection <code>itr</code> and its index or key. If there are multiple maximal elements, then the first one will be returned. Values are compared with <code>isless</code>.</p><p>See also: <a href="#Base.findmin"><code>findmin</code></a>, <a href="#Base.argmax"><code>argmax</code></a>, <a href="#Base.maximum"><code>maximum</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> findmax([8, 0.1, -9, pi])
(8.0, 1)
julia> findmax([1, 7, 7, 6])
(7, 2)
julia> findmax([1, 7, 7, NaN])
(NaN, 4)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L896-L917">source</a></section><section><div><pre><code class="language-none">findmax(A; dims) -> (maxval, index)</code></pre><p>For an array input, returns the value and index of the maximum over the given dimensions. <code>NaN</code> is treated as greater than all other values except <code>missing</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1.0 2; 3 4]
2×2 Matrix{Float64}:
1.0 2.0
3.0 4.0
julia> findmax(A, dims=1)
([3.0 4.0], CartesianIndex{2}[CartesianIndex(2, 1) CartesianIndex(2, 2)])
julia> findmax(A, dims=2)
([2.0; 4.0;;], CartesianIndex{2}[CartesianIndex(1, 2); CartesianIndex(2, 2);;])</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L1139-L1158">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.findmin" href="#Base.findmin"><code>Base.findmin</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">findmin(f, domain) -> (f(x), index)</code></pre><p>Returns a pair of a value in the codomain (outputs of <code>f</code>) and the index of the corresponding value in the <code>domain</code> (inputs to <code>f</code>) such that <code>f(x)</code> is minimised. If there are multiple minimal points, then the first one will be returned.</p><p><code>domain</code> must be a non-empty iterable.</p><p><code>NaN</code> is treated as less than all other values except <code>missing</code>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>This method requires Julia 1.7 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> findmin(identity, 5:9)
(5, 1)
julia> findmin(-, 1:10)
(-10, 10)
julia> findmin(first, [(2, :a), (2, :b), (3, :c)])
(2, 1)
julia> findmin(cos, 0:π/2:2π)
(-1.0, 3)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L921-L951">source</a></section><section><div><pre><code class="language-none">findmin(itr) -> (x, index)</code></pre><p>Return the minimal element of the collection <code>itr</code> and its index or key. If there are multiple minimal elements, then the first one will be returned. <code>NaN</code> is treated as less than all other values except <code>missing</code>.</p><p>See also: <a href="#Base.findmax"><code>findmax</code></a>, <a href="#Base.argmin"><code>argmin</code></a>, <a href="#Base.minimum"><code>minimum</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> findmin([8, 0.1, -9, pi])
(-9.0, 3)
julia> findmin([1, 7, 7, 6])
(1, 1)
julia> findmin([1, 7, 7, NaN])
(NaN, 4)</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L955-L976">source</a></section><section><div><pre><code class="language-none">findmin(A; dims) -> (minval, index)</code></pre><p>For an array input, returns the value and index of the minimum over the given dimensions. <code>NaN</code> is treated as less than all other values except <code>missing</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1.0 2; 3 4]
2×2 Matrix{Float64}:
1.0 2.0
3.0 4.0
julia> findmin(A, dims=1)
([1.0 2.0], CartesianIndex{2}[CartesianIndex(1, 1) CartesianIndex(1, 2)])
julia> findmin(A, dims=2)
([1.0; 3.0;;], CartesianIndex{2}[CartesianIndex(1, 1); CartesianIndex(2, 1);;])</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L1092-L1111">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.findmax!" href="#Base.findmax!"><code>Base.findmax!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">findmax!(rval, rind, A) -> (maxval, index)</code></pre><p>Find the maximum of <code>A</code> and the corresponding linear index along singleton dimensions of <code>rval</code> and <code>rind</code>, and store the results in <code>rval</code> and <code>rind</code>. <code>NaN</code> is treated as greater than all other values except <code>missing</code>.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L1127-L1133">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.findmin!" href="#Base.findmin!"><code>Base.findmin!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">findmin!(rval, rind, A) -> (minval, index)</code></pre><p>Find the minimum of <code>A</code> and the corresponding linear index along singleton dimensions of <code>rval</code> and <code>rind</code>, and store the results in <code>rval</code> and <code>rind</code>. <code>NaN</code> is treated as less than all other values except <code>missing</code>.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L1080-L1086">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.sum" href="#Base.sum"><code>Base.sum</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">sum(f, itr; [init])</code></pre><p>Sum the results of calling function <code>f</code> on each element of <code>itr</code>.</p><p>The return type is <code>Int</code> for signed integers of less than system word size, and <code>UInt</code> for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be the additive identity (i.e. zero) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> sum(abs2, [2; 3; 4])
29</code></pre><p>Note the important difference between <code>sum(A)</code> and <code>reduce(+, A)</code> for arrays with small integer eltype:</p><pre><code class="language-julia-repl">julia> sum(Int8[100, 28])
128
julia> reduce(+, Int8[100, 28])
-128</code></pre><p>In the former case, the integers are widened to system word size and therefore the result is 128. In the latter case, no such widening happens and integer overflow results in -128.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L491-L527">source</a></section><section><div><pre><code class="language-none">sum(itr; [init])</code></pre><p>Returns the sum of all elements in a collection.</p><p>The return type is <code>Int</code> for signed integers of less than system word size, and <code>UInt</code> for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be the additive identity (i.e. zero) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p>See also: <a href="#Base.reduce-Tuple{Any, Any}"><code>reduce</code></a>, <a href="#Base.mapreduce-Tuple{Any, Any, Any}"><code>mapreduce</code></a>, <a href="#Base.count"><code>count</code></a>, <a href="#Base.union"><code>union</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> sum(1:20)
210
julia> sum(1:20; init = 0.0)
210.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L530-L556">source</a></section><section><div><pre><code class="language-none">sum(A::AbstractArray; dims)</code></pre><p>Sum elements of an array over the given dimensions.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> sum(A, dims=1)
1×2 Matrix{Int64}:
4 6
julia> sum(A, dims=2)
2×1 Matrix{Int64}:
3
7</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L475-L496">source</a></section><section><div><pre><code class="language-none">sum(f, A::AbstractArray; dims)</code></pre><p>Sum the results of calling function <code>f</code> on each element of an array over the given dimensions.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> sum(abs2, A, dims=1)
1×2 Matrix{Int64}:
10 20
julia> sum(abs2, A, dims=2)
2×1 Matrix{Int64}:
5
25</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L499-L521">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.sum!" href="#Base.sum!"><code>Base.sum!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">sum!(r, A)</code></pre><p>Sum elements of <code>A</code> over the singleton dimensions of <code>r</code>, and write results to <code>r</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> sum!([1; 1], A)
2-element Vector{Int64}:
3
7
julia> sum!([1 1], A)
1×2 Matrix{Int64}:
4 6</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L524-L545">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.prod" href="#Base.prod"><code>Base.prod</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">prod(f, itr; [init])</code></pre><p>Returns the product of <code>f</code> applied to each element of <code>itr</code>.</p><p>The return type is <code>Int</code> for signed integers of less than system word size, and <code>UInt</code> for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be the multiplicative identity (i.e. one) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> prod(abs2, [2; 3; 4])
576</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L562-L583">source</a></section><section><div><pre><code class="language-none">prod(itr; [init])</code></pre><p>Returns the product of all elements of a collection.</p><p>The return type is <code>Int</code> for signed integers of less than system word size, and <code>UInt</code> for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted.</p><p>The value returned for empty <code>itr</code> can be specified by <code>init</code>. It must be the multiplicative identity (i.e. one) as it is unspecified whether <code>init</code> is used for non-empty collections.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>Keyword argument <code>init</code> requires Julia 1.6 or later.</p></div></div><p>See also: <a href="#Base.reduce-Tuple{Any, Any}"><code>reduce</code></a>, <a href="../arrays/#Base.cumprod"><code>cumprod</code></a>, <a href="#Base.any-Tuple{Any}"><code>any</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> prod(1:5)
120
julia> prod(1:5; init = 1.0)
120.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L586-L612">source</a></section><section><div><pre><code class="language-none">prod(A::AbstractArray; dims)</code></pre><p>Multiply elements of an array over the given dimensions.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> prod(A, dims=1)
1×2 Matrix{Int64}:
3 8
julia> prod(A, dims=2)
2×1 Matrix{Int64}:
2
12</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L548-L569">source</a></section><section><div><pre><code class="language-none">prod(f, A::AbstractArray; dims)</code></pre><p>Multiply the results of calling the function <code>f</code> on each element of an array over the given dimensions.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> prod(abs2, A, dims=1)
1×2 Matrix{Int64}:
9 64
julia> prod(abs2, A, dims=2)
2×1 Matrix{Int64}:
4
144</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L572-L594">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.prod!" href="#Base.prod!"><code>Base.prod!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">prod!(r, A)</code></pre><p>Multiply elements of <code>A</code> over the singleton dimensions of <code>r</code>, and write results to <code>r</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> prod!([1; 1], A)
2-element Vector{Int64}:
2
12
julia> prod!([1 1], A)
1×2 Matrix{Int64}:
3 8</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L597-L618">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.any-Tuple{Any}" href="#Base.any-Tuple{Any}"><code>Base.any</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">any(itr) -> Bool</code></pre><p>Test whether any elements of a boolean collection are <code>true</code>, returning <code>true</code> as soon as the first <code>true</code> value in <code>itr</code> is encountered (short-circuiting). To short-circuit on <code>false</code>, use <a href="#Base.all-Tuple{Any}"><code>all</code></a>.</p><p>If the input contains <a href="../base/#Base.missing"><code>missing</code></a> values, return <code>missing</code> if all non-missing values are <code>false</code> (or equivalently, if the input contains no <code>true</code> value), following <a href="https://en.wikipedia.org/wiki/Three-valued_logic">three-valued logic</a>.</p><p>See also: <a href="#Base.all-Tuple{Any}"><code>all</code></a>, <a href="#Base.count"><code>count</code></a>, <a href="#Base.sum"><code>sum</code></a>, <a href="../math/#Base.:|"><code>|</code></a>, , <a href="../math/#||"><code>||</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = [true,false,false,true]
4-element Vector{Bool}:
1
0
0
1
julia> any(a)
true
julia> any((println(i); v) for (i, v) in enumerate(a))
1
true
julia> any([missing, true])
true
julia> any([false, missing])
missing</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1089-L1124">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.any-Tuple{AbstractArray, Any}" href="#Base.any-Tuple{AbstractArray, Any}"><code>Base.any</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">any(p, itr) -> Bool</code></pre><p>Determine whether predicate <code>p</code> returns <code>true</code> for any elements of <code>itr</code>, returning <code>true</code> as soon as the first item in <code>itr</code> for which <code>p</code> returns <code>true</code> is encountered (short-circuiting). To short-circuit on <code>false</code>, use <a href="#Base.all-Tuple{Any}"><code>all</code></a>.</p><p>If the input contains <a href="../base/#Base.missing"><code>missing</code></a> values, return <code>missing</code> if all non-missing values are <code>false</code> (or equivalently, if the input contains no <code>true</code> value), following <a href="https://en.wikipedia.org/wiki/Three-valued_logic">three-valued logic</a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> any(i->(4<=i<=6), [3,5,7])
true
julia> any(i -> (println(i); i > 3), 1:10)
1
2
3
4
true
julia> any(i -> i > 0, [1, missing])
true
julia> any(i -> i > 0, [-1, missing])
missing
julia> any(i -> i > 0, [-1, 0])
false</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1166-L1198">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.any!" href="#Base.any!"><code>Base.any!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">any!(r, A)</code></pre><p>Test whether any values in <code>A</code> along the singleton dimensions of <code>r</code> are <code>true</code>, and write results to <code>r</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [true false; true false]
2×2 Matrix{Bool}:
1 0
1 0
julia> any!([1; 1], A)
2-element Vector{Int64}:
1
1
julia> any!([1 1], A)
1×2 Matrix{Int64}:
1 0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L963-L985">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.all-Tuple{Any}" href="#Base.all-Tuple{Any}"><code>Base.all</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">all(itr) -> Bool</code></pre><p>Test whether all elements of a boolean collection are <code>true</code>, returning <code>false</code> as soon as the first <code>false</code> value in <code>itr</code> is encountered (short-circuiting). To short-circuit on <code>true</code>, use <a href="#Base.any-Tuple{Any}"><code>any</code></a>.</p><p>If the input contains <a href="../base/#Base.missing"><code>missing</code></a> values, return <code>missing</code> if all non-missing values are <code>true</code> (or equivalently, if the input contains no <code>false</code> value), following <a href="https://en.wikipedia.org/wiki/Three-valued_logic">three-valued logic</a>.</p><p>See also: <a href="#Base.all!"><code>all!</code></a>, <a href="#Base.any-Tuple{Any}"><code>any</code></a>, <a href="#Base.count"><code>count</code></a>, <a href="../math/#Base.:&"><code>&</code></a>, , <a href="../math/#&&"><code>&&</code></a>, <a href="#Base.allunique"><code>allunique</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = [true,false,false,true]
4-element Vector{Bool}:
1
0
0
1
julia> all(a)
false
julia> all((println(i); v) for (i, v) in enumerate(a))
1
2
false
julia> all([missing, false])
false
julia> all([true, missing])
missing</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1127-L1163">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.all-Tuple{AbstractArray, Any}" href="#Base.all-Tuple{AbstractArray, Any}"><code>Base.all</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">all(p, itr) -> Bool</code></pre><p>Determine whether predicate <code>p</code> returns <code>true</code> for all elements of <code>itr</code>, returning <code>false</code> as soon as the first item in <code>itr</code> for which <code>p</code> returns <code>false</code> is encountered (short-circuiting). To short-circuit on <code>true</code>, use <a href="#Base.any-Tuple{Any}"><code>any</code></a>.</p><p>If the input contains <a href="../base/#Base.missing"><code>missing</code></a> values, return <code>missing</code> if all non-missing values are <code>true</code> (or equivalently, if the input contains no <code>false</code> value), following <a href="https://en.wikipedia.org/wiki/Three-valued_logic">three-valued logic</a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> all(i->(4<=i<=6), [4,5,6])
true
julia> all(i -> (println(i); i < 3), 1:10)
1
2
3
false
julia> all(i -> i > 0, [1, missing])
missing
julia> all(i -> i > 0, [-1, missing])
false
julia> all(i -> i > 0, [1, 2])
true</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1214-L1245">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.all!" href="#Base.all!"><code>Base.all!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">all!(r, A)</code></pre><p>Test whether all values in <code>A</code> along the singleton dimensions of <code>r</code> are <code>true</code>, and write results to <code>r</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [true false; true false]
2×2 Matrix{Bool}:
1 0
1 0
julia> all!([1; 1], A)
2-element Vector{Int64}:
0
0
julia> all!([1 1], A)
1×2 Matrix{Int64}:
1 0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L891-L912">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.count" href="#Base.count"><code>Base.count</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">count([f=identity,] itr; init=0) -> Integer</code></pre><p>Count the number of elements in <code>itr</code> for which the function <code>f</code> returns <code>true</code>. If <code>f</code> is omitted, count the number of <code>true</code> elements in <code>itr</code> (which should be a collection of boolean values). <code>init</code> optionally specifies the value to start counting from and therefore also determines the output type.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p><code>init</code> keyword was added in Julia 1.6.</p></div></div><p>See also: <a href="#Base.any-Tuple{Any}"><code>any</code></a>, <a href="#Base.sum"><code>sum</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> count(i->(4<=i<=6), [2,3,4,5,6])
3
julia> count([true, false, true, true])
3
julia> count(>(3), 1:7, init=0x03)
0x07</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1268-L1292">source</a></section><section><div><pre><code class="language-none">count(
pattern::Union{AbstractChar,AbstractString,AbstractPattern},
string::AbstractString;
overlap::Bool = false,
)</code></pre><p>Return the number of matches for <code>pattern</code> in <code>string</code>. This is equivalent to calling <code>length(findall(pattern, string))</code> but more efficient.</p><p>If <code>overlap=true</code>, the matching sequences are allowed to overlap indices in the original string, otherwise they must be from disjoint character ranges.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.3</header><div class="admonition-body"><p>This method requires at least Julia 1.3.</p></div></div><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>Using a character as the pattern requires at least Julia 1.7.</p></div></div></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/regex.jl#L506-L524">source</a></section><section><div><pre><code class="language-none">count([f=identity,] A::AbstractArray; dims=:)</code></pre><p>Count the number of elements in <code>A</code> for which <code>f</code> returns <code>true</code> over the given dimensions.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.5</header><div class="admonition-body"><p><code>dims</code> keyword was added in Julia 1.5.</p></div></div><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p><code>init</code> keyword was added in Julia 1.6.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> count(<=(2), A, dims=1)
1×2 Matrix{Int64}:
1 1
julia> count(<=(2), A, dims=2)
2×1 Matrix{Int64}:
2
0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reducedim.jl#L410-L438">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.any-Tuple{Any, Any}" href="#Base.any-Tuple{Any, Any}"><code>Base.any</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">any(p, itr) -> Bool</code></pre><p>Determine whether predicate <code>p</code> returns <code>true</code> for any elements of <code>itr</code>, returning <code>true</code> as soon as the first item in <code>itr</code> for which <code>p</code> returns <code>true</code> is encountered (short-circuiting). To short-circuit on <code>false</code>, use <a href="#Base.all-Tuple{Any}"><code>all</code></a>.</p><p>If the input contains <a href="base/#Base.missing"><code>missing</code></a> values, return <code>missing</code> if all non-missing values are <code>false</code> (or equivalently, if the input contains no <code>true</code> value), following <a href="https://en.wikipedia.org/wiki/Three-valued_logic">three-valued logic</a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> any(i->(4<=i<=6), [3,5,7])
true
julia> any(i -> (println(i); i > 3), 1:10)
1
2
3
4
true
julia> any(i -> i > 0, [1, missing])
true
julia> any(i -> i > 0, [-1, missing])
missing
julia> any(i -> i > 0, [-1, 0])
false</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1166-L1198">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.all-Tuple{Any, Any}" href="#Base.all-Tuple{Any, Any}"><code>Base.all</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">all(p, itr) -> Bool</code></pre><p>Determine whether predicate <code>p</code> returns <code>true</code> for all elements of <code>itr</code>, returning <code>false</code> as soon as the first item in <code>itr</code> for which <code>p</code> returns <code>false</code> is encountered (short-circuiting). To short-circuit on <code>true</code>, use <a href="#Base.any-Tuple{Any}"><code>any</code></a>.</p><p>If the input contains <a href="base/#Base.missing"><code>missing</code></a> values, return <code>missing</code> if all non-missing values are <code>true</code> (or equivalently, if the input contains no <code>false</code> value), following <a href="https://en.wikipedia.org/wiki/Three-valued_logic">three-valued logic</a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> all(i->(4<=i<=6), [4,5,6])
true
julia> all(i -> (println(i); i < 3), 1:10)
1
2
3
false
julia> all(i -> i > 0, [1, missing])
missing
julia> all(i -> i > 0, [-1, missing])
false
julia> all(i -> i > 0, [1, 2])
true</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L1214-L1245">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.foreach" href="#Base.foreach"><code>Base.foreach</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">foreach(f, c...) -> Nothing</code></pre><p>Call function <code>f</code> on each element of iterable <code>c</code>. For multiple iterable arguments, <code>f</code> is called elementwise, and iteration stops when any iterator is finished.</p><p><code>foreach</code> should be used instead of <a href="#Base.map"><code>map</code></a> when the results of <code>f</code> are not needed, for example in <code>foreach(println, array)</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> tri = 1:3:7; res = Int[];
julia> foreach(x -> push!(res, x^2), tri)
julia> res
3-element Vector{Int64}:
1
16
49
julia> foreach((x, y) -> println(x, " with ", y), tri, 'a':'z')
1 with a
4 with b
7 with c</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L2745-L2772">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.map" href="#Base.map"><code>Base.map</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">map(f, c...) -> collection</code></pre><p>Transform collection <code>c</code> by applying <code>f</code> to each element. For multiple collection arguments, apply <code>f</code> elementwise, and stop when when any of them is exhausted.</p><p>See also <a href="#Base.map!"><code>map!</code></a>, <a href="#Base.foreach"><code>foreach</code></a>, <a href="#Base.mapreduce-Tuple{Any, Any, Any}"><code>mapreduce</code></a>, <a href="../arrays/#Base.mapslices"><code>mapslices</code></a>, <a href="../iterators/#Base.Iterators.zip"><code>zip</code></a>, <a href="../iterators/#Base.Iterators.map"><code>Iterators.map</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> map(x -> x * 2, [1, 2, 3])
3-element Vector{Int64}:
2
4
6
julia> map(+, [1, 2, 3], [10, 20, 30, 400, 5000])
3-element Vector{Int64}:
11
22
33</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L2938-L2960">source</a></section><section><div><pre><code class="language-none">map(f, A::AbstractArray...) -> N-array</code></pre><p>When acting on multi-dimensional arrays of the same <a href="../arrays/#Base.ndims"><code>ndims</code></a>, they must all have the same <a href="../arrays/#Base.axes-Tuple{Any}"><code>axes</code></a>, and the answer will too.</p><p>See also <a href="../arrays/#Base.Broadcast.broadcast"><code>broadcast</code></a>, which allows mismatched sizes.</p><p><strong>Examples</strong></p><pre><code class="language-none">julia> map(//, [1 2; 3 4], [4 3; 2 1])
2×2 Matrix{Rational{Int64}}:
1//4 2//3
3//2 4//1
julia> map(+, [1 2; 3 4], zeros(2,1))
ERROR: DimensionMismatch
julia> map(+, [1 2; 3 4], [1,10,100,1000], zeros(3,1)) # iterates until 3rd is exhausted
3-element Vector{Float64}:
2.0
13.0
102.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L3030-L3054">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.map!" href="#Base.map!"><code>Base.map!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">map!(function, destination, collection...)</code></pre><p>Like <a href="#Base.map"><code>map</code></a>, but stores the result in <code>destination</code> rather than a new collection. <code>destination</code> must be at least as large as the smallest collection.</p><p>See also: <a href="#Base.map"><code>map</code></a>, <a href="#Base.foreach"><code>foreach</code></a>, <a href="../iterators/#Base.Iterators.zip"><code>zip</code></a>, <a href="../c/#Base.copyto!"><code>copyto!</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = zeros(3);
julia> map!(x -> x * 2, a, [1, 2, 3]);
julia> a
3-element Vector{Float64}:
2.0
4.0
6.0
julia> map!(+, zeros(Int, 5), 100:999, 1:3)
5-element Vector{Int64}:
101
103
105
0
0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L2995-L3023">source</a></section><section><div><pre><code class="language-none">map!(f, values(dict::AbstractDict))</code></pre><p>Modifies <code>dict</code> by transforming each value from <code>val</code> to <code>f(val)</code>. Note that the type of <code>dict</code> cannot be changed: if <code>f(val)</code> is not an instance of the value type of <code>dict</code> then it will be converted to the value type if possible and otherwise raise an error.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.2</header><div class="admonition-body"><p><code>map!(f, values(dict::AbstractDict))</code> requires Julia 1.2 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> d = Dict(:a => 1, :b => 2)
Dict{Symbol, Int64} with 2 entries:
:a => 1
:b => 2
julia> map!(v -> v-1, values(d))
ValueIterator for a Dict{Symbol, Int64} with 2 entries. Values:
0
1</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractdict.jl#L587-L609">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.mapreduce-Tuple{Any, Any, Any}" href="#Base.mapreduce-Tuple{Any, Any, Any}"><code>Base.mapreduce</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">mapreduce(f, op, itrs...; [init])</code></pre><p>Apply function <code>f</code> to each element(s) in <code>itrs</code>, and then reduce the result using the binary function <code>op</code>. If provided, <code>init</code> must be a neutral element for <code>op</code> that will be returned for empty collections. It is unspecified whether <code>init</code> is used for non-empty collections. In general, it will be necessary to provide <code>init</code> to work with empty collections.</p><p><a href="#Base.mapreduce-Tuple{Any, Any, Any}"><code>mapreduce</code></a> is functionally equivalent to calling <code>reduce(op, map(f, itr); init=init)</code>, but will in general execute faster since no intermediate collection needs to be created. See documentation for <a href="#Base.reduce-Tuple{Any, Any}"><code>reduce</code></a> and <a href="#Base.map"><code>map</code></a>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.2</header><div class="admonition-body"><p><code>mapreduce</code> with multiple iterators requires Julia 1.2 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> mapreduce(x->x^2, +, [1:3;]) # == 1 + 4 + 9
14</code></pre><p>The associativity of the reduction is implementation-dependent. Additionally, some implementations may reuse the return value of <code>f</code> for elements that appear multiple times in <code>itr</code>. Use <a href="#Base.mapfoldl-Tuple{Any, Any, Any}"><code>mapfoldl</code></a> or <a href="#Base.mapfoldr-Tuple{Any, Any, Any}"><code>mapfoldr</code></a> instead for guaranteed left or right associativity and invocation of <code>f</code> for every value.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L275-L301">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.mapfoldl-Tuple{Any, Any, Any}" href="#Base.mapfoldl-Tuple{Any, Any, Any}"><code>Base.mapfoldl</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">mapfoldl(f, op, itr; [init])</code></pre><p>Like <a href="#Base.mapreduce-Tuple{Any, Any, Any}"><code>mapreduce</code></a>, but with guaranteed left associativity, as in <a href="#Base.foldl-Tuple{Any, Any}"><code>foldl</code></a>. If provided, the keyword argument <code>init</code> will be used exactly once. In general, it will be necessary to provide <code>init</code> to work with empty collections.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L163-L169">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.mapfoldr-Tuple{Any, Any, Any}" href="#Base.mapfoldr-Tuple{Any, Any, Any}"><code>Base.mapfoldr</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">mapfoldr(f, op, itr; [init])</code></pre><p>Like <a href="#Base.mapreduce-Tuple{Any, Any, Any}"><code>mapreduce</code></a>, but with guaranteed right associativity, as in <a href="#Base.foldr-Tuple{Any, Any}"><code>foldr</code></a>. If provided, the keyword argument <code>init</code> will be used exactly once. In general, it will be necessary to provide <code>init</code> to work with empty collections.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/reduce.jl#L211-L217">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.first" href="#Base.first"><code>Base.first</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">first(coll)</code></pre><p>Get the first element of an iterable collection. Return the start point of an <a href="#Base.AbstractRange"><code>AbstractRange</code></a> even if it is empty.</p><p>See also: <a href="../iterators/#Base.Iterators.only"><code>only</code></a>, <a href="#Base.firstindex"><code>firstindex</code></a>, <a href="#Base.last"><code>last</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> first(2:2:10)
2
julia> first([1; 2; 3; 4])
1</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L406-L422">source</a></section><section><div><pre><code class="language-none">first(itr, n::Integer)</code></pre><p>Get the first <code>n</code> elements of the iterable collection <code>itr</code>, or fewer elements if <code>itr</code> is not long enough.</p><p>See also: <a href="../strings/#Base.startswith"><code>startswith</code></a>, <a href="../iterators/#Base.Iterators.take"><code>Iterators.take</code></a>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>This method requires at least Julia 1.6.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> first(["foo", "bar", "qux"], 2)
2-element Vector{String}:
"foo"
"bar"
julia> first(1:6, 10)
1:6
julia> first(Bool[], 1)
Bool[]</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L429-L453">source</a></section><section><div><pre><code class="language-julia">first(s::AbstractString, n::Integer)</code></pre><p>Get a string consisting of the first <code>n</code> characters of <code>s</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> first("∀ϵ≠0: ϵ²>0", 0)
""
julia> first("∀ϵ≠0: ϵ²>0", 1)
"∀"
julia> first("∀ϵ≠0: ϵ²>0", 3)
"∀ϵ≠"</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/strings/basic.jl#L641-L657">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.last" href="#Base.last"><code>Base.last</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">last(coll)</code></pre><p>Get the last element of an ordered collection, if it can be computed in O(1) time. This is accomplished by calling <a href="#Base.lastindex"><code>lastindex</code></a> to get the last index. Return the end point of an <a href="#Base.AbstractRange"><code>AbstractRange</code></a> even if it is empty.</p><p>See also <a href="#Base.first"><code>first</code></a>, <a href="../strings/#Base.endswith"><code>endswith</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> last(1:2:10)
9
julia> last([1; 2; 3; 4])
4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L461-L478">source</a></section><section><div><pre><code class="language-none">last(itr, n::Integer)</code></pre><p>Get the last <code>n</code> elements of the iterable collection <code>itr</code>, or fewer elements if <code>itr</code> is not long enough.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p>This method requires at least Julia 1.6.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> last(["foo", "bar", "qux"], 2)
2-element Vector{String}:
"bar"
"qux"
julia> last(1:6, 10)
1:6
julia> last(Float64[], 1)
Float64[]</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L481-L503">source</a></section><section><div><pre><code class="language-julia">last(s::AbstractString, n::Integer)</code></pre><p>Get a string consisting of the last <code>n</code> characters of <code>s</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> last("∀ϵ≠0: ϵ²>0", 0)
""
julia> last("∀ϵ≠0: ϵ²>0", 1)
"0"
julia> last("∀ϵ≠0: ϵ²>0", 3)
"²>0"</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/strings/basic.jl#L660-L676">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.front" href="#Base.front"><code>Base.front</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">front(x::Tuple)::Tuple</code></pre><p>Return a <code>Tuple</code> consisting of all but the last component of <code>x</code>.</p><p>See also: <a href="#Base.first"><code>first</code></a>, <a href="#Base.tail"><code>tail</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> Base.front((1,2,3))
(1, 2)
julia> Base.front(())
ERROR: ArgumentError: Cannot call front on an empty tuple.</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/tuple.jl#L190-L205">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.tail" href="#Base.tail"><code>Base.tail</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">tail(x::Tuple)::Tuple</code></pre><p>Return a <code>Tuple</code> consisting of all but the first component of <code>x</code>.</p><p>See also: <a href="#Base.front"><code>front</code></a>, <a href="#Base.rest"><code>rest</code></a>, <a href="#Base.first"><code>first</code></a>, <a href="../iterators/#Base.Iterators.peel"><code>Iterators.peel</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> Base.tail((1,2,3))
(2, 3)
julia> Base.tail(())
ERROR: ArgumentError: Cannot call tail on an empty tuple.</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/essentials.jl#L235-L250">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.step" href="#Base.step"><code>Base.step</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">step(r)</code></pre><p>Get the step size of an <a href="#Base.AbstractRange"><code>AbstractRange</code></a> object.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> step(1:10)
1
julia> step(1:2:10)
2
julia> step(2.5:0.3:10.9)
0.3
julia> step(range(2.5, stop=10.9, length=85))
0.1</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/range.jl#L658-L677">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.collect-Tuple{Any}" href="#Base.collect-Tuple{Any}"><code>Base.collect</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">collect(collection)</code></pre><p>Return an <code>Array</code> of all items in a collection or iterator. For dictionaries, returns <code>Pair{KeyType, ValType}</code>. If the argument is array-like or is an iterator with the <a href="#Base.IteratorSize"><code>HasShape</code></a> trait, the result will have the same shape and number of dimensions as the argument.</p><p>Used by comprehensions to turn a generator into an <code>Array</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> collect(1:2:13)
7-element Vector{Int64}:
1
3
5
7
9
11
13
julia> [x^2 for x in 1:8 if isodd(x)]
4-element Vector{Int64}:
1
9
25
49</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/array.jl#L682-L711">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.collect-Tuple{Type, Any}" href="#Base.collect-Tuple{Type, Any}"><code>Base.collect</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">collect(element_type, collection)</code></pre><p>Return an <code>Array</code> with the given element type of all items in a collection or iterable. The result has the same shape and number of dimensions as <code>collection</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> collect(Float64, 1:2:5)
3-element Vector{Float64}:
1.0
3.0
5.0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/array.jl#L632-L646">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.filter" href="#Base.filter"><code>Base.filter</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">filter(f, a)</code></pre><p>Return a copy of collection <code>a</code>, removing elements for which <code>f</code> is <code>false</code>. The function <code>f</code> is passed one argument.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.4</header><div class="admonition-body"><p>Support for <code>a</code> as a tuple requires at least Julia 1.4.</p></div></div><p>See also: <a href="#Base.filter!"><code>filter!</code></a>, <a href="../iterators/#Base.Iterators.filter"><code>Iterators.filter</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = 1:10
1:10
julia> filter(isodd, a)
5-element Vector{Int64}:
1
3
5
7
9</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/array.jl#L2509-L2533">source</a></section><section><div><pre><code class="language-none">filter(f, d::AbstractDict)</code></pre><p>Return a copy of <code>d</code>, removing elements for which <code>f</code> is <code>false</code>. The function <code>f</code> is passed <code>key=>value</code> pairs.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> d = Dict(1=>"a", 2=>"b")
Dict{Int64, String} with 2 entries:
2 => "b"
1 => "a"
julia> filter(p->isodd(p.first), d)
Dict{Int64, String} with 1 entry:
1 => "a"</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractdict.jl#L450-L467">source</a></section><section><div><pre><code class="language-none">filter(f, itr::SkipMissing{<:AbstractArray})</code></pre><p>Return a vector similar to the array wrapped by the given <code>SkipMissing</code> iterator but with all missing elements and those for which <code>f</code> returns <code>false</code> removed.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.2</header><div class="admonition-body"><p>This method requires Julia 1.2 or later.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> x = [1 2; missing 4]
2×2 Matrix{Union{Missing, Int64}}:
1 2
missing 4
julia> filter(isodd, skipmissing(x))
1-element Vector{Int64}:
1</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/missing.jl#L372-L392">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.filter!" href="#Base.filter!"><code>Base.filter!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">filter!(f, a)</code></pre><p>Update collection <code>a</code>, removing elements for which <code>f</code> is <code>false</code>. The function <code>f</code> is passed one argument.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> filter!(isodd, Vector(1:10))
5-element Vector{Int64}:
1
3
5
7
9</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/array.jl#L2563-L2579">source</a></section><section><div><pre><code class="language-none">filter!(f, d::AbstractDict)</code></pre><p>Update <code>d</code>, removing elements for which <code>f</code> is <code>false</code>. The function <code>f</code> is passed <code>key=>value</code> pairs.</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia> d = Dict(1=>"a", 2=>"b", 3=>"c")
Dict{Int64, String} with 3 entries:
2 => "b"
3 => "c"
1 => "a"
julia> filter!(p->isodd(p.first), d)
Dict{Int64, String} with 2 entries:
3 => "c"
1 => "a"</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractdict.jl#L408-L427">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.replace-Tuple{Any, Vararg{Pair}}" href="#Base.replace-Tuple{Any, Vararg{Pair}}"><code>Base.replace</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">replace(A, old_new::Pair...; [count::Integer])</code></pre><p>Return a copy of collection <code>A</code> where, for each pair <code>old=>new</code> in <code>old_new</code>, all occurrences of <code>old</code> are replaced by <code>new</code>. Equality is determined using <a href="base/#Base.isequal"><code>isequal</code></a>. If <code>count</code> is specified, then replace at most <code>count</code> occurrences in total.</p><p>The element type of the result is chosen using promotion (see <a href="base/#Base.promote_type"><code>promote_type</code></a>) based on the element type of <code>A</code> and on the types of the <code>new</code> values in pairs. If <code>count</code> is omitted and the element type of <code>A</code> is a <code>Union</code>, the element type of the result will not include singleton types which are replaced with values of a different type: for example, <code>Union{T,Missing}</code> will become <code>T</code> if <code>missing</code> is replaced.</p><p>See also <a href="collections/#Base.replace!"><code>replace!</code></a>, <a href="collections/#Base.splice!"><code>splice!</code></a>, <a href="collections/#Base.delete!"><code>delete!</code></a>, <a href="collections/#Base.insert!"><code>insert!</code></a>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>Version 1.7 is required to replace elements of a <code>Tuple</code>.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> replace([1, 2, 1, 3], 1=>0, 2=>4, count=2)
4-element Vector{Int64}:
0
4
1
3
julia> replace([1, missing], missing=>0)
2-element Vector{Int64}:
1
0</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L576-L610">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.replace-Tuple{Union{Function, Type}, Any}" href="#Base.replace-Tuple{Union{Function, Type}, Any}"><code>Base.replace</code></a> — <span class="docstring-category">Method</span></header><section><div><pre><code class="language-julia">replace(new::Function, A; [count::Integer])</code></pre><p>Return a copy of <code>A</code> where each value <code>x</code> in <code>A</code> is replaced by <code>new(x)</code>. If <code>count</code> is specified, then replace at most <code>count</code> values in total (replacements being defined as <code>new(x) !== x</code>).</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>Version 1.7 is required to replace elements of a <code>Tuple</code>.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> replace(x -> isodd(x) ? 2x : x, [1, 2, 3, 4])
4-element Vector{Int64}:
2
2
6
4
julia> replace(Dict(1=>2, 3=>4)) do kv
first(kv) < 3 ? first(kv)=>3 : kv
end
Dict{Int64, Int64} with 2 entries:
3 => 4
1 => 3</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L637-L663">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.replace!" href="#Base.replace!"><code>Base.replace!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">replace!(A, old_new::Pair...; [count::Integer])</code></pre><p>For each pair <code>old=>new</code> in <code>old_new</code>, replace all occurrences of <code>old</code> in collection <code>A</code> by <code>new</code>. Equality is determined using <a href="../base/#Base.isequal"><code>isequal</code></a>. If <code>count</code> is specified, then replace at most <code>count</code> occurrences in total. See also <a href="#Base.replace-Tuple{Any, Vararg{Pair}}"><code>replace</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> replace!([1, 2, 1, 3], 1=>0, 2=>4, count=2)
4-element Vector{Int64}:
0
4
1
3
julia> replace!(Set([1, 2, 3]), 1=>0)
Set{Int64} with 3 elements:
0
2
3</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L506-L530">source</a></section><section><div><pre><code class="language-none">replace!(new::Function, A; [count::Integer])</code></pre><p>Replace each element <code>x</code> in collection <code>A</code> by <code>new(x)</code>. If <code>count</code> is specified, then replace at most <code>count</code> values in total (replacements being defined as <code>new(x) !== x</code>).</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> replace!(x -> isodd(x) ? 2x : x, [1, 2, 3, 4])
4-element Vector{Int64}:
2
2
6
4
julia> replace!(Dict(1=>2, 3=>4)) do kv
first(kv) < 3 ? first(kv)=>3 : kv
end
Dict{Int64, Int64} with 2 entries:
3 => 4
1 => 3
julia> replace!(x->2x, Set([3, 6]))
Set{Int64} with 2 elements:
6
12</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/set.jl#L544-L572">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.rest" href="#Base.rest"><code>Base.rest</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">Base.rest(collection[, itr_state])</code></pre><p>Generic function for taking the tail of <code>collection</code>, starting from a specific iteration state <code>itr_state</code>. Return a <code>Tuple</code>, if <code>collection</code> itself is a <code>Tuple</code>, a subtype of <code>AbstractVector</code>, if <code>collection</code> is an <code>AbstractArray</code>, a subtype of <code>AbstractString</code> if <code>collection</code> is an <code>AbstractString</code>, and an arbitrary iterator, falling back to <code>Iterators.rest(collection[, itr_state])</code>, otherwise.</p><p>Can be overloaded for user-defined collection types to customize the behavior of <a href="../../manual/functions/#destructuring-assignment">slurping in assignments</a>, like <code>a, b... = collection</code>.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.6</header><div class="admonition-body"><p><code>Base.rest</code> requires at least Julia 1.6.</p></div></div><p>See also: <a href="#Base.first"><code>first</code></a>, <a href="../iterators/#Base.Iterators.rest"><code>Iterators.rest</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> first, state = iterate(a)
(1, 2)
julia> first, Base.rest(a, state)
(1, [3, 2, 4])</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/tuple.jl#L101-L131">source</a></section></article><h2 id="可索引集合"><a class="docs-heading-anchor" href="#可索引集合">可索引集合</a><a id="可索引集合-1"></a><a class="docs-heading-anchor-permalink" href="#可索引集合" title="Permalink"></a></h2><article class="docstring"><header><a class="docstring-binding" id="Base.getindex" href="#Base.getindex"><code>Base.getindex</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">getindex(collection, key...)</code></pre><p>Retrieve the value(s) stored at the given key or index within a collection. The syntax <code>a[i,j,...]</code> is converted by the compiler to <code>getindex(a, i, j, ...)</code>.</p><p>See also <a href="#Base.get"><code>get</code></a>, <a href="#Base.keys"><code>keys</code></a>, <a href="../arrays/#Base.eachindex"><code>eachindex</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> A = Dict("a" => 1, "b" => 2)
Dict{String, Int64} with 2 entries:
"b" => 2
"a" => 1
julia> getindex(A, "a")
1</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/array.jl#L902-L920">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.setindex!" href="#Base.setindex!"><code>Base.setindex!</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">setindex!(collection, value, key...)</code></pre><p>Store the given value at the given key or index within a collection. The syntax <code>a[i,j,...] = x</code> is converted by the compiler to <code>(setindex!(a, x, i, j, ...); x)</code>.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/array.jl#L958-L963">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.firstindex" href="#Base.firstindex"><code>Base.firstindex</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">firstindex(collection) -> Integer
firstindex(collection, d) -> Integer</code></pre><p>Return the first index of <code>collection</code>. If <code>d</code> is given, return the first index of <code>collection</code> along dimension <code>d</code>.</p><p>The syntaxes <code>A[begin]</code> and <code>A[1, begin]</code> lower to <code>A[firstindex(A)]</code> and <code>A[1, firstindex(A, 2)]</code>, respectively.</p><p>See also: <a href="#Base.first"><code>first</code></a>, <a href="../arrays/#Base.axes-Tuple{Any}"><code>axes</code></a>, <a href="#Base.lastindex"><code>lastindex</code></a>, <a href="../strings/#Base.nextind"><code>nextind</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> firstindex([1,2,4])
1
julia> firstindex(rand(3,4,5), 2)
1</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L381-L400">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.lastindex" href="#Base.lastindex"><code>Base.lastindex</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">lastindex(collection) -> Integer
lastindex(collection, d) -> Integer</code></pre><p>Return the last index of <code>collection</code>. If <code>d</code> is given, return the last index of <code>collection</code> along dimension <code>d</code>.</p><p>The syntaxes <code>A[end]</code> and <code>A[end, end]</code> lower to <code>A[lastindex(A)]</code> and <code>A[lastindex(A, 1), lastindex(A, 2)]</code>, respectively.</p><p>See also: <a href="../arrays/#Base.axes-Tuple{Any}"><code>axes</code></a>, <a href="#Base.firstindex"><code>firstindex</code></a>, <a href="../arrays/#Base.eachindex"><code>eachindex</code></a>, <a href="../strings/#Base.prevind"><code>prevind</code></a>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> lastindex([1,2,4])
3
julia> lastindex(rand(3,4,5), 2)
4</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/abstractarray.jl#L358-L377">source</a></section></article><p>以下类型均完全实现了上述函数:</p><ul><li><a href="../arrays/#Core.Array"><code>Array</code></a></li><li><a href="../arrays/#Base.BitArray"><code>BitArray</code></a></li><li><a href="../arrays/#Core.AbstractArray"><code>AbstractArray</code></a></li><li><code>SubArray</code></li></ul><p>以下类型仅实现了部分上述函数:</p><ul><li><a href="#Base.AbstractRange"><code>AbstractRange</code></a></li><li><a href="#Base.UnitRange"><code>UnitRange</code></a></li><li><code>Tuple</code></li><li><code>AbstractString</code></li><li><a href="#Base.Dict"><code>Dict</code></a></li><li><a href="#Base.IdDict"><code>IdDict</code></a></li><li><a href="#Base.WeakKeyDict"><code>WeakKeyDict</code></a></li><li><a href="../base/#Core.NamedTuple"><code>NamedTuple</code></a></li></ul><h2 id="字典"><a class="docs-heading-anchor" href="#字典">字典</a><a id="字典-1"></a><a class="docs-heading-anchor-permalink" href="#字典" title="Permalink"></a></h2><p><a href="#Base.Dict"><code>Dict</code></a> 是一个标准字典。其实现利用了 <a href="../base/#Base.hash"><code>hash</code></a> 作为键的哈希函数和 <a href="../base/#Base.isequal"><code>isequal</code></a> 来决定是否相等。对于自定义类型,可以定义这两个函数来重载它们在哈希表内的存储方式。</p><p><a href="#Base.IdDict"><code>IdDict</code></a> 是一种特殊的哈希表,在里面键始终是对象标识符。</p><p><a href="#Base.WeakKeyDict"><code>WeakKeyDict</code></a> 是一个哈希表的实现,里面键是对象的弱引用, 所以即使键在哈希表中被引用也有可能被垃圾回收。 它像 <code>Dict</code> 一样使用 <code>hash</code> 来做哈希和 <code>isequal</code> 来做相等判断, 但是它不会在插入时转换键,这点不像 <code>Dict</code>。</p><p><a href="#Base.Dict"><code>Dict</code></a>s 可以由传递含有 <code>=></code> 的成对对象给 <a href="#Base.Dict"><code>Dict</code></a> 的构造函数来被创建:<code>Dict("A"=>1, "B"=>2)</code>。 这个调用会尝试从键值对中推到类型信息(比如这个例子创造了一个 <code>Dict{String, Int64}</code>)。 为了显式指定类型,请使用语法 <code>Dict{KeyType,ValueType}(...)</code>。例如:<code>Dict{String,Int32}("A"=>1, "B"=>2)</code>。</p><p>字典也可以用生成器创建。例如:<code>Dict(i => f(i) for i = 1:10)</code>。</p><p>对于字典 <code>D</code>,若键 <code>x</code> 的值存在,则语法 <code>D[x]</code> 返回 <code>x</code> 的值;否则抛出一个错误。 <code>D[x] = y</code> 存储键值对 <code>x => y</code> 到 <code>D</code> 中,会覆盖键 <code>x</code> 的已有的值。 多个参数传入<code>D[...]</code> 会被转化成元组; 例如:语法 <code>D[x,y]</code> 等于 <code>D[(x,y)]</code>,也就是说,它指向键为元组 <code>(x,y)</code> 的值。</p><article class="docstring"><header><a class="docstring-binding" id="Base.AbstractDict" href="#Base.AbstractDict"><code>Base.AbstractDict</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">AbstractDict{K, V}</code></pre><p>Supertype for dictionary-like types with keys of type <code>K</code> and values of type <code>V</code>. <a href="#Base.Dict"><code>Dict</code></a>, <a href="#Base.IdDict"><code>IdDict</code></a> and other types are subtypes of this. An <code>AbstractDict{K, V}</code> should be an iterator of <code>Pair{K, V}</code>.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/essentials.jl#L17-L23">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.Dict" href="#Base.Dict"><code>Base.Dict</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">Dict([itr])</code></pre><p><code>Dict{K,V}()</code> constructs a hash table with keys of type <code>K</code> and values of type <code>V</code>. Keys are compared with <a href="../base/#Base.isequal"><code>isequal</code></a> and hashed with <a href="../base/#Base.hash"><code>hash</code></a>.</p><p>Given a single iterable argument, constructs a <a href="#Base.Dict"><code>Dict</code></a> whose key-value pairs are taken from 2-tuples <code>(key,value)</code> generated by the argument.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> Dict([("A", 1), ("B", 2)])
Dict{String, Int64} with 2 entries:
"B" => 2
"A" => 1</code></pre><p>Alternatively, a sequence of pair arguments may be passed.</p><pre><code class="language-julia-repl">julia> Dict("A"=>1, "B"=>2)
Dict{String, Int64} with 2 entries:
"B" => 2
"A" => 1</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/dict.jl#L52-L77">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.IdDict" href="#Base.IdDict"><code>Base.IdDict</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">IdDict([itr])</code></pre><p><code>IdDict{K,V}()</code> constructs a hash table using <a href="../base/#Base.objectid"><code>objectid</code></a> as hash and <code>===</code> as equality with keys of type <code>K</code> and values of type <code>V</code>.</p><p>See <a href="#Base.Dict"><code>Dict</code></a> for further help. In the example below, The <code>Dict</code> keys are all <code>isequal</code> and therefore get hashed the same, so they get overwritten. The <code>IdDict</code> hashes by object-id, and thus preserves the 3 different keys.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> Dict(true => "yes", 1 => "no", 1.0 => "maybe")
Dict{Real, String} with 1 entry:
1.0 => "maybe"
julia> IdDict(true => "yes", 1 => "no", 1.0 => "maybe")
IdDict{Any, String} with 3 entries:
true => "yes"
1.0 => "maybe"
1 => "no"</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/iddict.jl#L3-L25">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.WeakKeyDict" href="#Base.WeakKeyDict"><code>Base.WeakKeyDict</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">WeakKeyDict([itr])</code></pre><p><code>WeakKeyDict()</code> constructs a hash table where the keys are weak references to objects which may be garbage collected even when referenced in a hash table.</p><p>See <a href="#Base.Dict"><code>Dict</code></a> for further help. Note, unlike <a href="#Base.Dict"><code>Dict</code></a>, <code>WeakKeyDict</code> does not convert keys on insertion, as this would imply the key object was unreferenced anywhere before insertion.</p></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/weakkeydict.jl#L5-L15">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.ImmutableDict" href="#Base.ImmutableDict"><code>Base.ImmutableDict</code></a> — <span class="docstring-category">Type</span></header><section><div><pre><code class="language-julia">ImmutableDict</code></pre><p><code>ImmutableDict</code> is a dictionary implemented as an immutable linked list, which is optimal for small dictionaries that are constructed over many individual insertions. Note that it is not possible to remove a value, although it can be partially overridden and hidden by inserting a new value with the same key.</p><pre><code class="language-none">ImmutableDict(KV::Pair)</code></pre><p>Create a new entry in the <code>ImmutableDict</code> for a <code>key => value</code> pair</p><ul><li>use <code>(key => value) in dict</code> to see if this particular combination is in the properties set</li><li>use <code>get(dict, key, default)</code> to retrieve the most recent value for a particular key</li></ul></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/dict.jl#L772-L787">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.haskey" href="#Base.haskey"><code>Base.haskey</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">haskey(collection, key) -> Bool</code></pre><p>Determine whether a collection has a mapping for a given <code>key</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> D = Dict('a'=>2, 'b'=>3)
Dict{Char, Int64} with 2 entries:
'a' => 2
'b' => 3
julia> haskey(D, 'a')
true
julia> haskey(D, 'c')
false</code></pre></div><a class="docs-sourcelink" target="_blank" href="https://github.com/JuliaLang/julia/blob/17cfb8e65ead377bf1b4598d8a9869144142c84e/base/dict.jl#L550-L568">source</a></section></article><article class="docstring"><header><a class="docstring-binding" id="Base.get" href="#Base.get"><code>Base.get</code></a> — <span class="docstring-category">Function</span></header><section><div><pre><code class="language-julia">get(collection, key, default)</code></pre><p>Return the value stored for the given key, or the given default value if no mapping for the key is present.</p><div class="admonition is-compat"><header class="admonition-header">Julia 1.7</header><div class="admonition-body"><p>For tuples and numbers, this function requires at least Julia 1.7.</p></div></div><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia> d = Dict("a"=>1, "b"=>2);
julia> get(d, "a", 3)
1