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 conforms generator protocol. program begins executing loop calling next() method on stream. if value returned not none, assigned item pattern, program executes statements, , continues execution @ beginning of loop. otherwise, program not perform assignment or execute statements, , finished executing for-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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -