[swift-evolution] [Pitch] Enumerate from offset

Pavol Vaskovic pali at pali.sk
Sun May 7 04:18:15 CDT 2017

> On 7 May 2017, at 10:30, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 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.

I’m talking about scenario where you have a chain of sequence operations, say:

(1...N).makeIterator().enumerated().lazy.prefix(while: {$0.0 < oneLess}).count()

With a free function, you need to break the chain in order to replace enumerated with zip:

zip((1...N).makeIterator(), 0...oneLess).lazy.prefix(while: {$0.0 < oneLess}).count()

It forces you to rearrange your code into less than ideal order. Free function zip works great if you start with it. Not when you need to employ it in the middle of the chain. 

(1...N).makeIterator().zipped(with: 0...oneLess).lazy.prefix(while: {$0.0 < oneLess}).count()

Just to be clear, how much change I’m proposing here, in case we remove enumerated:

extension Sequence {
    func zipped<S>(with otherSequence: S) -> Zip2Sequence<Self, S> where S: Sequence {
        return zip (self, otherSequence)

Best regards
Pavol Vaskovic 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170507/765f08d3/attachment.html>

More information about the swift-evolution mailing list