Custom Enumerated Sequences in Just 1 Line of Code
In “Creating a Cheap Protocol-Oriented Copy of SequenceType (with a Twist!)” I created my own indexedEnumerate()
to return a special kind of enumeration: instead of a tuple of (Int, Element)
, I wanted to have a tuple of (Index, Element)
, where Index
was a UInt starting at 1. A custom CollectionType
can have a different type of index than Int
, so I was quite disappointed that the default enumerate()
simply returns a sequence with a counter for the elements at first.
Creating a Cheap Protocol-Oriented Copy of SequenceType (with a Twist!)
When you implement CollectionType
or SequenceType
in custom types, you get a lot for free with just a few steps, like being able to use map
on the collection. Any CollectionType
implementation comes with enumerate()
which, when you iterate over the result, wraps each element of the collection in a tuple with its index: