# \[Pitch\] Remove the single-pass requirement on Sequence

**URL:** <https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964>\
**Category:** Pitches\
**Created:** [January 20, 2018, 6:52pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964 "2018-01-20T18:52:50Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [January 23, 2018, 4:19am UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/43 "2018-01-23T04:19:16Z")

</div>

I've reread this thread a couple of times, and I haven't found any examples of something that isn't satisfied by `Sequence` or `Collection`. To be clear, this is what I'm using to judge this:

`Sequence`:

- Guaranteed to be single-pass but can also be multi-pass.
- Are not required to provide indices (but can).

`Collection`:

- Guaranteed to be safely multi-pass.
- Must provide some sort of meaningful index for each element.

Can someone who's invested in this proposal provide an example that doesn't fit into these two?

---

<div class="post-metadata">

**Author:** ![Jon\_Hull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_hull/32/678_2.png) [@Jon\_Hull](https://forums.swift.org/u/Jon_Hull)\
**Post date:** [January 23, 2018, 8:46am UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/44 "2018-01-23T08:46:42Z")

</div>

Relevant (read the part which the forum collapses at the end):

> [@\[Pitch\] Remove destructive consumption from Sequence](https://forums.swift.org/t/pitch-remove-destructive-consumption-from-sequence/3151/28):
>
> Swift is a language that embraces value semantics. Many common iterators \*can\* be implemented with value semantics. Just because we can’t implement \*all\* iterators with value semantics doesn’t mean we should require them to have reference semantics. It just means you can’t \*assume\* value semantics when working with iterators in generic code unless / until we have a way to specify a value semantics constraint. That’s not necessarily a bad thing especially when it leaves the door open…

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [January 23, 2018, 7:56pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/45 "2018-01-23T19:56:41Z")

</div>

It's not that there are examples that don't fit into `Sequence` or `Collection`—the OP is proposing that we require any `Sequence` to guarantee that it is safely multi-pass, while not requiring indices or finiteness. Dealing with the single-pass nature of sequences makes writing correct generic code more challenging than it would be without that requirement.

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [January 23, 2018, 10:12pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/46 "2018-01-23T22:12:57Z")

</div>

So if I'm understanding this correctly, OP is suggesting that we remove single-pass `Sequence`s altogether because he thinks they're "extremely rare"? I'm very opposed to this, for reasons outlined upthread, but I'd be much more receptive to creating a new `MultipassSequence` or similar protocol in between `Sequence` and `Collection` that provides the guarantees that @soroush is looking for. I'm less keen on `Iterator` (which is now the basic requirement for `for`-`in` loops), which would promote `Sequence` to being multipass.

---

<div class="post-metadata">

**Author:** ![soroush](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soroush/32/859_2.png) [@soroush](https://forums.swift.org/u/soroush)\
**Post date:** [January 23, 2018, 10:57pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/47 "2018-01-23T22:57:59Z")

</div>

@Jon_Hull thanks for linking that thread; I'd done some searching but couldn't find any real discussion about this topic, but this is good reading.

> [@nnnnnnnn](#):
>
> It’s not that there are examples that don’t fit into Sequence or Collection—the OP is proposing that we require any Sequence to guarantee that it is safely multi-pass, while not requiring indices or finiteness. Dealing with the single-pass nature of sequences makes writing correct generic code more challenging than it would be without that requirement.

Yup, this is a great summary of my position. But this pitch was a lot less popular than I expected, so y'all can considered it withdrawn. 😊

> [@saagarjha](#):
>
> I’d be much more receptive to creating a new MultipassSequence or similar protocol in between Sequence and Collection that provides the guarantees that @soroush is looking for.

I didn't offer this as an approach because, for better or worse, Sequence is the level that everyone trades in when working with these types of algorithms. If Swift added another protocol like `MultipassSequence`, all the current algorithms would still stay on `Sequence`, and people would continue to write all their new algorithms in terms of `Sequence` (because you want your algorithm to work on the biggest subset of iterable things, so you put it on `Sequence`). And then we'd be back to square one. This is similar to Nate's point [here](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/22). The only way I can see to break out of that pattern is to create a bright line between destructive (non-for...in-able) iterators, and reusable sequences.

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [January 23, 2018, 11:08pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/48 "2018-01-23T23:08:45Z")

</div>

> [@soroush](#):
>
> The only way I can see to break out of that pattern is to create a bright line between destructive (non-for…in-able) iterators, and reusable sequences.

The issue I see here is library authors restricting algorithms to `Sequence`s where an `Iterator` would have done because this change makes iteration more annoying.

---

<div class="post-metadata">

**Author:** ![soroush](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soroush/32/859_2.png) [@soroush](https://forums.swift.org/u/soroush)\
**Post date:** [January 23, 2018, 11:20pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/49 "2018-01-23T23:20:54Z")

</div>

> [@saagarjha](#):
>
> The issue I see here is library authors restricting algorithms to Sequences where an Iterator would have done because this change makes iteration more annoying.

I'm not sure I follow. The "bright line" I'm describing is basically how the world works today (minus random sequences and the other edge cases). You can't `for..in` over an Iterator today. You ...could write `map` on `Iterator` today, but no one does.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [January 24, 2018, 12:34am UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/50 "2018-01-24T00:34:57Z")

</div>

To put this another way: in your second post to this thread, you rewrote your algorithm to work with single-pass sequences after learning Sequence was single-pass. If Sequence were multi-pass, rewriting the function for Iterator would require a call to makeIterator() just to pass in a Sequence. Therefore, making Sequence multi-pass discourages this generalisation to single-pass sequences.

Currently, you can still write a more concise algorithm by making Collection be the parameter. The tradeoff is greater, in terms of what sequences can no longer be handled, encouraging generalisation to single-pass sequences through use of Sequence.

The status quo encourages generalisation, while your proposed change would actively discourage it.

* * *

PS:  
I understand there are merits to your suggestion - it can be easy for authors to forget the possibility for single-pass sequences, given the presence of extensions which only work with multi-pass sequences. Having this option would also be nice for those of us who aren't library authors, and are okay with making functions just for our own use-cases. One could also make the argument that single-pass sequences are an edge-case.

Personally, I think encouraging generalisation is worth sacrificing a bit of convenience. I like that the most obvious solution is also the best practice one. But it is a matter of opinion.

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [January 24, 2018, 12:54am UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/51 "2018-01-24T00:54:58Z")

</div>

> [@MutatingFunk](#):
>
> Currently, you can still write a more concise algorithm by making Collection be the parameter. The tradeoff is greater, in terms of what sequences can no longer be handled, encouraging generalisation to single-pass sequences through use of Sequence.

Yes, this is exactly what I'm talking about. If you make `Iterator` too annoying to work with, people will stop accepting them as parameters.

---

<div class="post-metadata">

**Author:** ![soroush](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soroush/32/859_2.png) [@soroush](https://forums.swift.org/u/soroush)\
**Post date:** [January 24, 2018, 4:52am UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/52 "2018-01-24T04:52:13Z")

</div>

> [@saagarjha](#):
>
> Yes, this is exactly what I’m talking about. If you make Iterator too annoying to work with, people will stop accepting them as parameters.

I still don't follow. No one really works at the level of iterators today (except to define them for other sequences). There should be very few functions that accept iterators as parameters, right?

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [January 24, 2018, 5:22am UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/53 "2018-01-24T05:22:39Z")

</div>

> [@soroush](#):
>
> No one really works at the level of iterators today (except to define them for other sequences)

`Iterator`s are great for dealing with streams of data, especially one coming from another device such as user input or bytes from a network. They're also useful for, as you mentioned, nondeterministic, nonreplicable data such as the product of a random number generator. Some languages (off the top of my head, C++) value this distinction enough to give it a name (no, I'm not saying that we should wholesale steal from what other languages are doing. I'm just providing an example of how an `Iterator` can be useful).

There are many algorithms that do not fully utilize the `Sequence` that you're proposing and are thus overqualified. From this thread, a cartesian product can be defined as:

```
func withAllPairs<I: Iterator, S: Sequence>(of iterator: I, and sequence: S, perform body: (I.Element, S.Element) -> Void, breakWhen breakingCondition: @autoclosure (I.Element, S.Element) -> Bool = false) {
	for first in iterator { // Strawman syntax to advance through iterator
		for second in sequence {
			if (breakingCondition(first, second)) {
				break
			} else {
				body(first, second)
			}
		}
	}
}

```

As for the standard library, many of `Sequence`'s operators apply to an `Iterator` as well, and free functions such as `zip(_:_:)` should also work on `Iterator`s.

---

<div class="post-metadata">

**Author:** ![soroush](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soroush/32/859_2.png) [@soroush](https://forums.swift.org/u/soroush)\
**Post date:** [January 24, 2018, 2:48pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/54 "2018-01-24T14:48:16Z")

</div>

> [@saagarjha](#):
>
> As for the standard library, many of Sequence's operators apply to an Iterator as well, and free functions such as zip(_:_:) should also work on Iterators.

If I'm understanding you correctly, it sounds like you want to move the `Sequence` algorithms down to `IteratorProtocol`. That's a bit different than what I'm proposing here, so it might be better to start a new thread about it?

---

<div class="post-metadata">

**Author:** ![Jon\_Hull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_hull/32/678_2.png) [@Jon\_Hull](https://forums.swift.org/u/Jon_Hull)\
**Post date:** [January 24, 2018, 4:40pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/55 "2018-01-24T16:40:46Z")

</div>

It looks like there is a much deeper issue at play here. If a sequence is single pass, then most of the methods on it (e.g. map, makeIterator, etc...) really should be marked as 'mutating' because they mutate the sequence by using it up. But they aren't marked that way because it would make working with the multi-pass case unbearable for value type sequences. You end up with all these inconsistencies around both Iterator and Sequence because we are very hand wavy as to what is mutating or not. Worse, as Dave pointed out in the thread I linked, using one of those mutating methods that aren't marked mutating will mutate all of the copies too when they contain a reference-type Iterator.

Tl;dr: Many single pass sequences are value types with reference semantics.

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [January 24, 2018, 7:45pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/56 "2018-01-24T19:45:52Z")

</div>

> [@Jon\_Hull](#):
>
> Tl;dr: Many single pass sequences are value types with reference semantics.

To put it another way: they're pointers.

---

<div class="post-metadata">

**Author:** ![soroush](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/soroush/32/859_2.png) [@soroush](https://forums.swift.org/u/soroush)\
**Post date:** [January 24, 2018, 9:06pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/57 "2018-01-24T21:06:37Z")

</div>

> [@Jon\_Hull](#):
>
> they aren’t marked that way because it would make working with the multi-pass case unbearable for value type sequences. You end up with all these inconsistencies around both Iterator and Sequence because we are very hand wavy as to what is mutating or not.

Yes, exactly. This is the thing I want to fix.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [January 24, 2018, 9:09pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/58 "2018-01-24T21:09:02Z")

</div>

That is a very good point.

I suppose generic code written to work with conforming classes (as all good generic code should) will already account for this possibility.

Classes in generics are already a potential pitfall, being able to mutate their state without using a `mutating` function. You really can't ever trust `mutating` to have meaning, in a generic context.

This does put me back on the fence about whether we should change Sequence, though.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [January 24, 2018, 10:35pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/59 "2018-01-24T22:35:04Z")

</div>

classes in swift are basically smart/shared pointers in C++, and methods in a class mutate the pointee, not the pointer itself. so it makes sense

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 24, 2018, 11:07pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/60 "2018-01-24T23:07:52Z")

</div>

> [@Jon\_Hull](#):
>
> Tl;dr: Many single pass sequences are value types with reference semantics.

Isn't this true _by construction_? Can there exist any `Sequence` type that's single-pass and also has value semantics?

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [January 24, 2018, 11:09pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/61 "2018-01-24T23:09:06Z")

</div>

Most Iterators, if conformed to Sequence, would be single-pass and value-semantic.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [January 24, 2018, 11:18pm UTC](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964/62 "2018-01-24T23:18:50Z")

</div>

How can it have value semantics?

Iterating over a `Sequence` type (for example, using `forEach`) is required to be nonmutating, but iterating over a single-pass `Sequence` by definition changes its value. Therefore, any single-pass conforming type must have reference semantics for both of these conditions to hold. Am I mistaken?

[Previous page](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964.md?page=2)

[Next page](https://forums.swift.org/t/pitch-remove-the-single-pass-requirement-on-sequence/7964.md?page=4)
