Let's say I have this code:
func work<S: Sequence>(sequence: S) {
// do stuff
}
How could I figure out how many elements there are in sequence
?
The obvious version I'd go for is pretty inefficient:
var count = 0
for element in sequence {
count += 1
}
There must be a nicer way, right?
I do not think that there is a better method for an arbitrary type conforming to
SequenceType
. The only thing that is known about a sequence is that is has agenerate()
method returning aGeneratorType
, which in turn has anext()
method. Thenext()
method advances to the next element of the sequence and returns it, or returnsnil
if there is no next element.Note that it is not required at all that
next()
eventually returnsnil
: a sequence may have "infinite" elements.Therefore enumerating the sequence is the only method to count its elements. But this need not terminate. Therefore the answer could also be: A function taking a sequence argument should not need to know the total number of elements.
For types conforming to
CollectionType
you can use thecountElements()
function (renamed tocount()
in Swift 1.2).There is also
underestimateCount()
:but that does not necessarily return the exact number of elements.
A more "functional" approach would be:
Or even make an extension:
Note This does still suffer from the basic sequence problem that you have to consume the sequence to count it.