[swift-evolution] [Draft] Rename Sequence.elementsEqual

Ole Begemann ole at oleb.net
Sat Oct 14 16:21:58 CDT 2017


> On 14. Oct 2017, at 21:55, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> 
> On Sat, Oct 14, 2017 at 14:36 Benjamin G <benjamin.garrigues at gmail.com <mailto:benjamin.garrigues at gmail.com>> wrote:
> I think what you're saying and what Kevin is saying are in way not contradictory : 
> 
> You're saying the "SpongeBob" protocol functions make sense and are coherent, Kevin is saying the consequence is that it creates weird functions for Sets, and i think you're both right.
> 
> an "UnorderedSequence" may very well have a "first" element. The problem is that when you apply it to sets, you're not calling:
> 
> mySet.generatedRandomOrderSequence().first()
> 
> but
> 
> mySet.first()
> 
> That's where the issue stands, and that's IMHO, a symptom that the protocol hierarchy is a bit misleading.
> What you would like for sets isn't "first()", but "any()".
> 
> 'first' is just a shorthand for 'let first; for element in sequence { first = element; break }'. Any type that offers iterated access in a for...in loop must, by construction, have a definable 'first'.
> 
> Swift Sequences are deliberately allowed to be single-pass, infinite, or unordered. A multi-pass, finite Sequence is a Collection. It's not an oversight that 'first' is defined on Sequence and not Collection. It's not 'weird' to ask what the first element you get on iteration must be; by definition, iteration gives you elements sequentially and something must be first.

I don't think this really changes the point you're making, but the 'first' property is in fact defined on Collection and not Sequence. Sequence has 'first(where:)', 'dropFirst', and 'prefix' methods, and essentially the same argument applies to them.

One rationale I heard (I think from Ben Cohen) for why 'first' is not defined on Collection is that Sequences can be single-pass, and it would feel weird for a seemingly innocuous property like 'first' to have side effects.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171014/a3bbeec5/attachment.html>


More information about the swift-evolution mailing list