The syntax can be used in the types which conform to SequenceType. And those types should conform to GeneratorType first. For example, a reversed generator and the corresponding sequence could be written as:

// 1
class ReverseGenerator<T>: GeneratorType {
    typealias Element = T

    var array: [Element]
    var currentIndex = 0

    init(array: [Element]) {
        self.array = array
        currentIndex = array.count - 1

    func next() -> Element? {
        if currentIndex < 0{
            return nil
        else {
            let element = array[currentIndex]
            currentIndex -= 1
            return element

// 2
struct ReverseSequence<T>: SequenceType {
    var array: [T]

    init (array: [T]) {
        self.array = array

    typealias Generator = ReverseGenerator<T>
    func generate() -> Generator {
        return ReverseGenerator(array: self.array)

let arr = [0,1,2,3,4]

// 3
for i in ReverseSequence(array: arr) {
    print("Index \(i) is \(arr[i])")
  1. First we use a type that conforms to GeneratorType protocol. A typealias Element should be specified in GeneratorType, and then we should implement a method named next() to return an Element?.
  2. Now we can define the SequenceType. Similar to the GeneratorType,but specifying a Generator typealias instead. At last, we implement a generate() method to return a Generator?.
  3. Finally we can use to access the SequenceType in a loop.

The output is

Index 4 is 4
Index 3 is 3
Index 2 is 2
Index 1 is 1
Index 0 is 0

Let's expand the syntax to see what happens under the hood. The equivalent is:

var g = array.generate()
while let obj = {

By the way, you will be able to use map, filter and reduce on it for free if you make it conform to SequenceType, since all of them are already defined in Swift standard library with a protocol extension of SequenceType:

extension SequenceType {
    func map<T>(@noescape transform: (Self.Generator.Element) -> T) -> [T]
    func filter(@noescape includeElement: (Self.Generator.Element) -> Bool) 
                                -> [Self.Generator.Element]
    func reduce<T>(initial: T, 
         @noescape combine: (T, Self.Generator.Element) -> T) -> T