[swift-evolution] [Pitch] Remove destructive consumption from Sequence
jhull at gbis.com
Sun Jun 26 22:28:56 CDT 2016
Good use case!
Would being handed an Iterator work for you in this case? Are there methods on Sequence that you need which aren’t on (or couldn’t be added to) Iterator?
The main issue for me is that Iterators, by the definition of their API are destructive (you can only use them once), but the ability of a sequence to vend a brand new Iterator implies multi-pass. I am not saying we get rid of single-pass ability, so much as we should use Iterators for that purpose, and Sequences (which vend Iterators) should be multi-pass because they kind of pretend to be already…
> On Jun 26, 2016, at 3:41 PM, Russ Bishop <xenadu at gmail.com> wrote:
>> On Jun 23, 2016, at 12:26 AM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
>> What are the actual use-cases where people have needed destructive iterators? Every time I have thought I wanted it, I ended up wanting multi-pass later. For example, I had a sequence of random numbers, but ended up having to build a random hash instead so I could reliably re-create that sequence. The other use-cases I can think of (e.g. markov-chains) would probably end up needing repeatability at some point as well.
>> The only thing I can think of without an eventual multi-pass requirement would be reading in an input or signal of some sort.
> I use it in a LazyRowSequence<T: SqlModelConvertible> where querying Sqlite in WAL mode allows multiple concurrent readers to get point-in-time snapshots of the database. The query can’t be replayed without buffering all the rows in memory because Sqlite’s step functions are not bi-directional. In some cases we are talking about tens of thousands of rows (or even hundreds of thousands) and the ability to avoid buffering them is a feature, not a bug.
> Sequence is the stream of things you can iterate over; that’s the only real promise it makes. If you want repeated iteration use a Collection.
> I’m sure someone has a use for a stream of values that can’t be buffered but can be iterated over multiple times, but is that common enough to warrant redesigning the entire sequence/collection/iterator hierarchy?
More information about the swift-evolution