[swift-evolution] Fixing the confusion between non-mutating algorithms and single-pass sequences
David Waite
david at alkaline-solutions.com
Wed Jul 20 16:41:32 CDT 2016
> On Jul 20, 2016, at 2:57 PM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
>
>> > True, people might try to get the iterator a second time, but we can
>> > make the iteratorProvider method optional (and trying to get an
>> > iterator from an iterator which is spent would return nil)
>> > and then they are forced to deal with the case where it was
>> > single-pass.
>>
>> Now you can't loop over the same array multiple times.
> I must be missing something. Isn’t that the point?
>
> I mean, your version is called “IterableOnce”. Why do you want to iterate on IterableOnce more than once? The point (at least in my mind) is to provide a common interface for things that we want to iterate over a single time. If you want to iterate multiple times, use collection’s interface where you are guaranteed multi-pass.
>
> That said, you actually can loop multiple times for collections by getting a new iterator from the provider (which could point to the same array storage). The optional just forces you to check for the single-pass case.
>
> I have a feeling like I am missing your true meaning here though...
>
> Thanks,
> Jon
Right - an iterator instance is single pass, but a method that returns an iterator may be able to make multiple iterators which iterate over the same sequence of values.
I still think ideally you can operate on iterators as streams of data directly, rather than requiring a base protocol. I understand how this could cause lots of duplication to have two ‘sequence’ implementations, however.
-DW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160720/ea6a3347/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160720/ea6a3347/attachment.sig>
More information about the swift-evolution
mailing list