<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Wed, Sep 28, 2016, at 02:27 PM, plx via swift-evolution wrote:<br></div>
<blockquote type="cite"><div>+1 to have something *like* this, but a few questions.<br></div>
<div><br></div>
<div>Is there a specific reason `IndexedSequence` isn’t `IndexedCollection`, conforming to `Collection` (and once conditional conformances are available picking up `BidirectionalCollection` and `RandomAccessCollection` when possible?).<br></div>
</blockquote><div><br></div>
<div>This is already being discussed in this thread, but the simple answer is that adds complexity and it's not obvious that it's worth the additional complexity.</div>
<div><br></div>
<blockquote type="cite"><div>Secondly, can you provide more detail on the proposed implementation?&nbsp;<br></div>
<div><br></div>
<div>Are you just walking the index forward and subscripting the base in the iterator, or something fancier?<br></div>
</blockquote><div><br></div>
<div>Yeah, that's what it would be. Something like<br></div>
<div><br></div>
<div>sequence(state: base.indices, next: {<br></div>
<div>&nbsp; &nbsp; guard let idx = $0.next() else { return nil }<br></div>
<div>&nbsp; &nbsp; return (idx, base[idx])<br></div>
<div>})<br></div>
<div><br></div>
<div>except done as a concrete type.</div>
<div><br></div>
<div>-Kevin</div>
<div><br></div>
<blockquote type="cite"><div><blockquote type="cite"><div>On Sep 28, 2016, at 12:55 PM, Erica Sadun via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<div><br></div>
<div><div style="word-wrap:break-word;-webkit-line-break:after-white-space;"><div>Gist here:&nbsp;<a href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2">https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2</a><br></div>
<div><br></div>
<div><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;">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;">indexed()</code>&nbsp;collections<br></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);"><li style="">Proposal: TBD<br></li><li style="margin-top:0.25em;">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">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">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">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">Kevin Ballard</a><br></li><li style="margin-top:0.25em;">Status: TBD<br></li><li style="margin-top:0.25em;">Review manager: TBD<br></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);"><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"></a>Introduction<br></h2><p style="margin-top: 0px; margin-bottom: 16px;"><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;">indexed()</code>&nbsp;to the standard library, a method on collections that returns an (index, element) tuple sequence.</span></span></span></span><br></p><p style="margin-top: 0px; margin-bottom: 16px;"><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">TBD</a></span></span></span></span><br></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);"><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"></a>Motivation<br></h2><p style="margin-top: 0px; margin-bottom: 16px;"><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;">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;">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;">enumerated()</code>-based integer subscripting with non-zero-based array slices.</span></span></span></span><br></p><p style="margin-top: 0px; margin-bottom: 16px;"><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;">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;">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></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);"><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;">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></pre><p style="margin-top: 0px; margin-bottom: 16px;"><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></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);"><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"></a>Detailed Design<br></h2><p style="margin-top: 0px; margin-bottom: 16px;"><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;">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;">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;">EnumeratedSequence</code>.</span></span></span></span><br></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);"><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"></a>Impact on Existing Code<br></h2><p style="margin-top: 0px; margin-bottom: 16px;"><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></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);"><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"></a>Alternatives Considered<br></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;">Not yet<br></div>
</div>
</div>
<div>_______________________________________________<br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div>https://lists.swift.org/mailman/listinfo/swift-evolution<br></div>
</div>
</blockquote></div>
<div><br></div>
<div><u>_______________________________________________</u><br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
</blockquote><div><br></div>
</body>
</html>