[swift-evolution] [Pitch] Enumerate from offset

Xiaodi Wu xiaodi.wu at gmail.com
Sun May 7 03:30:52 CDT 2017


On Sun, May 7, 2017 at 2:23 AM, Pavol Vaskovic via swift-evolution <
swift-evolution at swift.org> wrote:

> (I forgot to reply-all to mailing list, sorry for double posting Ben!)
>
>
> On Thu, May 4, 2017 at 8:11 PM, Ben Cohen via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>>
>> On May 4, 2017, at 10:15 AM, Jaden Geller via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>> It's been suggested by a core team member that enumerated itself might
>> not hold its weight in the standard library. Instead, we ought to write
>> `zip(foo.indices, foo)` or `zip(0..., foo)`. It's easier for the caller to
>> see which side of the tuple stores the index and whether the index is an
>> integer or an actual index type. This use case seems to further support
>> this design since `zip(x…, foo)` and `zip(foo.indicies.drop(x), foo)` can
>> be easily written.
>>
>>
>> Yup, this is my personal preference. Enumerated has a number of
>> correctness and usability issues: slices aren’t zero based so you could
>> miss a potential trap at runtime, order of the two arguments is unclear
>> unlike the zip alternative, and it encourages people to write code using
>> integers rather than indices even one the latter is more expressive and
>> generalizes better if you ever want to make what you wrote generic.
>>
>> Note also, since SE-0172 is now implemented on master, you can also write
>> zip(6…, myArray) if you want to alter the starting value.
>>
>
> I have mentioned this in Alternatives considered section, with following
> comment:
>
> > Drawback of this approach is that you need to use free function zip,
> forcing a break in the chain of sequence operations, as there is currently
> no zipped method on Sequence.
>
> > If this is the preffered approach, we should consider removing the
> enumerated() method altogether, because the limited usefullness in its
> current state hardly justifies the space on API surface it occupies.
>
> I'd be personally fine with removal of enumerated() if we added
> zipped(with:).
> Free function zip is essential, no doubt. What was the reason there isn't
> also zipped, Ben?
>

Sorry, I'm confused: what is the point of adding a method that does the
same thing as an existing free function? With one-sided ranges now a part
of the language, I'd support removal of `enumerated()` with no other
changes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170507/22dbb419/attachment.html>


More information about the swift-evolution mailing list