With your method, don't you need to write a new method for every dimension?<br><div class="gmail_quote"><div dir="ltr">On Mon, Aug 1, 2016 at 08:43 Ted F.A. van Gaalen via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi Erica</div><div><br></div><div>That would also be a workable solution, but imho still too tedious as </div><div>it has been so for many years, with nested iteration statements…</div><div>many times this is for;; for;; for tables and for;;for;;for;; for 3D structuring.</div><div><span style="color:rgb(49,89,93);font-family:Menlo;font-size:16px"> </span></div><div><br></div><div>Swift offers nice features (like protocols and generics as you know) to improve this. </div><div><br></div><div>- I no longer have to nest for;; s so, Now I rather do this: </div><div>Your example can already be coded like this:</div><div><br></div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="color:#31595d"> iterate2D</span><span>( </span><span>1, 2</span><span>, { $0 < 64 } ,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> 1, 2</span><span>, { $0 < 64 } ,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"> {outerIndex,innerIndex in</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> print (outerIndex,InnerIndex)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(187,44,162)">true // Obligatory. return “false" to break</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> } )</span></div></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px">With no chances in Swift, this already works like a charm!</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px">Imho much more readable and compact,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px"><br></span></div><div style="margin:0px;line-height:normal">Uses - AFAICS from a programmers’s perspective - no </div><div style="margin:0px;line-height:normal">underlying deep collection based coding with Sequence. etc.</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px"><br></span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px">I am already deploying it in my own apps e.g. l</span></div><div style="margin:0px;line-height:normal">(replaced for;; for;; for;; in the app) </div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">By using this new iterator…() functions my coding gets leaner </div><div style="margin:0px;line-height:normal">and errors are easier to spot. </div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">Actual working code with Swift 2.x here: </div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px"><br></span></div><div style="margin:0px;line-height:normal"><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">func</span><span> generateTiles()</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> {</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> w: </span><span style="color:#703daa">Float</span><span> = </span><span style="color:#272ad8">20</span><span> </span><span style="color:#008400">// tile size</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> h: </span><span style="color:#703daa">Float</span><span> = </span><span style="color:#272ad8">5</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> l: </span><span style="color:#703daa">Float</span><span> = </span><span style="color:#272ad8">5</span></div><p style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal;min-height:19px"><span> </span><br></p><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> xstart: </span><span style="color:#703daa">Float</span><span> = -</span><span style="color:#272ad8">120</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> ystart: </span><span style="color:#703daa">Float</span><span> = -</span><span style="color:#272ad8">60</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> zstart: </span><span style="color:#703daa">Float</span><span> = -</span><span style="color:#272ad8">10</span></div><p style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal;min-height:19px"><span> </span><br></p><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> xend: </span><span style="color:#703daa">Float</span><span> = </span><span style="color:#272ad8">120</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> yend: </span><span style="color:#703daa">Float</span><span> = </span><span style="color:#272ad8">60</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">let</span><span> zend: </span><span style="color:#703daa">Float</span><span> = </span><span style="color:#272ad8">10</span></div><p style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal;min-height:19px"><span> </span><br></p><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="color:#000000"> </span><span style="color:#bb2ca2">let</span><span style="color:#000000"> tolerance:</span><span style="color:#703daa">Float</span><span style="color:#000000"> = </span><span style="color:#272ad8">0.001</span><span style="color:#000000"> </span><span>// float drift compensation</span></div><p style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal;min-height:19px"><span> </span><br></p><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#31595d">iterate3D</span><span>( xstart, w * </span><span style="color:#272ad8">1.2</span><span>, { $0 < xend + tolerance } ,</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> ystart, h * </span><span style="color:#272ad8">1.2</span><span>, { $0 < yend + tolerance } ,</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> zstart, l * </span><span style="color:#272ad8">1.2</span><span>, { $0 < zend + tolerance } ,</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> {</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> x,y,z </span><span style="color:#bb2ca2">in</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">self</span><span>.</span><span style="color:#31595d">addTile</span><span>(x,y,z, </span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> w,h,l)</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> </span><span style="color:#bb2ca2">return</span><span> </span><span style="color:#bb2ca2">true</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> } )</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span> }</span></div><div style="font-family:Menlo;font-size:16px"><span><br></span></div><div style="font-family:Menlo;font-size:16px"><span style="font-family:Helvetica;font-size:14px">This generates a group of blocks or tiles in my Apple TV app (under construction)</span></div><div>like the one you can see in the image “Cloinckz” on my website <a href="http://www.tedvg.com" target="_blank">www.tedvg.com</a>.</div><div><br></div><div>I also prefer the one dimensional iterate(.. too above the for;; or stride()</div><div><br></div><div>One could extend these iterator…() functions by adding closures for pre and post iteration handling,</div><div>like for printing headers and footers before and/or after a complete [inner] iteration.</div><div><br></div><div>Note that breaking with *return false* -which is equivalent to the “break” stmt in</div><div>a classical for;; - does not only leave a nested iteration, but also the outer ones.</div><div>(maybe a TODO to make individual level- break possible) As it is now, If one </div><div>wants to break at the deep iteration level, then one should nest this using 1D iterators.</div><div><br></div><div><br></div><div>Anyway, this is just a thought starting to think about multi-dimensional iterators,</div><div>and other (encapsulation?) of multi dimensional data as well.</div><div>In any case for 2D because table data is used very frequently in many apps. as</div><div>most data are in tables.</div><div><br></div><div><div>You won’t believe this :o) but in a sense I might not make so much fuzz anymore</div><div>to keep the for;; as this proves to me that I can solve things much better that I thought.</div></div><div>So, I have to rethink this.</div><div><br></div><div>Kind Regards</div><div>Ted </div></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px"><a href="http://www.ravelnotes.com" target="_blank">www.ravelnotes.com</a></span></div></div><div style="word-wrap:break-word"><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px"><br></span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:14px"> </span></div><div><span><br></span></div><div><br></div><div><br></div><div><br></div><br><div><blockquote type="cite"><div>On 31.07.2016, at 18:33, Erica Sadun <<a href="mailto:erica@ericasadun.com" target="_blank">erica@ericasadun.com</a>> wrote:</div><br><div><div style="word-wrap:break-word"><div>I'm replying on Swift-Users and bcc'ing in Swift-Evolution to comply with the core team's request to focus SE on the current mission statement. </div><div><br></div><div>At some point soon, Russ Bishop's PR <a href="https://github.com/apple/swift/pull/3600" target="_blank">https://github.com/apple/swift/pull/3600</a> will be incorporated into Swift 3. This PR adds `prefix(while:)` and `drop(while:)` to finish implementing SE-0045. Once that's done, you can combine `sequence(first:, next:)` and `prefix(while:)` to into a single function `sequence(first:, next:, while:)` like this:</div><div><br></div><div><div><font face="Menlo">public func sequence<T>(first: T, next: (T) -> T?, while test: (T) -> Bool) -> UnfoldSequence<T, UnfoldSequence<T, (T?, Bool)>> {</font></div><div><font face="Menlo"> return sequence(first: first, next: next).prefix(while: test)</font></div><div><font face="Menlo">}</font></div><div><br></div><div>The combined sequence/prefix call allows you to create loops like this:</div><div><br></div><div><div><font face="Menlo">for outerIndex in sequence(first: 1, next: { $0 * 2 }, while: { $0 <= 64 }) {</font></div><div><font face="Menlo"> for innerIndex in sequence(first: 1, next: { $0 * 2 }, while: { $0 <= 64 }) {</font></div><div><font face="Menlo"> print(outerIndex, innerIndex)</font></div><div><font face="Menlo"> }</font></div><div><font face="Menlo">}</font></div></div></div><div><br></div><div>These loops can be nested. break and continue work. You can use tuples for multiple arguments. While I'd like to see a combined form adopted into Swift 4b (the deferred "sugar"), it isn't a high priority.</div><div><br></div><div>-- E</div><div><br></div><br><div><blockquote type="cite"><div>On Jul 31, 2016, at 7:18 AM, Ted F.A. van Gaalen via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On 31.07.2016, at 04:28, <a href="mailto:jaden.geller@gmail.com" target="_blank">jaden.geller@gmail.com</a> wrote:</div><br><div><div dir="auto"><div></div><div>What benefit do Iterator2D and Iterator3D provide that nesting does not?</div></div></div></blockquote>Hi Jaden,</div><div>well, simply because of hiding/enclosing repetitive functionality</div><div>like with every other programming element is convenient,</div><div>prevents errors and from writing the same over and over again.</div><div>That is why there are functions.</div><div>but you already know that, of course.</div><div>Kind Regards</div><div>TedvG</div><div><br></div><div><blockquote type="cite"><div><div dir="auto"><div><br>On Jul 30, 2016, at 1:48 PM, Ted F.A. van Gaalen via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div>Hi Chris,<div><br><div>thanks for the tip about Hirundo app!</div></div><div><br></div><div><div>A positive side-effect of removing the classical for;; loop</div><div> (yes, it’s me saying this :o) is that it forces me to find</div><div>a good and generic equivalent for it, </div><div>making the conversion of my for;;s to 3.0 easier.</div><div>which is *not* based on collections or sequences and</div><div>does not rely on deeper calls to Sequence etc.</div><div><br></div><div>so, I’ve made the functions [iterator, iterator2D, iterator3D] (hereunder)</div><div>wich btw clearly demonstrate the power and flexibility of Swift. </div><div><br></div><div>Very straightforward, and efficient (i assume) just like the classical for;; loop. </div><div>It works quite well in my sources.</div><div><br></div></div><div>As a spin-off, I’ve extended these to iterators for matrices 2D and cubes? 3D...</div><div><br></div><div>Question: </div><div>Perhaps implementing “multi dimensional iterator functions</div><div>in Swift might be a good idea. so that is no longer necessary to nest/nest/nest iterators. </div><div><br></div><div>Met vriendelijke groeten, sorry for my “intensity” in discussing the classical for;; </div><div>I'll have to rethink this for;; again.. </div><div>Thanks, Ted.</div><div><br></div><div>Any remarks ( all ), suggestions about the code hereunder: ? </div><div><br></div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">protocol</span><span> NumericType</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>{</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">func</span><span> +(lhs: </span><span style="color:rgb(79,129,135)">Self</span><span>, rhs: </span><span style="color:rgb(79,129,135)">Self</span><span>) -> </span><span style="color:rgb(112,61,170)">Self</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">func</span><span> -(lhs: </span><span style="color:rgb(79,129,135)">Self</span><span>, rhs: </span><span style="color:rgb(79,129,135)">Self</span><span>) -> </span><span style="color:rgb(112,61,170)">Self</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">func</span><span> *(lhs: </span><span style="color:rgb(79,129,135)">Self</span><span>, rhs: </span><span style="color:rgb(79,129,135)">Self</span><span>) -> </span><span style="color:rgb(112,61,170)">Self</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">func</span><span> /(lhs: </span><span style="color:rgb(79,129,135)">Self</span><span>, rhs: </span><span style="color:rgb(79,129,135)">Self</span><span>) -> </span><span style="color:rgb(112,61,170)">Self</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">func</span><span> %(lhs: </span><span style="color:rgb(79,129,135)">Self</span><span>, rhs: </span><span style="color:rgb(79,129,135)">Self</span><span>) -> </span><span style="color:rgb(112,61,170)">Self</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">Double</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">Float</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">CGFloat</span><span>: </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">Int</span><span> : </span><span style="color:rgb(79,129,135)">NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">Int8</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">Int16</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">Int32</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">Int64</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">UInt</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">UInt8</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">UInt16</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">UInt32</span><span> : </span><span>NumericType</span><span> { }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span> </span><span style="color:rgb(112,61,170)">UInt64</span><span> : </span><span>NumericType</span><span> { }</span></div><div><span><br></span></div></div><div><br></div><div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// Simple iterator with generic parameters, with just a few lines of code.</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">/// for most numeric types (see above)</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// Usage Example:</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>///</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// iterate(xmax, { $0 > xmin}, -xstep,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// {x in</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// print("x = \(x)")</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// return true // returning false acts like a break</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// } )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>///</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// -Parameter vstart: Initial value</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// -Parameter step: The iteration stepping value.</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// -Parameter test: A block with iteration test. e.g. {$0 > 10}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>///</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// -Parameter block: A block to be executed with each step.</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// The block must include a return true (acts like "continue")</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// or false (acts like "break")</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><div style="margin:0px;line-height:normal"><span>/// -Please Note: </span></div><div style="margin:0px;line-height:normal"><span>/// There is minor precision loss ca: 1/1000 ... 1/500 </span></div><div style="margin:0px;line-height:normal"><span>/// when iterating with floating point numbers.</span></div><div style="margin:0px;line-height:normal"><span>/// However, in most cases this can be safely ignored.</span></div><div style="margin:0px;line-height:normal"><span>/// made by ted van gaalen.</span></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><br></div></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span><span> iterate<T:</span><span style="color:rgb(79,129,135)">NumericType</span><span>> (</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> vstart: </span><span style="color:rgb(79,129,135)">T</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> vstep: </span><span style="color:rgb(79,129,135)">T</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> test: (</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> block: (</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span> )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>{</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> current = vstart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">while</span><span> test(current) && block(current)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> current = current </span><span style="color:rgb(49,89,93)">+</span><span> vstep</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// X,Y 2D matrix (table) iterator with generic parameters</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span><span> iterate2D<T:</span><span style="color:rgb(79,129,135)">NumericType</span><span>> (</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> xstart: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> xstep: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> xtest: (</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> ystart: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> ystep: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> ytest: (</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> block: (</span><span style="color:rgb(79,129,135)">T</span><span>,</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span> )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>{</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> xcurrent = xstart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> ycurrent = ystart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> dontStop = </span><span style="color:rgb(187,44,162)">true</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">while</span><span> xtest(xcurrent) && dontStop</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> ycurrent = ystart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">while</span><span> ytest(ycurrent) && dontStop</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> dontStop = block(xcurrent, ycurrent)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> ycurrent = ycurrent </span><span style="color:rgb(49,89,93)">+</span><span> ystep</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> xcurrent = xcurrent </span><span style="color:rgb(49,89,93)">+</span><span> xstep</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>/// X,Y,Z 3D (cubic) iterator with generic parameters:</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span><span> iterate3D<T:</span><span style="color:rgb(79,129,135)">NumericType</span><span>> (</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> xstart: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> xstep: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> xtest: (</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> ystart: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> ystep: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> ytest: (</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> zstart: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> zstep: </span><span style="color:rgb(79,129,135)">T</span><span>, </span><span style="color:rgb(187,44,162)">_</span><span> ztest: (</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span>,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">_</span><span> block: (</span><span style="color:rgb(79,129,135)">T</span><span>,</span><span style="color:rgb(79,129,135)">T</span><span>,</span><span style="color:rgb(79,129,135)">T</span><span>) -> </span><span style="color:rgb(112,61,170)">Bool</span><span> )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>{</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> xcurrent = xstart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> ycurrent = ystart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> zcurrent = zstart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">var</span><span> dontStop = </span><span style="color:rgb(187,44,162)">true</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">while</span><span> xtest(xcurrent) && dontStop</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> ycurrent = ystart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">while</span><span> ytest(ycurrent) && dontStop</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> zcurrent = zstart</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">while</span><span> ztest(zcurrent) && dontStop</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> dontStop = block(xcurrent, ycurrent, zcurrent)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> zcurrent = zcurrent </span><span style="color:rgb(49,89,93)">+</span><span> zstep</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> ycurrent = ycurrent </span><span style="color:rgb(49,89,93)">+</span><span> ystep</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> xcurrent = xcurrent </span><span style="color:rgb(49,89,93)">+</span><span> xstep</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span style="color:rgb(187,44,162)">func</span><span> testIterator()</span><span></span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span>{</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span> </span><span> </span><span style="color:rgb(49,89,93)">iterate</span><span>(</span><span style="color:rgb(39,42,216)">0.0</span><span>, </span><span style="color:rgb(39,42,216)">0.5</span><span>, {$0 < </span><span style="color:rgb(39,42,216)">1000.00000</span><span>} ,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> { value </span><span style="color:rgb(187,44,162)">in</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(61,29,129)">print</span><span>(</span><span style="color:rgb(209,47,27)">"Value = </span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>value</span><span style="color:rgb(209,47,27)">) </span><span style="color:rgb(209,47,27)">"</span><span>)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(187,44,162)">true</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> } )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span> </span><span style="color:rgb(187,44,162)">let</span><span> startv: </span><span style="color:rgb(112,61,170)">CGFloat</span><span> = -</span><span style="color:rgb(39,42,216)">20.0</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">let</span><span> stepv: </span><span style="color:rgb(112,61,170)">CGFloat</span><span> = </span><span style="color:rgb(39,42,216)">0.5</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(49,89,93)">iterate</span><span>(startv, stepv, {$0 < </span><span style="color:rgb(39,42,216)">1000.00000</span><span>} ,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> { val </span><span style="color:rgb(187,44,162)">in</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(61,29,129)">print</span><span>(</span><span style="color:rgb(209,47,27)">"R = </span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>val</span><span style="color:rgb(209,47,27)">)"</span><span>)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(187,44,162)">true</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> } )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span> </span><span style="color:rgb(187,44,162)">let</span><span> tolerance = </span><span style="color:rgb(39,42,216)">0.01</span><span> </span><span style="color:rgb(0,132,0)">// boundary tolerance for floating point type</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(49,89,93)">iterate2D</span><span>( </span><span style="color:rgb(39,42,216)">0.0</span><span>, </span><span style="color:rgb(39,42,216)">10.0</span><span>, { $0 < </span><span style="color:rgb(39,42,216)">100.0</span><span> + tolerance } ,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(39,42,216)">0.0</span><span>, </span><span style="color:rgb(39,42,216)">5.0</span><span>, { $0 < </span><span style="color:rgb(39,42,216)">50.0</span><span> + tolerance } ,</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {x,y </span><span style="color:rgb(187,44,162)">in</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(61,29,129)">print</span><span>(</span><span style="color:rgb(209,47,27)">"x = </span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>x</span><span style="color:rgb(209,47,27)">) y = </span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>y</span><span style="color:rgb(209,47,27)">)"</span><span>)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(187,44,162)">true</span><span> </span><span>// false from block stops iterating ( like break)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> } )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span> </span><span style="color:rgb(49,89,93)">iterate3D</span><span>( </span><span style="color:rgb(39,42,216)">0.0</span><span>, </span><span style="color:rgb(39,42,216)">10.0</span><span>, { $0 < </span><span style="color:rgb(39,42,216)">30.0</span><span> } , </span><span style="color:rgb(0,132,0)">// x</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(39,42,216)">0.0</span><span>, </span><span style="color:rgb(39,42,216)">5.0</span><span>, { $0 < </span><span style="color:rgb(39,42,216)">20.0</span><span> } , </span><span style="color:rgb(0,132,0)">// y</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(39,42,216)">10.0</span><span>, -</span><span style="color:rgb(39,42,216)">5.0</span><span>, { $0 > -</span><span style="color:rgb(39,42,216)">10.0</span><span> } , </span><span style="color:rgb(0,132,0)">// z</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {x,y,z </span><span style="color:rgb(187,44,162)">in</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(61,29,129)">print</span><span>(</span><span style="color:rgb(209,47,27)">"x = </span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>x</span><span style="color:rgb(209,47,27)">) y = </span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>y</span><span style="color:rgb(209,47,27)">) z = </span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>z</span><span style="color:rgb(209,47,27)">)"</span><span>)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">if</span><span> z < </span><span style="color:rgb(39,42,216)">0.0</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span> </span><span style="color:rgb(61,29,129)">print</span><span> ( </span><span>"** z value </span><span>\</span><span>(</span><span>z</span><span>) is below zero! **"</span><span> )</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span> </span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(187,44,162)">false</span><span> </span><span>// (acts as break in for;;)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span> }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(187,44,162)">true</span><span> </span><span>// return stmt is obligatory (continue)</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"> } )</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px">}</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span></span><br></div></div><div><br></div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></div></blockquote></div><br></div>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div>