[swift-evolution] [Pitch] Remove destructive consumption from Sequence

Dave Abrahams dabrahams at apple.com
Fri Jul 1 09:43:45 CDT 2016


on Fri Jul 01 2016, Brent Royal-Gordon <brent-AT-architechies.com> wrote:

>> On Jul 1, 2016, at 12:34 AM, Haravikk <swift-evolution at haravikk.me> wrote:
>> 
>> Sequences are currently dead easy to implement, and to implement in
>> an ad-hoc way via AnySequence(body:), how would that be done under
>> this required indexing scheme?
>
> `sequence(state:next:)` could be adapted into a possibly-infinite
> `Collection`, and it's not much more difficult to use than
> `AnySequence`.

Yes, the state would gain an Equatable constraint, that's all.

> (Although it would be more efficient to use a custom
> iterator for it than it would be to iterate with its indices.)

Whether the resulting collection is going to be efficient after
optimization depends on many factors.  I can easily imagine this being
optimal:

  for i in collection(
     first: 0, next: { state in state < 100 ? state + 5 : nil }) {
     print(i)
  }

However, it is true that the most reliable path to efficiency is always
going to be to encode the state and next in a protocol conformance

-- 
Dave


More information about the swift-evolution mailing list