---
layout: "default"
title: "Sequence"
description: "Swift documentation for 'Sequence': A type that provides sequential, iterated access to its elements."
keywords: "Sequence,protocol,swift,documentation,drop,dropFirst,dropLast,filter,forEach,makeIterator,map,prefix,prefix,split,suffix,underestimatedCount,Iterator,SubSequence"
root: "/v3.1"
---

<div class="intro-declaration"><code class="language-swift">protocol Sequence</code></div>

<div class="discussion comment">
    <p>A type that provides sequential, iterated access to its elements.</p>

<p>A sequence is a list of values that you can step through one at a time. The
most common way to iterate over the elements of a sequence is to use a
<code>for</code>-<code>in</code> loop:</p>

<pre><code class="language-swift">let oneTwoThree = 1...3
for number in oneTwoThree {
    print(number)
}
// Prints &quot;1&quot;
// Prints &quot;2&quot;
// Prints &quot;3&quot;</code></pre>

<p>While seemingly simple, this capability gives you access to a large number
of operations that you can perform on any sequence. As an example, to
check whether a sequence includes a particular value, you can test each
value sequentially until you&#39;ve found a match or reached the end of the
sequence. This example checks to see whether a particular insect is in an
array.</p>

<pre><code class="language-swift">let bugs = [&quot;Aphid&quot;, &quot;Bumblebee&quot;, &quot;Cicada&quot;, &quot;Damselfly&quot;, &quot;Earwig&quot;]
var hasMosquito = false
for bug in bugs {
    if bug == &quot;Mosquito&quot; {
        hasMosquito = true
        break
    }
}
print(&quot;&#39;bugs&#39; has a mosquito: \(hasMosquito)&quot;)
// Prints &quot;&#39;bugs&#39; has a mosquito: false&quot;</code></pre>

<p>The <code>Sequence</code> protocol provides default implementations for many common
operations that depend on sequential access to a sequence&#39;s values. For
clearer, more concise code, the example above could use the array&#39;s
<code>contains(_:)</code> method, which every sequence inherits from <code>Sequence</code>,
instead of iterating manually:</p>

<pre><code class="language-swift">if bugs.contains(&quot;Mosquito&quot;) {
    print(&quot;Break out the bug spray.&quot;)
} else {
    print(&quot;Whew, no mosquitos!&quot;)
}
// Prints &quot;Whew, no mosquitos!&quot;</code></pre>

<h1>Repeated Access</h1>

<p>The <code>Sequence</code> protocol makes no requirement on conforming types regarding
whether they will be destructively consumed by iteration. As a
consequence, don&#39;t assume that multiple <code>for</code>-<code>in</code> loops on a sequence
will either resume iteration or restart from the beginning:</p>

<pre><code class="language-swift">for element in sequence {
    if ... some condition { break }
}

for element in sequence {
    // No defined behavior
}</code></pre>

<p>In this case, you cannot assume either that a sequence will be consumable
and will resume iteration, or that a sequence is a collection and will
restart iteration from the first element. A conforming sequence that is
not a collection is allowed to produce an arbitrary sequence of elements
in the second <code>for</code>-<code>in</code> loop.</p>

<p>To establish that a type you&#39;ve created supports nondestructive iteration,
add conformance to the <code>Collection</code> protocol.</p>

<h1>Conforming to the Sequence Protocol</h1>

<p>Making your own custom types conform to <code>Sequence</code> enables many useful
operations, like <code>for</code>-<code>in</code> looping and the <code>contains</code> method, without
much effort. To add <code>Sequence</code> conformance to your own custom type, add a
<code>makeIterator()</code> method that returns an iterator.</p>

<p>Alternatively, if your type can act as its own iterator, implementing the
requirements of the <code>IteratorProtocol</code> protocol and declaring conformance
to both <code>Sequence</code> and <code>IteratorProtocol</code> are sufficient.</p>

<p>Here&#39;s a definition of a <code>Countdown</code> sequence that serves as its own
iterator. The <code>makeIterator()</code> method is provided as a default
implementation.</p>

<pre><code class="language-swift">struct Countdown: Sequence, IteratorProtocol {
    var count: Int

    mutating func next() -&gt; Int? {
        if count == 0 {
            return nil
        } else {
            defer { count -= 1 }
            return count
        }
    }
}

let threeToGo = Countdown(count: 3)
for i in threeToGo {
    print(i)
}
// Prints &quot;3&quot;
// Prints &quot;2&quot;
// Prints &quot;1&quot;</code></pre>

<h1>Expected Performance</h1>

<p>A sequence should provide its iterator in O(1). The <code>Sequence</code> protocol
makes no other requirements about element access, so routines that
traverse a sequence should be considered O(<em>n</em>) unless documented
otherwise.</p>

<p><strong>See Also:</strong> <code>IteratorProtocol</code>, <code>Collection</code></p>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>

<tr>
<th id="aliases">Associated Types</th>
<td>
<span id="aliasesmark"></span>
<div class="declaration">
<code class="language-swift">Iterator : IteratorProtocol</code>
<div class="comment">
    <p>A type that provides the sequence&#39;s iteration interface and
encapsulates its iteration state.</p>
</div>
</div>
<div class="declaration">
<code class="language-swift">SubSequence</code>
<div class="comment">
    <p>A type that represents a subsequence of some of the sequence&#39;s elements.</p>
</div>
</div>
</td>
</tr>


<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>




<h3>Instance Variables</h3>
<div class="declaration" id="var-underestimatedcount_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-underestimatedcount_-int">var underestimatedCount: Int</a><div class="comment collapse" id="comment-var-underestimatedcount_-int"><div class="p">
    <p>A value less than or equal to the number of elements in
the sequence, calculated nondestructively.</p>

<p><strong>Complexity:</strong> O(1)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var underestimatedCount: Int { get }</code>

    </div></div>
</div>



<h3>Instance Methods</h3>
<div class="declaration" id="func-drop-while_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-drop-while_">func drop(<wbr>while:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-drop-while_"><div class="p">
    <p>Returns a subsequence by skipping elements while <code>predicate</code> returns
<code>true</code> and returning the remaining elements.</p>

<p><strong><code>predicate</code>:</strong>  A closure that takes an element of the
  sequence as its argument and returns a Boolean value indicating
  whether the element is a match.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the collection.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func drop(while predicate: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-dropfirst_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-dropfirst_">func dropFirst(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-dropfirst_"><div class="p">
    <p>Returns a subsequence containing all but the given number of initial
elements.</p>

<p>If the number of elements to drop exceeds the number of elements in
the sequence, the result is an empty subsequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropFirst(2))
// Prints &quot;[3, 4, 5]&quot;
print(numbers.dropFirst(10))
// Prints &quot;[]&quot;</code></pre>

<p><strong><code>n</code>:</strong>  The number of elements to drop from the beginning of
  the sequence. <code>n</code> must be greater than or equal to zero.
<strong>Returns:</strong> A subsequence starting after the specified number of
  elements.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the number of elements to drop from
  the beginning of the sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func dropFirst(_ n: Int) -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-droplast_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-droplast_">func dropLast(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-droplast_"><div class="p">
    <p>Returns a subsequence containing all but the specified number of final
elements.</p>

<p>The sequence must be finite. If the number of elements to drop exceeds
the number of elements in the sequence, the result is an empty
subsequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropLast(2))
// Prints &quot;[1, 2, 3]&quot;
print(numbers.dropLast(10))
// Prints &quot;[]&quot;</code></pre>

<p><strong><code>n</code>:</strong>  The number of elements to drop off the end of the
  sequence. <code>n</code> must be greater than or equal to zero.
<strong>Returns:</strong> A subsequence leaving off the specified number of elements.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func dropLast(_ n: Int) -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-filter_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-filter_">func filter(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-filter_"><div class="p">
    <p>Returns an array containing, in order, the elements of the sequence
that satisfy the given predicate.</p>

<p>In this example, <code>filter</code> is used to include only names shorter than
five characters.</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let shortNames = cast.filter { $0.characters.count &lt; 5 }
print(shortNames)
// Prints &quot;[&quot;Kim&quot;, &quot;Karl&quot;]&quot;</code></pre>

<p><strong><code>isIncluded</code>:</strong>  A closure that takes an element of the
  sequence as its argument and returns a Boolean value indicating
  whether the element should be included in the returned array.
<strong>Returns:</strong> An array of the elements that <code>includeElement</code> allowed.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func filter(_ isIncluded: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; [Self.Iterator.Element]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-foreach_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-foreach_">func forEach(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-foreach_"><div class="p">
    <p>Calls the given closure on each element in the sequence in the same order
as a <code>for</code>-<code>in</code> loop.</p>

<p>The two loops in the following example produce the same output:</p>

<pre><code class="language-swift">let numberWords = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;]
for word in numberWords {
    print(word)
}
// Prints &quot;one&quot;
// Prints &quot;two&quot;
// Prints &quot;three&quot;

numberWords.forEach { word in
    print(word)
}
// Same as above</code></pre>

<p>Using the <code>forEach</code> method is distinct from a <code>for</code>-<code>in</code> loop in two
important ways:</p>

<ol><li>You cannot use a <code>break</code> or <code>continue</code> statement to exit the current
call of the <code>body</code> closure or skip subsequent calls.</li><li>Using the <code>return</code> statement in the <code>body</code> closure will exit only from
the current call to <code>body</code>, not from any outer scope, and won&#39;t skip
subsequent calls.</li></ol>

<p><strong><code>body</code>:</strong>  A closure that takes an element of the sequence as a
  parameter.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func forEach(_ body: (Self.Iterator.Element) throws -&gt; Swift.Void) rethrows</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-makeiterator">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-makeiterator">func makeIterator()</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-makeiterator"><div class="p">
    <p>Returns an iterator over the elements of this sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func makeIterator() -&gt; Self.Iterator</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-map_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-map_">func map(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-map_"><div class="p">
    <p>Returns an array containing the results of mapping the given closure
over the sequence&#39;s elements.</p>

<p>In this example, <code>map</code> is used first to convert the names in the array
to lowercase strings and then to count their characters.</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let lowercaseNames = cast.map { $0.lowercaseString }
// &#39;lowercaseNames&#39; == [&quot;vivien&quot;, &quot;marlon&quot;, &quot;kim&quot;, &quot;karl&quot;]
let letterCounts = cast.map { $0.characters.count }
// &#39;letterCounts&#39; == [6, 6, 3, 4]</code></pre>

<p><strong><code>transform</code>:</strong>  A mapping closure. <code>transform</code> accepts an
  element of this sequence as its parameter and returns a transformed
  value of the same or of a different type.
<strong>Returns:</strong> An array containing the transformed elements of this
  sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func map&lt;T&gt;(_ transform: (Self.Iterator.Element) throws -&gt; T) rethrows -&gt; [T]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-prefix_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-prefix_">func prefix(<wbr>_:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-prefix_"><div class="p">
    <p>Returns a subsequence, up to the specified maximum length, containing
the initial elements of the sequence.</p>

<p>If the maximum length exceeds the number of elements in the sequence,
the result contains all the elements in the sequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints &quot;[1, 2]&quot;
print(numbers.prefix(10))
// Prints &quot;[1, 2, 3, 4, 5]&quot;</code></pre>

<p><strong><code>maxLength</code>:</strong>  The maximum number of elements to return.
  <code>maxLength</code> must be greater than or equal to zero.
<strong>Returns:</strong> A subsequence starting at the beginning of this sequence
  with at most <code>maxLength</code> elements.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func prefix(_ maxLength: Int) -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-prefix-while_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-prefix-while_">func prefix(<wbr>while:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-prefix-while_"><div class="p">
    <p>Returns a subsequence containing the initial, consecutive elements that
satisfy the given predicate.</p>

<p>The following example uses the <code>prefix(while:)</code> method to find the
positive numbers at the beginning of the <code>numbers</code> array. Every element
of <code>numbers</code> up to, but not including, the first negative value is
included in the result.</p>

<pre><code class="language-swift">let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let positivePrefix = numbers.prefix(while: { $0 &gt; 0 })
// positivePrefix == [3, 7, 4]</code></pre>

<p>If <code>predicate</code> matches every element in the sequence, the resulting
sequence contains every element of the sequence.</p>

<p><strong><code>predicate</code>:</strong>  A closure that takes an element of the sequence as
  its argument and returns a Boolean value indicating whether the
  element should be included in the result.
<strong>Returns:</strong> A subsequence of the initial, consecutive elements that
  satisfy <code>predicate</code>.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the collection.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func prefix(while predicate: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-split_omittingemptysubsequences_whereseparator_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-split_omittingemptysubsequences_whereseparator_">func split(<wbr>_:<wbr>omittingEmptySubsequences:<wbr>whereSeparator:)</a>
        
<div class="comment collapse" id="comment-func-split_omittingemptysubsequences_whereseparator_"><div class="p">
    <p>Returns the longest possible subsequences of the sequence, in order, that
don&#39;t contain elements satisfying the given predicate.</p>

<p>The resulting array consists of at most <code>maxSplits + 1</code> subsequences.
Elements that are used to split the sequence are not returned as part of
any subsequence.</p>

<p>The following examples show the effects of the <code>maxSplits</code> and
<code>omittingEmptySubsequences</code> parameters when splitting a string using a
closure that matches spaces. The first use of <code>split</code> returns each word
that was originally separated by one or more spaces.</p>

<pre><code class="language-swift">let line = &quot;BLANCHE:   I don&#39;t want realism. I want magic!&quot;
print(line.characters.split(whereSeparator: { $0 == &quot; &quot; })
                     .map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;I&quot;, &quot;don\&#39;t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;</code></pre>

<p>The second example passes <code>1</code> for the <code>maxSplits</code> parameter, so the
original string is split just once, into two new strings.</p>

<pre><code class="language-swift">print(
    line.characters.split(maxSplits: 1, whereSeparator: { $0 == &quot; &quot; })
                   .map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;  I don\&#39;t want realism. I want magic!&quot;]&quot;</code></pre>

<p>The final example passes <code>false</code> for the <code>omittingEmptySubsequences</code>
parameter, so the returned array contains empty strings where spaces
were repeated.</p>

<pre><code class="language-swift">print(
    line.characters.split(
        omittingEmptySubsequences: false, 
        whereSeparator: { $0 == &quot; &quot; })
    ).map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;&quot;, &quot;&quot;, &quot;I&quot;, &quot;don\&#39;t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;</code></pre>

<p><strong>Parameters:</strong>
  <strong>maxSplits:</strong> The maximum number of times to split the sequence, or one
    less than the number of subsequences to return. If <code>maxSplits + 1</code>
    subsequences are returned, the last one is a suffix of the original
    sequence containing the remaining elements. <code>maxSplits</code> must be
    greater than or equal to zero. The default value is <code>Int.max</code>.
  <strong>omittingEmptySubsequences:</strong> If <code>false</code>, an empty subsequence is
    returned in the result for each pair of consecutive elements
    satisfying the <code>isSeparator</code> predicate and for each element at the
    start or end of the sequence satisfying the <code>isSeparator</code> predicate.
    If <code>true</code>, only nonempty subsequences are returned. The default
    value is <code>true</code>.
  <strong>isSeparator:</strong> A closure that returns <code>true</code> if its argument should be
    used to split the sequence; otherwise, <code>false</code>.
<strong>Returns:</strong> An array of subsequences, split from this sequence&#39;s elements.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator isSeparator: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; [Self.SubSequence]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-suffix_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-suffix_">func suffix(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-suffix_"><div class="p">
    <p>Returns a subsequence, up to the given maximum length, containing the
final elements of the sequence.</p>

<p>The sequence must be finite. If the maximum length exceeds the number
of elements in the sequence, the result contains all the elements in
the sequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.suffix(2))
// Prints &quot;[4, 5]&quot;
print(numbers.suffix(10))
// Prints &quot;[1, 2, 3, 4, 5]&quot;</code></pre>

<p><strong><code>maxLength</code>:</strong>  The maximum number of elements to return. The
  value of <code>maxLength</code> must be greater than or equal to zero.
<strong>Returns:</strong> A subsequence terminating at the end of this sequence with
  at most <code>maxLength</code> elements.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func suffix(_ maxLength: Int) -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>


<h3>Default Implementations</h3>





<div class="declaration" id="-var-lazy_-lazysequence-self">
<a class="toggle-link" data-toggle="collapse" href="#comment--var-lazy_-lazysequence-self">var lazy: LazySequence&lt;Self&gt;</a><div class="comment collapse" id="comment--var-lazy_-lazysequence-self"><div class="p">
    <p>A sequence containing the same elements as this sequence,
but on which some operations, such as <code>map</code> and <code>filter</code>, are
implemented lazily.</p>

<p><strong>See Also:</strong> <code>LazySequenceProtocol</code>, <code>LazySequence</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">var lazy: LazySequence&lt;Self&gt; { get }</code>

    </div></div>
</div>
<div class="declaration" id="-var-underestimatedcount_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment--var-underestimatedcount_-int">var underestimatedCount: Int</a><div class="comment collapse" id="comment--var-underestimatedcount_-int"><div class="p">
    <p>Returns a value less than or equal to the number of elements in
the sequence, nondestructively.</p>

<p><strong>Complexity:</strong> O(<em>n</em>)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var underestimatedCount: Int { get }</code>

    </div></div>
</div>


<div class="declaration" id="func--contains-where_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--contains-where_">func contains(<wbr>where:)</a>
        
<div class="comment collapse" id="comment-func--contains-where_"><div class="p">
    <p>Returns a Boolean value indicating whether the sequence contains an
element that satisfies the given predicate.</p>

<p>You can use the predicate to check for an element of a type that
doesn&#39;t conform to the <code>Equatable</code> protocol, such as the
<code>HTTPResponse</code> enumeration in this example.</p>

<pre><code class="language-swift">enum HTTPResponse {
    case ok
    case error(Int)
}

let lastThreeResponses: [HTTPResponse] = [.ok, .ok, .error(404)]
let hadError = lastThreeResponses.contains { element in
    if case .error = element {
        return true
    } else {
        return false
    }
}
// &#39;hadError&#39; == true</code></pre>

<p>Alternatively, a predicate can be satisfied by a range of <code>Equatable</code>
elements or a general condition. This example shows how you can check an
array for an expense greater than $100.</p>

<pre><code class="language-swift">let expenses = [21.37, 55.21, 9.32, 10.18, 388.77, 11.41]
let hasBigPurchase = expenses.contains { $0 &gt; 100 }
// &#39;hasBigPurchase&#39; == true</code></pre>

<p><strong><code>predicate</code>:</strong>  A closure that takes an element of the sequence
  as its argument and returns a Boolean value that indicates whether
  the passed element represents a match.
<strong>Returns:</strong> <code>true</code> if the sequence contains an element that satisfies
  <code>predicate</code>; otherwise, <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func contains(where predicate: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--dropfirst">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--dropfirst">func dropFirst()</a>
        
<div class="comment collapse" id="comment-func--dropfirst"><div class="p">
    <p>Returns a subsequence containing all but the first element of the
sequence.</p>

<p>The following example drops the first element from an array of integers.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropFirst())
// Prints &quot;[2, 3, 4, 5]&quot;</code></pre>

<p>If the sequence has no elements, the result is an empty subsequence.</p>

<pre><code class="language-swift">let empty: [Int] = []
print(empty.dropFirst())
// Prints &quot;[]&quot;</code></pre>

<p><strong>Returns:</strong> A subsequence starting after the first element of the
  sequence.</p>

<p><strong>Complexity:</strong> O(1)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func dropFirst() -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--droplast">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--droplast">func dropLast()</a>
        
<div class="comment collapse" id="comment-func--droplast"><div class="p">
    <p>Returns a subsequence containing all but the last element of the
sequence.</p>

<p>The sequence must be finite. </p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropLast())
// Prints &quot;[1, 2, 3, 4]&quot;</code></pre>

<p>If the sequence has no elements, the result is an empty subsequence.</p>

<pre><code class="language-swift">let empty: [Int] = []
print(empty.dropLast())
// Prints &quot;[]&quot;</code></pre>

<p><strong>Returns:</strong> A subsequence leaving off the last element of the sequence.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func dropLast() -&gt; Self.SubSequence</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--elementsequal_by_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--elementsequal_by_">func elementsEqual(<wbr>_:<wbr>by:)</a>
        
<div class="comment collapse" id="comment-func--elementsequal_by_"><div class="p">
    <p>Returns a Boolean value indicating whether this sequence and another
sequence contain equivalent elements, using the given predicate as the
equivalence test.</p>

<p>At least one of the sequences must be finite.</p>

<p>The predicate must be a <em>equivalence relation</em> over the elements. That
is, for any elements <code>a</code>, <code>b</code>, and <code>c</code>, the following conditions must
hold:</p>

<ul><li><code>areEquivalent(a, a)</code> is always <code>true</code>. (Reflexivity)</li><li><code>areEquivalent(a, b)</code> implies <code>areEquivalent(b, a)</code>. (Symmetry)</li><li>If <code>areEquivalent(a, b)</code> and <code>areEquivalent(b, c)</code> are both <code>true</code>, then
<code>areEquivalent(a, c)</code> is also <code>true</code>. (Transitivity)</li></ul>

<p><strong>Parameters:</strong>
  <strong>other:</strong> A sequence to compare to this sequence.
  <strong>areEquivalent:</strong> A predicate that returns <code>true</code> if its two arguments
    are equivalent; otherwise, <code>false</code>.
<strong>Returns:</strong> <code>true</code> if this sequence and <code>other</code> contain equivalent items,
  using <code>areEquivalent</code> as the equivalence test; otherwise, <code>false.</code></p>

<p><strong>See Also:</strong> <code>elementsEqual(_:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func elementsEqual&lt;OtherSequence where OtherSequence : Sequence, OtherSequence.Iterator.Element == Self.Iterator.Element&gt;(_ other: OtherSequence, by areEquivalent: (Self.Iterator.Element, Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--enumerated">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--enumerated">func enumerated()</a>
        
<div class="comment collapse" id="comment-func--enumerated"><div class="p">
    <p>Returns a sequence of pairs (<em>n</em>, <em>x</em>), where <em>n</em> represents a
consecutive integer starting at zero, and <em>x</em> represents an element of
the sequence.</p>

<p>This example enumerates the characters of the string &quot;Swift&quot; and prints
each character along with its place in the string.</p>

<pre><code class="language-swift">for (n, c) in &quot;Swift&quot;.characters.enumerated() {
    print(&quot;\(n): &#39;\(c)&#39;&quot;)
}
// Prints &quot;0: &#39;S&#39;&quot;
// Prints &quot;1: &#39;w&#39;&quot;
// Prints &quot;2: &#39;i&#39;&quot;
// Prints &quot;3: &#39;f&#39;&quot;
// Prints &quot;4: &#39;t&#39;&quot;</code></pre>

<p>When enumerating a collection, the integer part of each pair is a counter
for the enumeration, not necessarily the index of the paired value.
These counters can only be used as indices in instances of zero-based,
integer-indexed collections, such as <code>Array</code> and <code>ContiguousArray</code>. For
other collections the counters may be out of range or of the wrong type
to use as an index. To iterate over the elements of a collection with its
indices, use the <code>zip(_:_:)</code> function.</p>

<p>This example iterates over the indices and elements of a set, building a
list of indices of names with five or fewer letters.</p>

<pre><code class="language-swift">let names: Set = [&quot;Sofia&quot;, &quot;Camilla&quot;, &quot;Martina&quot;, &quot;Mateo&quot;, &quot;Nicolás&quot;]
var shorterIndices: [SetIndex&lt;String&gt;] = []
for (i, name) in zip(names.indices, names) {
    if name.characters.count &lt;= 5 {
        shorterIndices.append(i)
    }
}</code></pre>

<p>Now that the <code>shorterIndices</code> array holds the indices of the shorter
names in the <code>names</code> set, you can use those indices to access elements in
the set.</p>

<pre><code class="language-swift">for i in shorterIndices {
    print(names[i])
}
// Prints &quot;Sofia&quot;
// Prints &quot;Mateo&quot;</code></pre>

<p><strong>Returns:</strong> A sequence of pairs enumerating the sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func enumerated() -&gt; EnumeratedSequence&lt;Self&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--filter_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--filter_">func filter(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func--filter_"><div class="p">
    <p>Returns an array containing, in order, the elements of the sequence
that satisfy the given predicate.</p>

<p>In this example, <code>filter</code> is used to include only names shorter than
five characters.</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let shortNames = cast.filter { $0.characters.count &lt; 5 }
print(shortNames)
// Prints &quot;[&quot;Kim&quot;, &quot;Karl&quot;]&quot;</code></pre>

<p><strong><code>isIncluded</code>:</strong>  A closure that takes an element of the
  sequence as its argument and returns a Boolean value indicating
  whether the element should be included in the returned array.
<strong>Returns:</strong> An array of the elements that <code>includeElement</code> allowed.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func filter(_ isIncluded: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; [Self.Iterator.Element]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--first-where_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--first-where_">func first(<wbr>where:)</a>
        
<div class="comment collapse" id="comment-func--first-where_"><div class="p">
    <p>Returns the first element of the sequence that satisfies the given
predicate.</p>

<p>The following example uses the <code>first(where:)</code> method to find the first
negative number in an array of integers:</p>

<pre><code class="language-swift">let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
if let firstNegative = numbers.first(where: { $0 &lt; 0 }) {
    print(&quot;The first negative number is \(firstNegative).&quot;)
}
// Prints &quot;The first negative number is -2.&quot;</code></pre>

<p><strong><code>predicate</code>:</strong>  A closure that takes an element of the sequence as
  its argument and returns a Boolean value indicating whether the
  element is a match.
<strong>Returns:</strong> The first element of the sequence that satisfies <code>predicate</code>,
  or <code>nil</code> if there is no element that satisfies <code>predicate</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func first(where predicate: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Self.Iterator.Element?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--flatmap-elementofresult_-self-iterator-element-throws-elementofresult">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--flatmap-elementofresult_-self-iterator-element-throws-elementofresult">func flatMap&lt;ElementOfResult&gt;(<wbr>_: (Self.Iterator.Element) throws -&gt; ElementOfResult?)</a>
        
<div class="comment collapse" id="comment-func--flatmap-elementofresult_-self-iterator-element-throws-elementofresult"><div class="p">
    <p>Returns an array containing the non-<code>nil</code> results of calling the given
transformation with each element of this sequence.</p>

<p>Use this method to receive an array of nonoptional values when your
transformation produces an optional value.</p>

<p>In this example, note the difference in the result of using <code>map</code> and
<code>flatMap</code> with a transformation that returns an optional <code>Int</code> value.</p>

<pre><code class="language-swift">let possibleNumbers = [&quot;1&quot;, &quot;2&quot;, &quot;three&quot;, &quot;///4///&quot;, &quot;5&quot;]

let mapped: [Int?] = possibleNumbers.map { str in Int(str) }
// [1, 2, nil, nil, 5]

let flatMapped: [Int] = possibleNumbers.flatMap { str in Int(str) }
// [1, 2, 5]</code></pre>

<p><strong><code>transform</code>:</strong>  A closure that accepts an element of this
  sequence as its argument and returns an optional value.
<strong>Returns:</strong> An array of the non-<code>nil</code> results of calling <code>transform</code>
  with each element of the sequence.</p>

<p><strong>Complexity:</strong> O(<em>m</em> + <em>n</em>), where <em>m</em> is the length of this sequence
  and <em>n</em> is the length of the result.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func flatMap&lt;ElementOfResult&gt;(_ transform: (Self.Iterator.Element) throws -&gt; ElementOfResult?) rethrows -&gt; [ElementOfResult]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--flatmap-segmentofresult-where-segmentofresult_-sequence_-self-iterator-element-throws-segmentofresult">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--flatmap-segmentofresult-where-segmentofresult_-sequence_-self-iterator-element-throws-segmentofresult">func <wbr>flatMap&lt;SegmentOfResult where SegmentOfResult : Sequence&gt;(<wbr>_: (Self.Iterator.Element) throws -&gt; SegmentOfResult)</a>
        
<div class="comment collapse" id="comment-func--flatmap-segmentofresult-where-segmentofresult_-sequence_-self-iterator-element-throws-segmentofresult"><div class="p">
    <p>Returns an array containing the concatenated results of calling the
given transformation with each element of this sequence.</p>

<p>Use this method to receive a single-level collection when your
transformation produces a sequence or collection for each element.</p>

<p>In this example, note the difference in the result of using <code>map</code> and
<code>flatMap</code> with a transformation that returns an array.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4]

let mapped = numbers.map { Array(count: $0, repeatedValue: $0) }
// [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]

let flatMapped = numbers.flatMap { Array(count: $0, repeatedValue: $0) }
// [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]</code></pre>

<p>In fact, <code>s.flatMap(transform)</code>  is equivalent to
<code>Array(s.map(transform).joined())</code>.</p>

<p><strong><code>transform</code>:</strong>  A closure that accepts an element of this
  sequence as its argument and returns a sequence or collection.
<strong>Returns:</strong> The resulting flattened array.</p>

<p><strong>Complexity:</strong> O(<em>m</em> + <em>n</em>), where <em>m</em> is the length of this sequence
  and <em>n</em> is the length of the result.
<strong>See Also:</strong> <code>joined()</code>, <code>map(_:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func flatMap&lt;SegmentOfResult where SegmentOfResult : Sequence&gt;(_ transform: (Self.Iterator.Element) throws -&gt; SegmentOfResult) rethrows -&gt; [SegmentOfResult.Iterator.Element]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--foreach_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--foreach_">func forEach(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func--foreach_"><div class="p">
    <p>Calls the given closure on each element in the sequence in the same order
as a <code>for</code>-<code>in</code> loop.</p>

<p>The two loops in the following example produce the same output:</p>

<pre><code class="language-swift">let numberWords = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;]
for word in numberWords {
    print(word)
}
// Prints &quot;one&quot;
// Prints &quot;two&quot;
// Prints &quot;three&quot;

numberWords.forEach { word in
    print(word)
}
// Same as above</code></pre>

<p>Using the <code>forEach</code> method is distinct from a <code>for</code>-<code>in</code> loop in two
important ways:</p>

<ol><li>You cannot use a <code>break</code> or <code>continue</code> statement to exit the current
call of the <code>body</code> closure or skip subsequent calls.</li><li>Using the <code>return</code> statement in the <code>body</code> closure will exit only from
the current call to <code>body</code>, not from any outer scope, and won&#39;t skip
subsequent calls.</li></ol>

<p><strong><code>body</code>:</strong>  A closure that takes an element of the sequence as a
  parameter.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func forEach(_ body: (Self.Iterator.Element) throws -&gt; Swift.Void) rethrows</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--lexicographicallyprecedes_by_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--lexicographicallyprecedes_by_">func lexicographicallyPrecedes(<wbr>_:<wbr>by:)</a>
        
<div class="comment collapse" id="comment-func--lexicographicallyprecedes_by_"><div class="p">
    <p>Returns a Boolean value indicating whether the sequence precedes another
sequence in a lexicographical (dictionary) ordering, using the given
predicate to compare elements.</p>

<p>The predicate must be a <em>strict weak ordering</em> over the elements. That
is, for any elements <code>a</code>, <code>b</code>, and <code>c</code>, the following conditions must
hold:</p>

<ul><li><code>areInIncreasingOrder(a, a)</code> is always <code>false</code>. (Irreflexivity)</li><li>If <code>areInIncreasingOrder(a, b)</code> and <code>areInIncreasingOrder(b, c)</code> are
both <code>true</code>, then <code>areInIncreasingOrder(a, c)</code> is also
<code>true</code>. (Transitive comparability)</li><li>Two elements are <em>incomparable</em> if neither is ordered before the other
according to the predicate. If <code>a</code> and <code>b</code> are incomparable, and <code>b</code>
and <code>c</code> are incomparable, then <code>a</code> and <code>c</code> are also incomparable.
(Transitive incomparability)</li></ul>

<p><strong>Parameters:</strong>
  <strong>other:</strong> A sequence to compare to this sequence.
  <strong>areInIncreasingOrder:</strong>  A predicate that returns <code>true</code> if its first
    argument should be ordered before its second argument; otherwise,
    <code>false</code>.
<strong>Returns:</strong> <code>true</code> if this sequence precedes <code>other</code> in a dictionary
  ordering as ordered by <code>areInIncreasingOrder</code>; otherwise, <code>false</code>.</p>

<p><strong>Note:</strong> This method implements the mathematical notion of lexicographical
  ordering, which has no connection to Unicode.  If you are sorting
  strings to present to the end user, use <code>String</code> APIs that perform
  localized comparison instead.
<strong>See Also:</strong> <code>lexicographicallyPrecedes(_:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func lexicographicallyPrecedes&lt;OtherSequence where OtherSequence : Sequence, OtherSequence.Iterator.Element == Self.Iterator.Element&gt;(_ other: OtherSequence, by areInIncreasingOrder: (Self.Iterator.Element, Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--map_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--map_">func map(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func--map_"><div class="p">
    <p>Returns an array containing the results of mapping the given closure
over the sequence&#39;s elements.</p>

<p>In this example, <code>map</code> is used first to convert the names in the array
to lowercase strings and then to count their characters.</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let lowercaseNames = cast.map { $0.lowercaseString }
// &#39;lowercaseNames&#39; == [&quot;vivien&quot;, &quot;marlon&quot;, &quot;kim&quot;, &quot;karl&quot;]
let letterCounts = cast.map { $0.characters.count }
// &#39;letterCounts&#39; == [6, 6, 3, 4]</code></pre>

<p><strong><code>transform</code>:</strong>  A mapping closure. <code>transform</code> accepts an
  element of this sequence as its parameter and returns a transformed
  value of the same or of a different type.
<strong>Returns:</strong> An array containing the transformed elements of this
  sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func map&lt;T&gt;(_ transform: (Self.Iterator.Element) throws -&gt; T) rethrows -&gt; [T]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--max-by_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--max-by_">@warn_unqualified_access
     func max(<wbr>by:)</a>
        
<div class="comment collapse" id="comment-func--max-by_"><div class="p">
    <p>Returns the maximum element in the sequence, using the given predicate
as the comparison between elements.</p>

<p>The predicate must be a <em>strict weak ordering</em> over the elements. That
is, for any elements <code>a</code>, <code>b</code>, and <code>c</code>, the following conditions must
hold:</p>

<ul><li><code>areInIncreasingOrder(a, a)</code> is always <code>false</code>. (Irreflexivity)</li><li>If <code>areInIncreasingOrder(a, b)</code> and <code>areInIncreasingOrder(b, c)</code> are
both <code>true</code>, then <code>areInIncreasingOrder(a, c)</code> is also
<code>true</code>. (Transitive comparability)</li><li>Two elements are <em>incomparable</em> if neither is ordered before the other
according to the predicate. If <code>a</code> and <code>b</code> are incomparable, and <code>b</code>
and <code>c</code> are incomparable, then <code>a</code> and <code>c</code> are also incomparable.
(Transitive incomparability)</li></ul>

<p>This example shows how to use the <code>max(by:)</code> method on a
dictionary to find the key-value pair with the highest value.</p>

<pre><code class="language-swift">let hues = [&quot;Heliotrope&quot;: 296, &quot;Coral&quot;: 16, &quot;Aquamarine&quot;: 156]
let greatestHue = hues.max { a, b in a.value &lt; b.value }
print(greatestHue)
// Prints &quot;Optional((&quot;Heliotrope&quot;, 296))&quot;</code></pre>

<p><strong><code>areInIncreasingOrder</code>:</strong>   A predicate that returns <code>true</code> if its
  first argument should be ordered before its second argument;
  otherwise, <code>false</code>.
<strong>Returns:</strong> The sequence&#39;s maximum element if the sequence is not empty;
  otherwise, <code>nil</code>.</p>

<p><strong>See Also:</strong> <code>max()</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">@warn_unqualified_access
     func max(by areInIncreasingOrder: (Self.Iterator.Element, Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Self.Iterator.Element?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--min-by_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--min-by_">func min(<wbr>by:)</a>
        
<div class="comment collapse" id="comment-func--min-by_"><div class="p">
    <p>Returns the minimum element in the sequence, using the given predicate as
the comparison between elements.</p>

<p>The predicate must be a <em>strict weak ordering</em> over the elements. That
is, for any elements <code>a</code>, <code>b</code>, and <code>c</code>, the following conditions must
hold:</p>

<ul><li><code>areInIncreasingOrder(a, a)</code> is always <code>false</code>. (Irreflexivity)</li><li>If <code>areInIncreasingOrder(a, b)</code> and <code>areInIncreasingOrder(b, c)</code> are
both <code>true</code>, then <code>areInIncreasingOrder(a, c)</code> is also
<code>true</code>. (Transitive comparability)</li><li>Two elements are <em>incomparable</em> if neither is ordered before the other
according to the predicate. If <code>a</code> and <code>b</code> are incomparable, and <code>b</code>
and <code>c</code> are incomparable, then <code>a</code> and <code>c</code> are also incomparable.
(Transitive incomparability)</li></ul>

<p>This example shows how to use the <code>min(by:)</code> method on a
dictionary to find the key-value pair with the lowest value.</p>

<pre><code class="language-swift">let hues = [&quot;Heliotrope&quot;: 296, &quot;Coral&quot;: 16, &quot;Aquamarine&quot;: 156]
let leastHue = hues.min { a, b in a.value &lt; b.value }
print(leastHue)
// Prints &quot;Optional((&quot;Coral&quot;, 16))&quot;</code></pre>

<p><strong><code>areInIncreasingOrder</code>:</strong>  A predicate that returns <code>true</code>
  if its first argument should be ordered before its second
  argument; otherwise, <code>false</code>.
<strong>Returns:</strong> The sequence&#39;s minimum element, according to
  <code>areInIncreasingOrder</code>. If the sequence has no elements, returns
  <code>nil</code>.</p>

<p><strong>See Also:</strong> <code>min()</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func min(by areInIncreasingOrder: (Self.Iterator.Element, Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Self.Iterator.Element?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--reduce__">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--reduce__">func reduce(<wbr>_:<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func--reduce__"><div class="p">
    <p>Returns the result of combining the elements of the sequence using the
given closure.</p>

<p>Use the <code>reduce(_:_:)</code> method to produce a single value from the elements
of an entire sequence. For example, you can use this method on an array
of numbers to find their sum or product.</p>

<p>The <code>nextPartialResult</code> closure is called sequentially with an
accumulating value initialized to <code>initialResult</code> and each element of
the sequence. This example shows how to find the sum of an array of
numbers.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
    x + y
})
// numberSum == 10</code></pre>

<p>When <code>numbers.reduce(_:_:)</code> is called, the following steps occur:</p>

<ol><li>The <code>nextPartialResult</code> closure is called with <code>initialResult</code>---<code>0</code>
in this case---and the first element of <code>numbers</code>, returning the sum:
<code>1</code>.</li><li>The closure is called again repeatedly with the previous call&#39;s return
value and each element of the sequence.</li><li>When the sequence is exhausted, the last value returned from the
closure is returned to the caller.</li></ol>

<p>If the sequence has no elements, <code>nextPartialResult</code> is never executed
and <code>initialResult</code> is the result of the call to <code>reduce(_:_:)</code>.</p>

<p><strong>Parameters:</strong>
  <strong>initialResult:</strong> The value to use as the initial accumulating value.
    <code>initialResult</code> is passed to <code>nextPartialResult</code> the first time the
    closure is executed.
  <strong>nextPartialResult:</strong> A closure that combines an accumulating value and
    an element of the sequence into a new accumulating value, to be used
    in the next call of the <code>nextPartialResult</code> closure or returned to
    the caller.
<strong>Returns:</strong> The final accumulated value. If the sequence has no elements,
  the result is <code>initialResult</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func reduce&lt;Result&gt;(_ initialResult: Result, _ nextPartialResult: (Result, Self.Iterator.Element) throws -&gt; Result) rethrows -&gt; Result</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--reversed">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--reversed">func reversed()</a>
        
<div class="comment collapse" id="comment-func--reversed"><div class="p">
    <p>Returns an array containing the elements of this sequence in reverse
order.</p>

<p>The sequence must be finite.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the sequence.</p>

<p><strong>Returns:</strong> An array containing the elements of this sequence in
  reverse order.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func reversed() -&gt; [Self.Iterator.Element]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--sorted-by_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--sorted-by_">func sorted(<wbr>by:)</a>
        
<div class="comment collapse" id="comment-func--sorted-by_"><div class="p">
    <p>Returns the elements of the sequence, sorted using the given
predicate as the comparison between elements.</p>

<p>When you want to sort a sequence of elements that don&#39;t conform to
the <code>Comparable</code> protocol, pass a predicate to this method that returns
<code>true</code> when the first element passed should be ordered before the
second. The elements of the resulting array are ordered according to the
given predicate.</p>

<p>The predicate must be a <em>strict weak ordering</em> over the elements. That
is, for any elements <code>a</code>, <code>b</code>, and <code>c</code>, the following conditions must
hold:</p>

<ul><li><code>areInIncreasingOrder(a, a)</code> is always <code>false</code>. (Irreflexivity)</li><li>If <code>areInIncreasingOrder(a, b)</code> and <code>areInIncreasingOrder(b, c)</code> are
both <code>true</code>, then <code>areInIncreasingOrder(a, c)</code> is also <code>true</code>.
(Transitive comparability)</li><li>Two elements are <em>incomparable</em> if neither is ordered before the other
according to the predicate. If <code>a</code> and <code>b</code> are incomparable, and <code>b</code>
and <code>c</code> are incomparable, then <code>a</code> and <code>c</code> are also incomparable.
(Transitive incomparability)</li></ul>

<p>The sorting algorithm is not stable. A nonstable sort may change the
relative order of elements for which <code>areInIncreasingOrder</code> does not
establish an order.</p>

<p>In the following example, the predicate provides an ordering for an array
of a custom <code>HTTPResponse</code> type. The predicate orders errors before
successes and sorts the error responses by their error code.</p>

<pre><code class="language-swift">enum HTTPResponse {
    case ok
    case error(Int)
}

let responses: [HTTPResponse] = [.error(500), .ok, .ok, .error(404), .error(403)]
let sortedResponses = responses.sorted {
    switch ($0, $1) {
    // Order errors by code
    case let (.error(aCode), .error(bCode)):
        return aCode &lt; bCode

    // All successes are equivalent, so none is before any other
    case (.ok, .ok): return false

    // Order errors before successes
    case (.error, .ok): return true
    case (.ok, .error): return false
    }
}
print(sortedResponses)
// Prints &quot;[.error(403), .error(404), .error(500), .ok, .ok]&quot;</code></pre>

<p>You also use this method to sort elements that conform to the
<code>Comparable</code> protocol in descending order. To sort your sequence
in descending order, pass the greater-than operator (<code>&gt;</code>) as the
<code>areInIncreasingOrder</code> parameter.</p>

<pre><code class="language-swift">let students: Set = [&quot;Kofi&quot;, &quot;Abena&quot;, &quot;Peter&quot;, &quot;Kweku&quot;, &quot;Akosua&quot;]
let descendingStudents = students.sorted(by: &gt;)
print(descendingStudents)
// Prints &quot;[&quot;Peter&quot;, &quot;Kweku&quot;, &quot;Kofi&quot;, &quot;Akosua&quot;, &quot;Abena&quot;]&quot;</code></pre>

<p>Calling the related <code>sorted()</code> method is equivalent to calling this
method and passing the less-than operator (<code>&lt;</code>) as the predicate.</p>

<pre><code class="language-swift">print(students.sorted())
// Prints &quot;[&quot;Abena&quot;, &quot;Akosua&quot;, &quot;Kofi&quot;, &quot;Kweku&quot;, &quot;Peter&quot;]&quot;
print(students.sorted(by: &lt;))
// Prints &quot;[&quot;Abena&quot;, &quot;Akosua&quot;, &quot;Kofi&quot;, &quot;Kweku&quot;, &quot;Peter&quot;]&quot;</code></pre>

<p><strong><code>areInIncreasingOrder</code>:</strong>  A predicate that returns <code>true</code> if its first
  argument should be ordered before its second argument; otherwise,
  <code>false</code>.
<strong>Returns:</strong> A sorted array of the sequence&#39;s elements.</p>

<p><strong>See Also:</strong> <code>sorted()</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func sorted(by areInIncreasingOrder: (Self.Iterator.Element, Self.Iterator.Element) -&gt; Bool) -&gt; [Self.Iterator.Element]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--split_omittingemptysubsequences_whereseparator_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--split_omittingemptysubsequences_whereseparator_">func split(<wbr>_:<wbr>omittingEmptySubsequences:<wbr>whereSeparator:)</a>
        
<div class="comment collapse" id="comment-func--split_omittingemptysubsequences_whereseparator_"><div class="p">
    <p>Returns the longest possible subsequences of the sequence, in order, that
don&#39;t contain elements satisfying the given predicate. Elements that are
used to split the sequence are not returned as part of any subsequence.</p>

<p>The following examples show the effects of the <code>maxSplits</code> and
<code>omittingEmptySubsequences</code> parameters when splitting a string using a
closure that matches spaces. The first use of <code>split</code> returns each word
that was originally separated by one or more spaces.</p>

<pre><code class="language-swift">let line = &quot;BLANCHE:   I don&#39;t want realism. I want magic!&quot;
print(line.characters.split(whereSeparator: { $0 == &quot; &quot; })
                     .map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;I&quot;, &quot;don\&#39;t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;</code></pre>

<p>The second example passes <code>1</code> for the <code>maxSplits</code> parameter, so the
original string is split just once, into two new strings.</p>

<pre><code class="language-swift">print(
   line.characters.split(maxSplits: 1, whereSeparator: { $0 == &quot; &quot; })
                  .map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;  I don\&#39;t want realism. I want magic!&quot;]&quot;</code></pre>

<p>The final example passes <code>true</code> for the <code>allowEmptySlices</code> parameter, so
the returned array contains empty strings where spaces were repeated.</p>

<pre><code class="language-swift">print(
    line.characters.split(
        omittingEmptySubsequences: false, 
        whereSeparator: { $0 == &quot; &quot; }
    ).map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;&quot;, &quot;&quot;, &quot;I&quot;, &quot;don\&#39;t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;</code></pre>

<p><strong>Parameters:</strong>
  <strong>maxSplits:</strong> The maximum number of times to split the sequence, or one
    less than the number of subsequences to return. If <code>maxSplits + 1</code>
    subsequences are returned, the last one is a suffix of the original
    sequence containing the remaining elements. <code>maxSplits</code> must be
    greater than or equal to zero. The default value is <code>Int.max</code>.
  <strong>omittingEmptySubsequences:</strong> If <code>false</code>, an empty subsequence is
    returned in the result for each pair of consecutive elements
    satisfying the <code>isSeparator</code> predicate and for each element at the
    start or end of the sequence satisfying the <code>isSeparator</code> predicate.
    If <code>true</code>, only nonempty subsequences are returned. The default
    value is <code>true</code>.
  <strong>isSeparator:</strong> A closure that returns <code>true</code> if its argument should be
    used to split the sequence; otherwise, <code>false</code>.
<strong>Returns:</strong> An array of subsequences, split from this sequence&#39;s elements.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func split(maxSplits: Int = default, omittingEmptySubsequences: Bool = default, whereSeparator isSeparator: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; [AnySequence&lt;Self.Iterator.Element&gt;]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--starts-with_by_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--starts-with_by_">func starts(<wbr>with:<wbr>by:)</a>
        
<div class="comment collapse" id="comment-func--starts-with_by_"><div class="p">
    <p>Returns a Boolean value indicating whether the initial elements of the
sequence are equivalent to the elements in another sequence, using
the given predicate as the equivalence test.</p>

<p>The predicate must be a <em>equivalence relation</em> over the elements. That
is, for any elements <code>a</code>, <code>b</code>, and <code>c</code>, the following conditions must
hold:</p>

<ul><li><code>areEquivalent(a, a)</code> is always <code>true</code>. (Reflexivity)</li><li><code>areEquivalent(a, b)</code> implies <code>areEquivalent(b, a)</code>. (Symmetry)</li><li>If <code>areEquivalent(a, b)</code> and <code>areEquivalent(b, c)</code> are both <code>true</code>, then
<code>areEquivalent(a, c)</code> is also <code>true</code>. (Transitivity)</li></ul>

<p><strong>Parameters:</strong>
  <strong>possiblePrefix:</strong> A sequence to compare to this sequence.
  <strong>areEquivalent:</strong> A predicate that returns <code>true</code> if its two arguments
    are equivalent; otherwise, <code>false</code>.
<strong>Returns:</strong> <code>true</code> if the initial elements of the sequence are equivalent
  to the elements of <code>possiblePrefix</code>; otherwise, <code>false</code>. If
  <code>possiblePrefix</code> has no elements, the return value is <code>true</code>.</p>

<p><strong>See Also:</strong> <code>starts(with:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func starts&lt;PossiblePrefix where PossiblePrefix : Sequence, PossiblePrefix.Iterator.Element == Self.Iterator.Element&gt;(with possiblePrefix: PossiblePrefix, by areEquivalent: (Self.Iterator.Element, Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func--suffix_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--suffix_">func suffix(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func--suffix_"><div class="p">
    <p>Returns a subsequence, up to the given maximum length, containing the
final elements of the sequence.</p>

<p>The sequence must be finite. If the maximum length exceeds the number of
elements in the sequence, the result contains all the elements in the
sequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.suffix(2))
// Prints &quot;[4, 5]&quot;
print(numbers.suffix(10))
// Prints &quot;[1, 2, 3, 4, 5]&quot;</code></pre>

<p><strong><code>maxLength</code>:</strong>  The maximum number of elements to return. The
  value of <code>maxLength</code> must be greater than or equal to zero.
<strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func suffix(_ maxLength: Int) -&gt; AnySequence&lt;Self.Iterator.Element&gt;</code>
    
    
</div></div>
</div>







<h4>Where Base.Iterator == Iterator</h4>




<div class="declaration" id="func-base-iterator-iterator-filter_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-base-iterator-iterator-filter_">func filter(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-base-iterator-iterator-filter_"><div class="p">
    <p>Returns an array containing, in order, the elements of the sequence
that satisfy the given predicate.</p>

<p>In this example, <code>filter</code> is used to include only names shorter than five
characters.</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let shortNames = cast.filter { $0.characters.count &lt; 5 }
print(shortNames)
// Prints &quot;[&quot;Kim&quot;, &quot;Karl&quot;]&quot;</code></pre>

<p><strong><code>includeElement</code>:</strong>  A closure that takes an element of the
  sequence as its argument and returns a Boolean value indicating
  whether the element should be included in the returned array.
<strong>Returns:</strong> An array of the elements that <code>includeElement</code> allowed.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func filter(_ isIncluded: (Self.Base.Iterator.Element) throws -&gt; Bool) rethrows -&gt; [Self.Base.Iterator.Element]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-base-iterator-iterator-makeiterator">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-base-iterator-iterator-makeiterator">func makeIterator()</a>
        
<div class="comment collapse" id="comment-func-base-iterator-iterator-makeiterator"><div class="p">
    <p>Returns an iterator over the elements of this sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func makeIterator() -&gt; Self.Base.Iterator</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-base-iterator-iterator-map_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-base-iterator-iterator-map_">func map(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-base-iterator-iterator-map_"><div class="p">
    <p>Returns an array containing the results of mapping the given closure
over the sequence&#39;s elements.</p>

<p>In this example, <code>map</code> is used first to convert the names in the array to
lowercase strings and then to count their characters.</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let lowercaseNames = cast.map { $0.lowercaseString }
// &#39;lowercaseNames&#39; == [&quot;vivien&quot;, &quot;marlon&quot;, &quot;kim&quot;, &quot;karl&quot;]
let letterCounts = cast.map { $0.characters.count }
// &#39;letterCounts&#39; == [6, 6, 3, 4]</code></pre>

<p><strong><code>transform</code>:</strong>  A mapping closure. <code>transform</code> accepts an
  element of this sequence as its parameter and returns a transformed
  value of the same or of a different type.
<strong>Returns:</strong> An array containing the transformed elements of this
  sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func map&lt;T&gt;(_ transform: (Self.Base.Iterator.Element) throws -&gt; T) rethrows -&gt; [T]</code>
    
    
</div></div>
</div>





<h4>Where Iterator == Self</h4>




<div class="declaration" id="func-iterator-self-makeiterator">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-self-makeiterator">func makeIterator()</a>
        
<div class="comment collapse" id="comment-func-iterator-self-makeiterator"><div class="p">
    <p>Returns an iterator over the elements of this sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func makeIterator() -&gt; Self</code>
    
    
</div></div>
</div>





<h4>Where Iterator.Element : Comparable</h4>




<div class="declaration" id="func-iterator-element_-comparable-lexicographicallyprecedes_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-comparable-lexicographicallyprecedes_">func lexicographicallyPrecedes(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-comparable-lexicographicallyprecedes_"><div class="p">
    <p>Returns a Boolean value indicating whether the sequence precedes another
sequence in a lexicographical (dictionary) ordering, using the
less-than operator (<code>&lt;</code>) to compare elements.</p>

<p>This example uses the <code>lexicographicallyPrecedes</code> method to test which
array of integers comes first in a lexicographical ordering.</p>

<pre><code class="language-swift">let a = [1, 2, 2, 2]
let b = [1, 2, 3, 4]

print(a.lexicographicallyPrecedes(b))
// Prints &quot;true&quot;
print(b.lexicographicallyPrecedes(b))
// Prints &quot;false&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A sequence to compare to this sequence.
<strong>Returns:</strong> <code>true</code> if this sequence precedes <code>other</code> in a dictionary
  ordering; otherwise, <code>false</code>.</p>

<p><strong>Note:</strong> This method implements the mathematical notion of lexicographical
  ordering, which has no connection to Unicode.  If you are sorting
  strings to present to the end user, use <code>String</code> APIs that
  perform localized comparison.
<strong>See Also:</strong> <code>lexicographicallyPrecedes(_:by:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func lexicographicallyPrecedes&lt;OtherSequence where OtherSequence : Sequence, OtherSequence.Iterator.Element == Self.Iterator.Element&gt;(_ other: OtherSequence) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-iterator-element_-comparable-max">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-comparable-max">@warn_unqualified_access
     func max()</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-comparable-max"><div class="p">
    <p>Returns the maximum element in the sequence.</p>

<p>This example finds the smallest value in an array of height measurements.</p>

<pre><code class="language-swift">let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
let greatestHeight = heights.max()
print(greatestHeight)
// Prints &quot;Optional(67.5)&quot;</code></pre>

<p><strong>Returns:</strong> The sequence&#39;s maximum element. If the sequence has no
  elements, returns <code>nil</code>.</p>

<p><strong>See Also:</strong> <code>max(by:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">@warn_unqualified_access
     func max() -&gt; Self.Iterator.Element?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-iterator-element_-comparable-min">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-comparable-min">@warn_unqualified_access
     func min()</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-comparable-min"><div class="p">
    <p>Returns the minimum element in the sequence.</p>

<p>This example finds the smallest value in an array of height measurements.</p>

<pre><code class="language-swift">let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
let lowestHeight = heights.min()
print(lowestHeight)
// Prints &quot;Optional(58.5)&quot;</code></pre>

<p><strong>Returns:</strong> The sequence&#39;s minimum element. If the sequence has no
  elements, returns <code>nil</code>.</p>

<p><strong>See Also:</strong> <code>min(by:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">@warn_unqualified_access
     func min() -&gt; Self.Iterator.Element?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-iterator-element_-comparable-sorted">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-comparable-sorted">func sorted()</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-comparable-sorted"><div class="p">
    <p>Returns the elements of the sequence, sorted.</p>

<p>You can sort any sequence of elements that conform to the
<code>Comparable</code> protocol by calling this method. Elements are sorted in
ascending order.</p>

<p>The sorting algorithm is not stable. A nonstable sort may change the
relative order of elements that compare equal.</p>

<p>Here&#39;s an example of sorting a list of students&#39; names. Strings in Swift
conform to the <code>Comparable</code> protocol, so the names are sorted in
ascending order according to the less-than operator (<code>&lt;</code>).</p>

<pre><code class="language-swift">let students: Set = [&quot;Kofi&quot;, &quot;Abena&quot;, &quot;Peter&quot;, &quot;Kweku&quot;, &quot;Akosua&quot;]
let sortedStudents = students.sorted()
print(sortedStudents)
// Prints &quot;[&quot;Abena&quot;, &quot;Akosua&quot;, &quot;Kofi&quot;, &quot;Kweku&quot;, &quot;Peter&quot;]&quot;</code></pre>

<p>To sort the elements of your sequence in descending order, pass the
greater-than operator (<code>&gt;</code>) to the <code>sorted(by:)</code> method.</p>

<pre><code class="language-swift">let descendingStudents = students.sorted(by: &gt;)
print(descendingStudents)
// Prints &quot;[&quot;Peter&quot;, &quot;Kweku&quot;, &quot;Kofi&quot;, &quot;Akosua&quot;, &quot;Abena&quot;]&quot;</code></pre>

<p><strong>Returns:</strong> A sorted array of the sequence&#39;s elements.</p>

<p><strong>See Also:</strong> <code>sorted(by:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func sorted() -&gt; [Self.Iterator.Element]</code>
    
    
</div></div>
</div>





<h4>Where Iterator.Element : Equatable</h4>




<div class="declaration" id="func-iterator-element_-equatable-contains_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-equatable-contains_">func contains(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-equatable-contains_"><div class="p">
    <p>Returns a Boolean value indicating whether the sequence contains the
given element.</p>

<p>This example checks to see whether a favorite actor is in an array
storing a movie&#39;s cast.</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
print(cast.contains(&quot;Marlon&quot;))
// Prints &quot;true&quot;
print(cast.contains(&quot;James&quot;))
// Prints &quot;false&quot;</code></pre>

<p><strong><code>element</code>:</strong>  The element to find in the sequence.
<strong>Returns:</strong> <code>true</code> if the element was found in the sequence; otherwise,
  <code>false</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func contains(_ element: Self.Iterator.Element) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-iterator-element_-equatable-elementsequal_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-equatable-elementsequal_">func elementsEqual(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-equatable-elementsequal_"><div class="p">
    <p>Returns a Boolean value indicating whether this sequence and another
sequence contain the same elements in the same order.</p>

<p>At least one of the sequences must be finite.</p>

<p>This example tests whether one countable range shares the same elements
as another countable range and an array.</p>

<pre><code class="language-swift">let a = 1...3
let b = 1...10

print(a.elementsEqual(b))
// Prints &quot;false&quot;
print(a.elementsEqual([1, 2, 3]))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>other</code>:</strong>  A sequence to compare to this sequence.
<strong>Returns:</strong> <code>true</code> if this sequence and <code>other</code> contain the same elements
  in the same order.</p>

<p><strong>See Also:</strong> <code>elementsEqual(_:by:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func elementsEqual&lt;OtherSequence where OtherSequence : Sequence, OtherSequence.Iterator.Element == Self.Iterator.Element&gt;(_ other: OtherSequence) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-iterator-element_-equatable-split_maxsplits_omittingemptysubsequences_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-equatable-split_maxsplits_omittingemptysubsequences_">func split(<wbr>_:<wbr>maxSplits:<wbr>omittingEmptySubsequences:)</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-equatable-split_maxsplits_omittingemptysubsequences_"><div class="p">
    <p>Returns the longest possible subsequences of the sequence, in order,
around elements equal to the given element.</p>

<p>The resulting array consists of at most <code>maxSplits + 1</code> subsequences.
Elements that are used to split the sequence are not returned as part of
any subsequence.</p>

<p>The following examples show the effects of the <code>maxSplits</code> and
<code>omittingEmptySubsequences</code> parameters when splitting a string at each
space character (&quot; &quot;). The first use of <code>split</code> returns each word that
was originally separated by one or more spaces.</p>

<pre><code class="language-swift">let line = &quot;BLANCHE:   I don&#39;t want realism. I want magic!&quot;
print(line.characters.split(separator: &quot; &quot;)
                     .map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;I&quot;, &quot;don\&#39;t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;</code></pre>

<p>The second example passes <code>1</code> for the <code>maxSplits</code> parameter, so the
original string is split just once, into two new strings.</p>

<pre><code class="language-swift">print(line.characters.split(separator: &quot; &quot;, maxSplits: 1)
                      .map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;  I don\&#39;t want realism. I want magic!&quot;]&quot;</code></pre>

<p>The final example passes <code>false</code> for the <code>omittingEmptySubsequences</code>
parameter, so the returned array contains empty strings where spaces
were repeated.</p>

<pre><code class="language-swift">print(line.characters.split(separator: &quot; &quot;, omittingEmptySubsequences: false)
                      .map(String.init))
// Prints &quot;[&quot;BLANCHE:&quot;, &quot;&quot;, &quot;&quot;, &quot;I&quot;, &quot;don\&#39;t&quot;, &quot;want&quot;, &quot;realism.&quot;, &quot;I&quot;, &quot;want&quot;, &quot;magic!&quot;]&quot;</code></pre>

<p><strong>Parameters:</strong>
  <strong>separator:</strong> The element that should be split upon.
  <strong>maxSplits:</strong> The maximum number of times to split the sequence, or one
    less than the number of subsequences to return. If <code>maxSplits + 1</code>
    subsequences are returned, the last one is a suffix of the original
    sequence containing the remaining elements. <code>maxSplits</code> must be
    greater than or equal to zero. The default value is <code>Int.max</code>.
  <strong>omittingEmptySubsequences:</strong> If <code>false</code>, an empty subsequence is
    returned in the result for each consecutive pair of <code>separator</code>
    elements in the sequence and for each instance of <code>separator</code> at the
    start or end of the sequence. If <code>true</code>, only nonempty subsequences
    are returned. The default value is <code>true</code>.
<strong>Returns:</strong> An array of subsequences, split from this sequence&#39;s elements.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func split(separator: Self.Iterator.Element, maxSplits: Int = default, omittingEmptySubsequences: Bool = default) -&gt; [AnySequence&lt;Self.Iterator.Element&gt;]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-iterator-element_-equatable-starts-with_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-equatable-starts-with_">func starts(<wbr>with:)</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-equatable-starts-with_"><div class="p">
    <p>Returns a Boolean value indicating whether the initial elements of the
sequence are the same as the elements in another sequence.</p>

<p>This example tests whether one countable range begins with the elements
of another countable range.</p>

<pre><code class="language-swift">let a = 1...3
let b = 1...10

print(b.starts(with: a))
// Prints &quot;true&quot;</code></pre>

<p>Passing a sequence with no elements or an empty collection as
<code>possiblePrefix</code> always results in <code>true</code>.</p>

<pre><code class="language-swift">print(b.starts(with: []))
// Prints &quot;true&quot;</code></pre>

<p><strong><code>possiblePrefix</code>:</strong>  A sequence to compare to this sequence.
<strong>Returns:</strong> <code>true</code> if the initial elements of the sequence are the same as
  the elements of <code>possiblePrefix</code>; otherwise, <code>false</code>. If
  <code>possiblePrefix</code> has no elements, the return value is <code>true</code>.</p>

<p><strong>See Also:</strong> <code>starts(with:by:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func starts&lt;PossiblePrefix where PossiblePrefix : Sequence, PossiblePrefix.Iterator.Element == Self.Iterator.Element&gt;(with possiblePrefix: PossiblePrefix) -&gt; Bool</code>
    
    
</div></div>
</div>





<h4>Where Iterator.Element : Sequence</h4>




<div class="declaration" id="func-iterator-element_-sequence-joined">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-sequence-joined">func joined()</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-sequence-joined"><div class="p">
    <p>Returns the elements of this sequence of sequences, concatenated.</p>

<p>In this example, an array of three ranges is flattened so that the
elements of each range can be iterated in turn.</p>

<pre><code class="language-swift">let ranges = [0..&lt;3, 8..&lt;10, 15..&lt;17]

// A for-in loop over &#39;ranges&#39; accesses each range:
for range in ranges {
  print(range)
}
// Prints &quot;0..&lt;3&quot;
// Prints &quot;8..&lt;10&quot;
// Prints &quot;15..&lt;17&quot;

// Use &#39;joined()&#39; to access each element of each range:
for index in ranges.joined() {
    print(index, terminator: &quot; &quot;)
}
// Prints: &quot;0 1 2 8 9 15 16&quot;</code></pre>

<p><strong>Returns:</strong> A flattened view of the elements of this
  sequence of sequences.</p>

<p><strong>See Also:</strong> <code>flatMap(_:)</code>, <code>joined(separator:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func joined() -&gt; FlattenSequence&lt;Self&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-iterator-element_-sequence-joined_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element_-sequence-joined_">func joined(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-iterator-element_-sequence-joined_"><div class="p">
    <p>Returns the concatenated elements of this sequence of sequences,
inserting the given separator between each element.</p>

<p>This example shows how an array of <code>[Int]</code> instances can be joined, using
another <code>[Int]</code> instance as the separator:</p>

<pre><code class="language-swift">let nestedNumbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let joined = nestedNumbers.joined(separator: [-1, -2])
print(Array(joined))
// Prints &quot;[1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9]&quot;</code></pre>

<p><strong><code>separator</code>:</strong>  A sequence to insert between each of this
  sequence&#39;s elements.
<strong>Returns:</strong> The joined sequence of elements.</p>

<p><strong>See Also:</strong> <code>joined()</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func joined&lt;Separator where Separator : Sequence, Separator.Iterator.Element == Self.Iterator.Element.Iterator.Element&gt;(separator: Separator) -&gt; JoinedSequence&lt;Self&gt;</code>
    
    
</div></div>
</div>





<h4>Where Iterator.Element == String</h4>




<div class="declaration" id="func-iterator-element-string-joined_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-iterator-element-string-joined_">func joined(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-iterator-element-string-joined_"><div class="p">
    <p>Returns a new string by concatenating the elements of the sequence,
adding the given separator between each element.</p>

<p>The following example shows how an array of strings can be joined to a
single, comma-separated string:</p>

<pre><code class="language-swift">let cast = [&quot;Vivien&quot;, &quot;Marlon&quot;, &quot;Kim&quot;, &quot;Karl&quot;]
let list = cast.joined(separator: &quot;, &quot;)
print(list)
// Prints &quot;Vivien, Marlon, Kim, Karl&quot;</code></pre>

<p><strong><code>separator</code>:</strong>  A string to insert between each of the elements
  in this sequence. The default separator is an empty string.
<strong>Returns:</strong> A single, concatenated string.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func joined(separator: String = default) -&gt; String</code>
    
    
</div></div>
</div>





<h4>Where SubSequence : Sequence, SubSequence.SubSequence == SubSequence, SubSequence.Iterator.Element == Iterator.Element</h4>




<div class="declaration" id="func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-drop-while_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-drop-while_">func drop(<wbr>while:)</a>
        
<div class="comment collapse" id="comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-drop-while_"><div class="p">
    <p>Returns a subsequence by skipping the initial, consecutive elements that
satisfy the given predicate.</p>

<p>The following example uses the <code>drop(while:)</code> method to skip over the
positive numbers at the beginning of the <code>numbers</code> array. The result
begins with the first element of <code>numbers</code> that does not satisfy
<code>predicate</code>.</p>

<pre><code class="language-swift">let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let startingWithNegative = numbers.drop(while: { $0 &gt; 0 })
// startingWithNegative == [-2, 9, -6, 10, 1]</code></pre>

<p>If <code>predicate</code> matches every element in the sequence, the result is an
empty sequence.</p>

<p><strong><code>predicate</code>:</strong>  A closure that takes an element of the sequence as
  its argument and returns a Boolean value indicating whether the
  element should be included in the result.
<strong>Returns:</strong> A subsequence starting after the initial, consecutive elements
  that satisfy <code>predicate</code>.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the collection.
<strong>See Also:</strong> <code>prefix(while:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func drop(while predicate: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; AnySequence&lt;Self.Iterator.Element&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-dropfirst_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-dropfirst_">func dropFirst(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-dropfirst_"><div class="p">
    <p>Returns a subsequence containing all but the given number of initial
elements.</p>

<p>If the number of elements to drop exceeds the number of elements in
the sequence, the result is an empty subsequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropFirst(2))
// Prints &quot;[3, 4, 5]&quot;
print(numbers.dropFirst(10))
// Prints &quot;[]&quot;</code></pre>

<p><strong><code>n</code>:</strong>  The number of elements to drop from the beginning of
  the sequence. <code>n</code> must be greater than or equal to zero.
<strong>Returns:</strong> A subsequence starting after the specified number of
  elements.</p>

<p><strong>Complexity:</strong> O(1).</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func dropFirst(_ n: Int) -&gt; AnySequence&lt;Self.Iterator.Element&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-droplast_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-droplast_">func dropLast(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-droplast_"><div class="p">
    <p>Returns a subsequence containing all but the given number of final
elements.</p>

<p>The sequence must be finite. If the number of elements to drop exceeds
the number of elements in the sequence, the result is an empty
subsequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.dropLast(2))
// Prints &quot;[1, 2, 3]&quot;
print(numbers.dropLast(10))
// Prints &quot;[]&quot;</code></pre>

<p><strong><code>n</code>:</strong>  The number of elements to drop off the end of the
  sequence. <code>n</code> must be greater than or equal to zero.
<strong>Returns:</strong> A subsequence leaving off the specified number of elements.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the sequence.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func dropLast(_ n: Int) -&gt; AnySequence&lt;Self.Iterator.Element&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-prefix_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-prefix_">func prefix(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-prefix_"><div class="p">
    <p>Returns a subsequence, up to the specified maximum length, containing the
initial elements of the sequence.</p>

<p>If the maximum length exceeds the number of elements in the sequence,
the result contains all the elements in the sequence.</p>

<pre><code class="language-swift">let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints &quot;[1, 2]&quot;
print(numbers.prefix(10))
// Prints &quot;[1, 2, 3, 4, 5]&quot;</code></pre>

<p><strong><code>maxLength</code>:</strong>  The maximum number of elements to return. The
  value of <code>maxLength</code> must be greater than or equal to zero.
<strong>Returns:</strong> A subsequence starting at the beginning of this sequence
  with at most <code>maxLength</code> elements.</p>

<p><strong>Complexity:</strong> O(1)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func prefix(_ maxLength: Int) -&gt; AnySequence&lt;Self.Iterator.Element&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-prefix-while_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-prefix-while_">func prefix(<wbr>while:)</a>
        
<div class="comment collapse" id="comment-func-subsequence_-sequence-subsequence-subsequence-subsequence-subsequence-iterator-element-iterator-element-prefix-while_"><div class="p">
    <p>Returns a subsequence containing the initial, consecutive elements that
satisfy the given predicate.</p>

<p>The following example uses the <code>prefix(while:)</code> method to find the
positive numbers at the beginning of the <code>numbers</code> array. Every element
of <code>numbers</code> up to, but not including, the first negative value is
included in the result.</p>

<pre><code class="language-swift">let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let positivePrefix = numbers.prefix(while: { $0 &gt; 0 })
// positivePrefix == [3, 7, 4]</code></pre>

<p>If <code>predicate</code> matches every element in the sequence, the resulting
sequence contains every element of the sequence.</p>

<p><strong><code>predicate</code>:</strong>  A closure that takes an element of the sequence as
  its argument and returns a Boolean value indicating whether the
  element should be included in the result.
<strong>Returns:</strong> A subsequence of the initial, consecutive elements that
  satisfy <code>predicate</code>.</p>

<p><strong>Complexity:</strong> O(<em>n</em>), where <em>n</em> is the length of the collection.
<strong>See Also:</strong> <code>drop(while:)</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">func prefix(while predicate: (Self.Iterator.Element) throws -&gt; Bool) rethrows -&gt; AnySequence&lt;Self.Iterator.Element&gt;</code>
    
    
</div></div>
</div>



