I was implementing my own Stack collection based on the WDDC session video. After implementing Sequence protocol, I thought that this look like a generic code.

extension Stack : Sequence {
 func generate() -> StackGenerator<T> {
    return StackGenerator(items: items[0..<items.count])
  }
}

struct StackGenerator<T> : Generator {
   mutating func next() -> T? {
    if items.isEmpty { return nil }
    let ret = items[0]
    items = items[1..<items.count]
    return ret
  }

  var items: Slice<T>
}

I've start looking for alternative. There is SequenceOf<T> and GeneratorOf<T>in Swift Let's use that

extension Stack : Sequence {
 func generate() -> GeneratorOf<T> {

  var items = self.items[0..<self.items.count]
  return GeneratorOf({
    if items.isEmpty { return nil }
        let ret = items[0]
        items = items[1..<items.count]
        return ret
    })
  }
}

This looks better, but not perfect. My Stack is using Arrray as storage. Array has adopted Sequence already. Maybe we could use it's implementation. I found an article that does exactly what I want

extension Stack : Sequence {
 func generate() -> IndexingGenerator<[T]> {
    return items.generate()
  }
}