How does Swift's for-in loop work? -
with java's stream api possible use functional internal iteration on collections, like
collection.foreach(out::println)
is swift's following for-each loop construct...
for in names { println(i) }
...merely syntactic sugar internal (functional) iteration paraphrased following imperative loop?
for var = 0; < names.count; i++ { println(names[i]) }
the language reference states for
-in
loop,
the
generate()
method called on collection expression obtain value of generator type—that is, type conformsgenerator
protocol. program begins executing loop callingnext()
method on stream. if value returned notnone
, assigned item pattern, program executes statements, , continues execution @ beginning of loop. otherwise, program not perform assignment or execute statements, , finished executingfor
-in
statement.
so, can that
for in names { println(i) }
is equivalent to
var g = names.generate() // "var" because next() mutating function while let = g.next() { // "let" pattern because next() returns optional println(i) }
as functional iteration, well, have sequencetype's func map<t>(_ transform: (element) -> t) -> [t]
.
Comments
Post a Comment