News for hackers. Stuff that matters.
Re:Swift Standard Library: Generators, Sequences and Collect... by bigdubs on Tuesday December 30, 2014 @03:18PM (#8816513)
the Sequence / Generator pattern in swift is analogous to IEnumerable, IEnumerator in .net, though I've found it's harder to implement.
Re:Swift Standard Library: Generators, Sequences and Collect... by suzuki on Thursday January 01, 2015 @08:17AM (#8822835)
Yes indeed. Swift lacks the "yield return" statement of C#. Yet you can do well with "SequenceOf" and "GeneratorOf".

// Fibonacci sequence

let fib = SequenceOf { () -> GeneratorOf<Int> in

    var a = 0, b = 1

    return GeneratorOf {
        () -> Int? in
        let c = a + b
        a = b
        b = c
        return a
    }
}

// Print Fibonacci numbers <= 1000

for e in fib {

    if e > 1000 {
        break
    }

    print(" \(e)")
}

println()

$ xcrun swift fib_test.swift

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

See http://www.oki-osk.jp/esc/swift/linq.html