<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I’m aware, which is why I didn’t say it should be removed. (And if I hadn’t been aware, this wouldn’t have helped me discover them. :-)<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 28, 2016, at 8:58 PM, Kevin Ballard &lt;<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">


<title class=""></title>

<div class=""><div class="">There's more uses for enumerated() than just producing Array indices.<br class=""></div>
<div class=""><br class=""></div>
<div class="">-Kevin</div>
<div class=""><br class=""></div>
<div class="">On Wed, Sep 28, 2016, at 05:49 PM, Colin Barrett via swift-evolution wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">Definitely well motivated. It seems like having both .enumerated() and .indexed() methods would still leave open the possibility of novices using .enumerated and making the same mistake as before. I realize that because of where .enumerated() sits it has to work the way it does, but is there perhaps a better design (with constrained extensions?) for a single method that can give an Int for a Sequence and an appropriate Index for a Collection?<br class=""></div>
<div class=""><br class=""></div>
<div class="">-Colin<br class=""></div>
<div class=""><br class=""></div>
<div class=""><blockquote type="cite" class=""><div class="">On Sep 28, 2016, at 1:55 PM, Erica Sadun via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div>
<div class=""><br class=""></div>
<div class=""><div style="word-wrap:break-word;-webkit-line-break:after-white-space;" class=""><div class="">Gist here:&nbsp;<a href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2" class="">https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2</a><br class=""></div>
<div class=""><br class=""></div>
<div class=""><h1 style="margin-right:0px;margin-bottom:16px;margin-left:0px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238, 238, 238);color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';background-color:rgb(255, 255, 255);margin-top:0px !important;" class="">Introducing&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:inherit;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">indexed()</code>&nbsp;collections<br class=""></h1><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';font-size:16px;background-color:rgb(255, 255, 255);" class=""><li style="" class="">Proposal: TBD<br class=""></li><li style="margin-top:0.25em;" class="">Author:&nbsp;<a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;" href="https://github.com/erica" class="">Erica Sadun</a>,&nbsp;<a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;" href="https://github.com/natecook1000" class="">Nate Cook</a>,&nbsp;<a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;" href="https://github.com/jtbandes" class="">Jacob Bandes-Storch</a>,&nbsp;<a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;" href="https://github.com/kballard" class="">Kevin Ballard</a><br class=""></li><li style="margin-top:0.25em;" class="">Status: TBD<br class=""></li><li style="margin-top:0.25em;" class="">Review manager: TBD<br class=""></li></ul><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238, 238, 238);color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';background-color:rgb(255, 255, 255);" class=""><a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;float:left;padding-right:4px;line-height:1;" href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2#introduction" class=""></a>Introduction<br class=""></h2><p style="margin-top: 0px; margin-bottom: 16px;" class=""><span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"><span class="size" style="font-size:16px">This proposal introduces&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">indexed()</code>&nbsp;to the standard library, a method on collections that returns an (index, element) tuple sequence.</span></span></span></span><br class=""></p><p style="margin-top: 0px; margin-bottom: 16px;" class=""><span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"><span class="size" style="font-size:16px">Swift-evolution thread:&nbsp;<a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;" href="https://gist.github.com/erica/tbd" class="">TBD</a></span></span></span></span><br class=""></p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238, 238, 238);color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';background-color:rgb(255, 255, 255);" class=""><a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;float:left;padding-right:4px;line-height:1;" href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2#motivation" class=""></a>Motivation<br class=""></h2><p style="margin-top: 0px; margin-bottom: 16px;" class=""><span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"><span class="size" style="font-size:16px">The standard library's&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">enumerated()</code>&nbsp;method returns a sequence of pairs enumerating a sequence. The pair's first member is a monotonically incrementing integer starting at zero, and the second member is the corresponding element of the sequence. When working with arrays, the integer is coincidentally the same type and value as an&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">Array</code>&nbsp;index but the enumerated value is not generated with index-specific semantics. This may lead to confusion when developers attempt to subscript a non-array collection with enumerated integers. It can introduce serious bugs when developers use&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">enumerated()</code>-based integer subscripting with non-zero-based array slices.</span></span></span></span><br class=""></p><p style="margin-top: 0px; margin-bottom: 16px;" class=""><span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"><span class="size" style="font-size:16px">Indices have a specific, fixed meaning in Swift, which are used to create valid collection subscripts. This proposal introduces&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">indexed()</code>&nbsp;to produce a more semantically relevant sequence by pairing a collection's&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">indices</code>&nbsp;with its members. While it is trivial to create a solution in Swift, the most common developer approach shown here calculates indexes twice:&nbsp;</span></span></span></span><br class=""></p><pre style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding-top:16px;padding-right:16px;padding-bottom:16px;padding-left:16px;overflow-y:auto;overflow-x:auto;background-color:rgb(247, 247, 247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;color:rgb(51, 51, 51);" class=""><code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:transparent;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-top-style:initial;border-right-style:initial;border-bottom-style:initial;border-left-style:initial;border-top-color:initial;border-right-color:initial;border-bottom-color:initial;border-left-color:initial;border-image-source:initial;border-image-slice:initial;border-image-width:initial;border-image-outset:initial;border-image-repeat:initial;display:inline;overflow-y:visible;overflow-x:visible;line-height:inherit;word-wrap:normal;" class="">extension Collection {
    /// Returns a sequence of pairs (*idx*, *x*), where *idx* represents a
    /// consecutive collection index, and *x* represents an element of
    /// the sequence.
    func indexed() -&gt; Zip2Sequence&lt;Self.Indices, Self&gt; {
        return zip(indices, self)
    }
}
</code><br class=""></pre><p style="margin-top: 0px; margin-bottom: 16px;" class=""><span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"><span class="size" style="font-size:16px">Incrementing an index in some collections can be unnecessarily costly. In a lazy filtered collection, an index increment is potentially O(N). We feel this is better addressed introducing a new function into the Standard Library to provide a more efficient design that avoids the attractive nuisance of the "obvious" solution.</span></span></span></span><br class=""></p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238, 238, 238);color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';background-color:rgb(255, 255, 255);" class=""><a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;float:left;padding-right:4px;line-height:1;" href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2#detailed-design" class=""></a>Detailed Design<br class=""></h2><p style="margin-top: 0px; margin-bottom: 16px;" class=""><span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"><span class="size" style="font-size:16px">Our vision of&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">indexed()</code>&nbsp;bypasses duplicated index generation with their potentially high computation costs. We'd create an iterator that calculates each index once and then applies that index to subscript the collection. Implementation would take place through&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">IndexedSequence</code>, similar to&nbsp;<code style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:13.600000381469727px;padding-top:0.2em;padding-right:0px;padding-bottom:0.2em;padding-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;background-color:rgba(0, 0, 0, 0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;" class="">EnumeratedSequence</code>.</span></span></span></span><br class=""></p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238, 238, 238);color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';background-color:rgb(255, 255, 255);" class=""><a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;float:left;padding-right:4px;line-height:1;" href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2#impact-on-existing-code" class=""></a>Impact on Existing Code<br class=""></h2><p style="margin-top: 0px; margin-bottom: 16px;" class=""><span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"><span class="size" style="font-size:16px">This proposal is purely additive and has no impact on existing code.</span></span></span></span><br class=""></p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238, 238, 238);color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';background-color:rgb(255, 255, 255);" class=""><a style="background-color:transparent;-webkit-text-decoration-skip:objects;color:rgb(64, 120, 192);text-decoration:none;float:left;padding-right:4px;line-height:1;" href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2#alternatives-considered" class=""></a>Alternatives Considered<br class=""></h2><div style="margin-top:0px;color:rgb(51, 51, 51);font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';font-size:16px;background-color:rgb(255, 255, 255);margin-bottom:0px !important;" class="">Not yet<br class=""></div>
</div>
</div>
<div class="">_______________________________________________<br class=""></div>
<div class="">swift-evolution mailing list<br class=""></div>
<div class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""></div>
<div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div>
</div>
</blockquote></div>
<div class=""><br class=""></div>
<div class=""><u class="">_______________________________________________</u><br class=""></div>
<div class="">swift-evolution mailing list<br class=""></div>
<div class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""></div>
<div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div>
</blockquote><div class=""><br class=""></div>
</div>

</div></blockquote></div><br class=""></div></body></html>