mollusk 0e4acfb8f2 fix incorrect folder name for julia-0.6.x
Former-commit-id: ef2c7401e0876f22d2f7762d182cfbcd5a7d9c70
2018-06-11 03:28:36 -07:00

107 lines
21 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Iteration utilities · The Julia Language</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-6', 'auto');
ga('send', 'pageview');
</script><link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css" rel="stylesheet" type="text/css"/><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/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js" data-main="../assets/documenter.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link href="../assets/documenter.css" rel="stylesheet" type="text/css"/><link href="../assets/julia-manual.css" rel="stylesheet" type="text/css"/></head><body><nav class="toc"><a href="../index.html"><img class="logo" src="../assets/logo.png" alt="The Julia Language logo"/></a><h1>The Julia Language</h1><select id="version-selector" onChange="window.location.href=this.value" style="visibility: hidden"></select><form class="search" id="search-form" action="../search.html"><input id="search-query" name="q" type="text" placeholder="Search docs"/></form><ul><li><a class="toctext" href="../index.html">Home</a></li><li><span class="toctext">Manual</span><ul><li><a class="toctext" href="../manual/introduction.html">Introduction</a></li><li><a class="toctext" href="../manual/getting-started.html">Getting Started</a></li><li><a class="toctext" href="../manual/variables.html">Variables</a></li><li><a class="toctext" href="../manual/integers-and-floating-point-numbers.html">Integers and Floating-Point Numbers</a></li><li><a class="toctext" href="../manual/mathematical-operations.html">Mathematical Operations and Elementary Functions</a></li><li><a class="toctext" href="../manual/complex-and-rational-numbers.html">Complex and Rational Numbers</a></li><li><a class="toctext" href="../manual/strings.html">Strings</a></li><li><a class="toctext" href="../manual/functions.html">Functions</a></li><li><a class="toctext" href="../manual/control-flow.html">Control Flow</a></li><li><a class="toctext" href="../manual/variables-and-scoping.html">Scope of Variables</a></li><li><a class="toctext" href="../manual/types.html">Types</a></li><li><a class="toctext" href="../manual/methods.html">Methods</a></li><li><a class="toctext" href="../manual/constructors.html">Constructors</a></li><li><a class="toctext" href="../manual/conversion-and-promotion.html">Conversion and Promotion</a></li><li><a class="toctext" href="../manual/interfaces.html">Interfaces</a></li><li><a class="toctext" href="../manual/modules.html">Modules</a></li><li><a class="toctext" href="../manual/documentation.html">Documentation</a></li><li><a class="toctext" href="../manual/metaprogramming.html">Metaprogramming</a></li><li><a class="toctext" href="../manual/arrays.html">Multi-dimensional Arrays</a></li><li><a class="toctext" href="../manual/linear-algebra.html">Linear algebra</a></li><li><a class="toctext" href="../manual/networking-and-streams.html">Networking and Streams</a></li><li><a class="toctext" href="../manual/parallel-computing.html">Parallel Computing</a></li><li><a class="toctext" href="../manual/dates.html">Date and DateTime</a></li><li><a class="toctext" href="../manual/interacting-with-julia.html">Interacting With Julia</a></li><li><a class="toctext" href="../manual/running-external-programs.html">Running External Programs</a></li><li><a class="toctext" href="../manual/calling-c-and-fortran-code.html">Calling C and Fortran Code</a></li><li><a class="toctext" href="../manual/handling-operating-system-variation.html">Handling Operating System Variation</a></li><li><a class="toctext" href="../manual/environment-variables.html">Environment Variables</a></li><li><a class="toctext" href="../manual/embedding.html">Embedding Julia</a></li><li><a class="toctext" href="../manual/packages.html">Packages</a></li><li><a class="toctext" href="../manual/profile.html">Profiling</a></li><li><a class="toctext" href="../manual/stacktraces.html">Stack Traces</a></li><li><a class="toctext" href="../manual/performance-tips.html">Performance Tips</a></li><li><a class="toctext" href="../manual/workflow-tips.html">Workflow Tips</a></li><li><a class="toctext" href="../manual/style-guide.html">Style Guide</a></li><li><a class="toctext" href="../manual/faq.html">Frequently Asked Questions</a></li><li><a class="toctext" href="../manual/noteworthy-differences.html">Noteworthy Differences from other Languages</a></li><li><a class="toctext" href="../manual/unicode-input.html">Unicode Input</a></li></ul></li><li><span class="toctext">Standard Library</span><ul><li><a class="toctext" href="base.html">Essentials</a></li><li><a class="toctext" href="collections.html">Collections and Data Structures</a></li><li><a class="toctext" href="math.html">Mathematics</a></li><li><a class="toctext" href="numbers.html">Numbers</a></li><li><a class="toctext" href="strings.html">Strings</a></li><li><a class="toctext" href="arrays.html">Arrays</a></li><li><a class="toctext" href="parallel.html">Tasks and Parallel Computing</a></li><li><a class="toctext" href="linalg.html">Linear Algebra</a></li><li><a class="toctext" href="constants.html">Constants</a></li><li><a class="toctext" href="file.html">Filesystem</a></li><li><a class="toctext" href="io-network.html">I/O and Network</a></li><li><a class="toctext" href="punctuation.html">Punctuation</a></li><li><a class="toctext" href="sort.html">Sorting and Related Functions</a></li><li><a class="toctext" href="pkg.html">Package Manager Functions</a></li><li><a class="toctext" href="dates.html">Dates and Time</a></li><li class="current"><a class="toctext" href="iterators.html">Iteration utilities</a><ul class="internal"></ul></li><li><a class="toctext" href="test.html">Unit Testing</a></li><li><a class="toctext" href="c.html">C Interface</a></li><li><a class="toctext" href="libc.html">C Standard Library</a></li><li><a class="toctext" href="libdl.html">Dynamic Linker</a></li><li><a class="toctext" href="profile.html">Profiling</a></li><li><a class="toctext" href="stacktraces.html">StackTraces</a></li><li><a class="toctext" href="simd-types.html">SIMD Support</a></li></ul></li><li><span class="toctext">Developer Documentation</span><ul><li><a class="toctext" href="../devdocs/reflection.html">Reflection and introspection</a></li><li><span class="toctext">Documentation of Julia&#39;s Internals</span><ul><li><a class="toctext" href="../devdocs/init.html">Initialization of the Julia runtime</a></li><li><a class="toctext" href="../devdocs/ast.html">Julia ASTs</a></li><li><a class="toctext" href="../devdocs/types.html">More about types</a></li><li><a class="toctext" href="../devdocs/object.html">Memory layout of Julia Objects</a></li><li><a class="toctext" href="../devdocs/eval.html">Eval of Julia code</a></li><li><a class="toctext" href="../devdocs/callconv.html">Calling Conventions</a></li><li><a class="toctext" href="../devdocs/compiler.html">High-level Overview of the Native-Code Generation Process</a></li><li><a class="toctext" href="../devdocs/functions.html">Julia Functions</a></li><li><a class="toctext" href="../devdocs/cartesian.html">Base.Cartesian</a></li><li><a class="toctext" href="../devdocs/meta.html">Talking to the compiler (the <code>:meta</code> mechanism)</a></li><li><a class="toctext" href="../devdocs/subarrays.html">SubArrays</a></li><li><a class="toctext" href="../devdocs/sysimg.html">System Image Building</a></li><li><a class="toctext" href="../devdocs/llvm.html">Working with LLVM</a></li><li><a class="toctext" href="../devdocs/stdio.html">printf() and stdio in the Julia runtime</a></li><li><a class="toctext" href="../devdocs/boundscheck.html">Bounds checking</a></li><li><a class="toctext" href="../devdocs/locks.html">Proper maintenance and care of multi-threading locks</a></li><li><a class="toctext" href="../devdocs/offset-arrays.html">Arrays with custom indices</a></li><li><a class="toctext" href="../devdocs/libgit2.html">Base.LibGit2</a></li><li><a class="toctext" href="../devdocs/require.html">Module loading</a></li></ul></li><li><span class="toctext">Developing/debugging Julia&#39;s C code</span><ul><li><a class="toctext" href="../devdocs/backtraces.html">Reporting and analyzing crashes (segfaults)</a></li><li><a class="toctext" href="../devdocs/debuggingtips.html">gdb debugging tips</a></li><li><a class="toctext" href="../devdocs/valgrind.html">Using Valgrind with Julia</a></li><li><a class="toctext" href="../devdocs/sanitizers.html">Sanitizer support</a></li></ul></li></ul></li></ul></nav><article id="docs"><header><nav><ul><li>Standard Library</li><li><a href="iterators.html">Iteration utilities</a></li></ul><a class="edit-page" href="https://github.com/JuliaLang/julia/blob/master/doc/src/stdlib/iterators.md"><span class="fa"></span> Edit on GitHub</a></nav><hr/><div id="topbar"><span>Iteration utilities</span><a class="fa fa-bars" href="#"></a></div></header><h1><a class="nav-anchor" id="Iteration-utilities-1" href="#Iteration-utilities-1">Iteration utilities</a></h1><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.zip" href="#Base.Iterators.zip"><code>Base.Iterators.zip</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">zip(iters...)</code></pre><p>For a set of iterable objects, returns an iterable of tuples, where the <code>i</code>th tuple contains the <code>i</code>th component of each input iterable.</p><p>Note that <a href="iterators.html#Base.Iterators.zip"><code>zip</code></a> is its own inverse: <code>collect(zip(zip(a...)...)) == collect(a)</code>.</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; a = 1:5
1:5
julia&gt; b = [&quot;e&quot;,&quot;d&quot;,&quot;b&quot;,&quot;c&quot;,&quot;a&quot;]
5-element Array{String,1}:
&quot;e&quot;
&quot;d&quot;
&quot;b&quot;
&quot;c&quot;
&quot;a&quot;
julia&gt; c = zip(a,b)
Base.Iterators.Zip2{UnitRange{Int64},Array{String,1}}(1:5, String[&quot;e&quot;, &quot;d&quot;, &quot;b&quot;, &quot;c&quot;, &quot;a&quot;])
julia&gt; length(c)
5
julia&gt; first(c)
(1, &quot;e&quot;)</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L201-L232">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.enumerate" href="#Base.Iterators.enumerate"><code>Base.Iterators.enumerate</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">enumerate(iter)</code></pre><p>An iterator that yields <code>(i, x)</code> where <code>i</code> is a counter starting at 1, and <code>x</code> is the <code>i</code>th value from the given iterator. It&#39;s useful when you need not only the values <code>x</code> over which you are iterating, but also the number of iterations so far. Note that <code>i</code> may not be valid for indexing <code>iter</code>; it&#39;s also possible that <code>x != iter[i]</code>, if <code>iter</code> has indices that do not start at 1. See the <code>enumerate(IndexLinear(), iter)</code> method if you want to ensure that <code>i</code> is an index.</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; a = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];
julia&gt; for (index, value) in enumerate(a)
println(&quot;$index $value&quot;)
end
1 a
2 b
3 c</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L35-L58">source</a><div><pre><code class="language-none">enumerate(IndexLinear(), A)
enumerate(IndexCartesian(), A)
enumerate(IndexStyle(A), A)</code></pre><p>An iterator that accesses each element of the array <code>A</code>, returning <code>(i, x)</code>, where <code>i</code> is the index for the element and <code>x = A[i]</code>. This is similar to <code>enumerate(A)</code>, except <code>i</code> will always be a valid index for <code>A</code>.</p><p>Specifying <code>IndexLinear()</code> ensures that <code>i</code> will be an integer; specifying <code>IndexCartesian()</code> ensures that <code>i</code> will be a <code>CartesianIndex</code>; specifying <code>IndexStyle(A)</code> chooses whichever has been defined as the native indexing style for array <code>A</code>.</p><p><strong>Examples</strong></p><pre><code class="language-julia-repl">julia&gt; A = [&quot;a&quot; &quot;d&quot;; &quot;b&quot; &quot;e&quot;; &quot;c&quot; &quot;f&quot;];
julia&gt; for (index, value) in enumerate(IndexStyle(A), A)
println(&quot;$index $value&quot;)
end
1 a
2 b
3 c
4 d
5 e
6 f
julia&gt; S = view(A, 1:2, :);
julia&gt; for (index, value) in enumerate(IndexStyle(S), S)
println(&quot;$index $value&quot;)
end
CartesianIndex{2}((1, 1)) a
CartesianIndex{2}((2, 1)) b
CartesianIndex{2}((1, 2)) d
CartesianIndex{2}((2, 2)) e</code></pre><p>Note that <code>enumerate(A)</code> returns <code>i</code> as a <em>counter</em> (always starting at 1), whereas <code>enumerate(IndexLinear(), A)</code> returns <code>i</code> as an <em>index</em> (starting at the first linear index of <code>A</code>, which may or may not be 1).</p><p>See also: <a href="arrays.html#Base.IndexStyle"><code>IndexStyle</code></a>, <a href="arrays.html#Base.indices-Tuple{Any}"><code>indices</code></a>.</p></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L80-L127">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.rest" href="#Base.Iterators.rest"><code>Base.Iterators.rest</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">rest(iter, state)</code></pre><p>An iterator that yields the same elements as <code>iter</code>, but starting at the given <code>state</code>.</p></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L312-L316">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.countfrom" href="#Base.Iterators.countfrom"><code>Base.Iterators.countfrom</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">countfrom(start=1, step=1)</code></pre><p>An iterator that counts forever, starting at <code>start</code> and incrementing by <code>step</code>.</p></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L337-L341">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.take" href="#Base.Iterators.take"><code>Base.Iterators.take</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">take(iter, n)</code></pre><p>An iterator that generates at most the first <code>n</code> elements of <code>iter</code>.</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; a = 1:2:11
1:2:11
julia&gt; collect(a)
6-element Array{Int64,1}:
1
3
5
7
9
11
julia&gt; collect(Iterators.take(a,3))
3-element Array{Int64,1}:
1
3
5</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L361-L387">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.drop" href="#Base.Iterators.drop"><code>Base.Iterators.drop</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">drop(iter, n)</code></pre><p>An iterator that generates all but the first <code>n</code> elements of <code>iter</code>.</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; a = 1:2:11
1:2:11
julia&gt; collect(a)
6-element Array{Int64,1}:
1
3
5
7
9
11
julia&gt; collect(Iterators.drop(a,4))
2-element Array{Int64,1}:
9
11</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L418-L443">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.cycle" href="#Base.Iterators.cycle"><code>Base.Iterators.cycle</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">cycle(iter)</code></pre><p>An iterator that cycles through <code>iter</code> forever.</p></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L477-L481">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.repeated" href="#Base.Iterators.repeated"><code>Base.Iterators.repeated</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">repeated(x[, n::Int])</code></pre><p>An iterator that generates the value <code>x</code> forever. If <code>n</code> is specified, generates <code>x</code> that many times (equivalent to <code>take(repeated(x), n)</code>).</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; a = Iterators.repeated([1 2], 4);
julia&gt; collect(a)
4-element Array{Array{Int64,2},1}:
[1 2]
[1 2]
[1 2]
[1 2]</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L512-L530">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.product" href="#Base.Iterators.product"><code>Base.Iterators.product</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">product(iters...)</code></pre><p>Returns an iterator over the product of several iterators. Each generated element is a tuple whose <code>i</code>th element comes from the <code>i</code>th argument iterator. The first iterator changes the fastest. Example:</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; collect(Iterators.product(1:2,3:5))
2×3 Array{Tuple{Int64,Int64},2}:
(1, 3) (1, 4) (1, 5)
(2, 3) (2, 4) (2, 5)</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L602-L617">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.flatten" href="#Base.Iterators.flatten"><code>Base.Iterators.flatten</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">flatten(iter)</code></pre><p>Given an iterator that yields iterators, return an iterator that yields the elements of those iterators. Put differently, the elements of the argument iterator are concatenated.</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; collect(Iterators.flatten((1:2, 8:9)))
4-element Array{Int64,1}:
1
2
8
9</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L681-L698">source</a></section><section class="docstring"><div class="docstring-header"><a class="docstring-binding" id="Base.Iterators.partition" href="#Base.Iterators.partition"><code>Base.Iterators.partition</code></a><span class="docstring-category">Function</span>.</div><div><pre><code class="language-none">partition(collection, n)</code></pre><p>Iterate over a collection <code>n</code> elements at a time.</p><p><strong>Example</strong></p><pre><code class="language-julia-repl">julia&gt; collect(Iterators.partition([1,2,3,4,5], 2))
3-element Array{Array{Int64,1},1}:
[1, 2]
[3, 4]
[5]</code></pre></div><a class="source-link" target="_blank" href="https://github.com/JuliaLang/julia/blob/d55cadc350d426a95fd967121ba77494d08364c8/base/iterators.jl#L738-L752">source</a></section><footer><hr/><a class="previous" href="dates.html"><span class="direction">Previous</span><span class="title">Dates and Time</span></a><a class="next" href="test.html"><span class="direction">Next</span><span class="title">Unit Testing</span></a></footer></article></body></html>