[swift-evolution] Proposal: Add SequenceType.first

Félix Cloutier felixcca at yahoo.ca
Sat Jan 2 13:10:08 CST 2016


No one appears to have considered the C# example yet. In C#, IEnumerable<T> (Swift's SequenceType) has a `First()` (and `FirstOrDefault(T)`) method.

C# also has a coroutine syntax (`yield return xyz`) that makes it easy to create sequences that are evaluated with side effects. However, Googling "c# ienumerable first", nobody seems to be wondering if the sequence will be evaluated multiple times. This true even though C# has a coroutine syntax that makes it very easy to create sequences with side effects.

I'm not passionate about whether a `first` property on sequences needs to feel "part of the language" (since that's what the stdlib is about), or how it should behave exactly. However, off the list into the world of making programs, it doesn't appear to matter much whether using `first` successively evaluates the sequence multiple times or not. If it needs to be evaluated just once, there are trivial ways to make that happen.

(I don't think that the observation can be extended to a lot of other sequence methods. This is about the specific case of `first`.)

Félix

> Le 2 janv. 2016 à 08:53:39, Gwendal Roué via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> The best interface is no interface, isn’t it? Why should we absolutely add methods with unclear meanings or behavior, when there are already perfectly clear, if verbose, alternatives? seq.generate().next() may not be nice, but no one can get fooled by it.
> 
> Maybe we should discuss use cases, instead of "completing" a standard lib that did not ask anything. It never stops: shouldn’t we add isEmpty to SequenceType, if first was added? With the same ambiguity in both meaning and behavior...
> 
> Indeed I agree that the buffered sequence is much more interesting, and that it overlaps with the implicit enhancement request behind SequenceType.first. I have one experience where I wanted to know whether a sequence was empty before consuming it, and a standard buffered sequence would have been a much welcomed tool.
> 
> Gwendal
> 
>> Le 2 janv. 2016 à 13:42, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> a écrit :
>> 
>> May I suggest a simple solution?
>> 
>> 	extension SequenceType {
>> 		/// Returns one element from the beginning of the sequence, or `nil` if the sequence is empty.
>> 		/// If `self` is a single-pass sequence, this may consume the element.
>> 		func one() -> Generator.Element? {
>> 			var generator = generate()
>> 			return generator.next()
>> 		}
>> 	}
>> 
>> This should probably be a method in the protocol, and CollectionType should have an override which calls `first`.
>> 
>> The BufferedSequence stuff suggested elsewhere is probably useful too, and should be considered in addition to this, but I think this would cover the most common case.
>> 
>> -- 
>> Brent Royal-Gordon
>> Architechies
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list