anyGenerator

func anyGenerator<G : GeneratorType>(_: G)

Return a GeneratorType instance that wraps base but whose type depends only on the type of G.Element.

Example:

func countStrings() -> AnyGenerator<String> {
  let lazyStrings = (0..<10).lazy.map { String($0) }

  // This is a really complicated type of no interest to our
  // clients.
  let g: MapSequenceGenerator<RangeGenerator<Int>, String>
    = lazyStrings.generate()
  return anyGenerator(g)
}

Declaration

func anyGenerator<G : GeneratorType>(base: G) -> AnyGenerator<G.Element>
func anyGenerator<Element>(_: () -> Element?)

Return a GeneratorType instance whose next method invokes body and returns the result.

Example:

var x = 7
let g = anyGenerator { x < 15 ? x++ : nil }
let a = Array(g) // [ 7, 8, 9, 10, 11, 12, 13, 14 ]

Declaration

func anyGenerator<Element>(body: () -> Element?) -> AnyGenerator<Element>