[swift-evolution] Proposal: Add SequenceType.first

Brent Royal-Gordon brent at architechies.com
Sat Jan 2 06:42:46 CST 2016


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



More information about the swift-evolution mailing list