[swift-evolution] Proposal: Add SequenceType.first

Gwendal Roué gwendal.roue at gmail.com
Sun Jan 3 00:48:09 CST 2016


> Le 3 janv. 2016 à 01:58, Kevin Ballard <kevin at sb.org> a écrit :
> 
>>> seq.generate().next() may not be nice, but no one can get fooled by it.
>> 
>> Well, for one thing, because it doesn't work. […]
> 
> Exactly. If `seq.generate().next()` worked, I'd be perfectly happy with that. […]

Right.

> Le 3 janv. 2016 à 06:50, Brent Royal-Gordon <brent at architechies.com> a écrit :
> 
>> If I don’t sound sympathetic, it’s because nobody has shown a use-case for this functionality, and until I see one I am going to have a hard time believing there’s a problem worth solving.  If you want to make the case that we need something like this, please show me why. 
> 
> Didn't this thread start off with a use case?
> 
> 	seq.lazy.filter(predicate).first		// is not actually lazy, and Swift provides no good way to do this
> 
> One way to fix this is to add `first` to `SequenceType`, but it feels strange for a property to potentially consume part of the sequence. `buffered` ultimately has the same problem. By representing this as a function, it at least looks like something that might have side effects.

Out of honesty, here is another use case: `database.fetch(…).first`.

Since this is not possible today, database APIs have to expose another `fetchFirst()` (fetchOne, pluck, whatever) method, that uses a temporary generator on the sequence returned by the fetch() method.

If sequence.first would exist, the database API would not have to define this extra method.

Gwendal



More information about the swift-evolution mailing list