News for hackers. Stuff that matters.
Re:Swift Standard Library: Generators, Sequences and Collect... by melling on Tuesday December 30, 2014 @01:23PM (#8815845)
If you're learning Swift, I'm building a database of Swift resources to make the process easier.

http://www.h4labs.com/dev/ios/swift.html

And I just posted it to HN here:

https://news.ycombinator.com/item?id=8815739

Re:Swift Standard Library: Generators, Sequences and Collect... by wkcamp on Tuesday December 30, 2014 @02:05PM (#8816076)
Could you perhaps add this to Github so others can make contributions to the database?
Re:Swift Standard Library: Generators, Sequences and Collect... by melling on Tuesday December 30, 2014 @02:32PM (#8816233)
I probably should create an input screen, etc. I'm not familiar with AppEngine's datastore though. Anyway, I put the tsv on github.

https://github.com/melling/SwiftResource

The data is here:

https://github.com/melling/SwiftResource/blob/master/swift_u...

For this to work, I probably need scripts to prevent duplicates, etc.

Re:Swift Standard Library: Generators, Sequences and Collect... by briandek on Tuesday December 30, 2014 @01:58PM (#8816032)
this is a great collection. Thanks!
Re:Swift Standard Library: Generators, Sequences and Collect... by tlrobinson on Tuesday December 30, 2014 @02:15PM (#8816143)
"So a generator is simply something that can give you the next element of some sequence of elements."

Is this a common usage of the word "generator"?

At least in JavaScript and Python you'd call that an "iterator", and a "generator" is a special type of function that acts as an iterator, using the "yield" keyword.

Re:Swift Standard Library: Generators, Sequences and Collect... by nickfargo on Tuesday December 30, 2014 @03:51PM (#8816672)
A generator is an iterator created and returned by a generator function.
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