<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Garth<div class=""><br class=""><blockquote type="cite" class="">I’m not sure why you’re trying to avoid sequences - as far as the actual values you are iterating over, your needs seem to be pretty well covered by the existing stride() family.<br class=""></blockquote><br class=""> I am not avoiding per se the use of Strides & Sequence & Co.<div class=""> It is just because I simply don’t need them in my iterator functions.<br class=""><blockquote type="cite" class=""><br class="">Given that you just want to flatten the call sites, perhaps something like this would suit your needs:<br class=""><br class="">let xrange = stride(from: 0.0, to: 30.0, by: 10.0)<br class="">let yrange = stride(from: 0.0, to: 20.0, by: 5.0)<br class="">let zrange = stride(from: 10.0, to:-10.0, by:-5.0)<br class=""><br class="">for (x, y, z) in cartesianProduct(xrange, yrange, zrange) {<br class=""> print("x = \(x) y = \(y) z = \(z)")<br class=""> if z < 0.0 {<br class=""> print ( "** z value \(z) is below zero! **" )<br class=""> break<br class=""> }<br class="">}<br class="">Using strides removes the need for any of the NumericType labels, and cartesianProduct() would be usable for any (reiterable) sequences, not just your designated types. <br class=""></blockquote><br class="">True. but what I have now works. all I need was new iterators for numeric type as soon as possible </div><div class="">because the for;; will be no longer available in 3.0.</div><div class="">As a sort of spin-off I also made it for 2 and 3 dimensions.</div><div class="">For my apps this is sufficient and even better to use than the nested for;;for;; </div><div class=""><br class=""></div><div class="">I am currently trying to write an iterator that should work</div><div class="">with a variable depth of levels. A recursive approach</div><div class="">Currently it is under construction not yet working as expected.</div><div class="">I cannot wrap my brain around the recursive part. (yet)</div><div class="">will drop it for a day or two. :o) </div><div class="">You can try / improve it in your xcode playground if you like. is 2.3 but should also work in 3.0.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> IteratorParameters<T:NumericType></span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> start: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> step: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> test: (</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> iterateR<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">NumericType</span><span style="font-variant-ligatures: no-common-ligatures" class="">>( parameters: [</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">IteratorParameters</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">>], block: (</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">,[</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">]) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> currents = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">]()</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> parm </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> parameters</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> currents.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(parm.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">start</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// initialize current values with start values.</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""> <span style="font-variant-ligatures: no-common-ligatures" class=""></span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> levels = parameters.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span><span style="font-variant-ligatures: no-common-ligatures" class=""> - <font color="#272ad8" class="">1</font></span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> level = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">//——under construction————obviously still not ok here---------------------------</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> iterateLevel() </span><span style="font-variant-ligatures: no-common-ligatures" class="">// recursive nested function.</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span><span style="font-variant-ligatures: no-common-ligatures" class=""> parameters[level].</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">test</span><span style="font-variant-ligatures: no-common-ligatures" class="">(currents[level]) && block(level,currents)</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> currents[level] = currents[level] </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">+</span><span style="font-variant-ligatures: no-common-ligatures" class=""> parameters[level].</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">step</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">iterateLevel</span><span style="font-variant-ligatures: no-common-ligatures" class="">() </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// recursive</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> level = level + </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"\n"</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">//-------------------------------------------------------------------------------</span></div><p style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(49, 89, 93); font-variant-ligatures: no-common-ligatures;" class="">iterateLevel</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></p><p style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class="">}</p><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> testIteratorR()</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><p style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> itparms = [</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">IteratorParameters</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">>]()</span></div><p style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> itparms.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span><span style="font-variant-ligatures: no-common-ligatures" class="">( </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">IteratorParameters</span><span style="font-variant-ligatures: no-common-ligatures" class="">( start: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, step : </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">, test: {$0 < </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures" class="">} ) )</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> itparms.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span><span style="font-variant-ligatures: no-common-ligatures" class="">( </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">IteratorParameters</span><span style="font-variant-ligatures: no-common-ligatures" class="">( start: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, step : </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">, test: {$0 < </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures" class="">} ) )</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> itparms.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span><span style="font-variant-ligatures: no-common-ligatures" class="">( </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">IteratorParameters</span><span style="font-variant-ligatures: no-common-ligatures" class="">( start: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, step : </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">, test: {$0 < </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures" class="">} ) )</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> itparms.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span><span style="font-variant-ligatures: no-common-ligatures" class="">( </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">IteratorParameters</span><span style="font-variant-ligatures: no-common-ligatures" class="">( start: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">, step : </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">, test: {$0 < </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures" class="">} ) )</span></div><p style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">iterateR</span><span style="font-variant-ligatures: no-common-ligatures" class="">(itparms,</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> block: {lvl, values </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"level=</span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">lvl</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">) values = </span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">values</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">true</span><span style="font-variant-ligatures: no-common-ligatures" class=""> } )</span></div><p style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo; min-height: 19px;" class="">}</p></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><br class="">If you want a language issue to obsess over, I suggest the inflexibility of tuples, which force you to have a separate wrapper for each number of dimensions. :-)<br class=""></blockquote> :o) I find very little use for tuples, except perhaps when tuples are used to pass byte arrays to lower-level functions, and even then.</div><div class="">I am more and more less “obsessed” with changing and disappearing language features, because </div><div class="">in many cases, in Swift increasingly you can roll your own to replace them or even make something better, if really needed.</div><div class="">like in Smalltalk as well. <br class=""><blockquote type="cite" class=""><br class="">I would have thought there’d be an off-the-shelf cartesian product somewhere that you could use, but it doesn’t seem to come up in the Google. It’d look something like this. (This is causing a compiler crash in Xcode 8b3 and so is not completely vetted, but it’s probably close…)<br class=""></blockquote><div class=""><br class=""></div>Will try your solution (looks clever) although I don’t need the collection “by-product” Perhaps more suited for</div><div class="">in progs that have a more functional programming inclination?<br class=""><blockquote type="cite" class=""><br class="">func cartesianProduct<U, V where U: Sequence, V == U.Iterator.Element>(_ args: U...) -><br class=""> AnyIterator<[V]><br class="">{<br class=""> var iterators = args.map { $0.makeIterator() }<br class=""> var values = [V?]()<br class=""> for i in 0 ... iterators.endIndex {<br class=""> values.append(iterators[i].next())<br class=""> }<br class=""> var done = values.contains { $0 == nil }<br class=""><br class=""> return AnyIterator() {<br class=""> if done {<br class=""> return nil<br class=""> }<br class=""> let thisValue = values.map { $0! }<br class=""> var i = args.endIndex<br class=""> repeat {<br class=""> values[i] = iterators[i].next()<br class=""> if values[i] != nil {<br class=""> return thisValue<br class=""> } else if i == 0 {<br class=""> done = true<br class=""> return thisValue<br class=""> } else {<br class=""> iterators[i] = args[i].makeIterator()<br class=""> values[i] = iterators[i].next()<br class=""> i -= 1<br class=""> }<br class=""> } while true<br class=""> }<br class="">}<br class=""><br class="">func cartesianProduct<U, V where U: Sequence, V == U.Iterator.Element>(_ a: U, _ b: U, _ c: U) -><br class=""> AnyIterator<(V, V, V)><br class="">{<br class=""> var subIterator: AnyIterator<[V]> = cartesianProduct(a, b, c)<br class=""> return AnyIterator() {<br class=""> if let value = subIterator.next() {<br class=""> return (value[0], value[1], value[2])<br class=""> }<br class=""> return nil<br class=""> }<br class="">}<br class=""></blockquote><div class=""><br class=""></div><div class="">Kind Regards</div><div class="">Ted</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><blockquote type="cite" class="">Garth<br class=""><br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class=""><br class="">On Jul 30, 2016, at 1:48 PM, Ted F.A. van Gaalen via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a> <<a href="mailto:swift-evolution@swift.org" class="">mailto:swift-evolution@swift.org</a>>> wrote:<br class=""><br class=""><blockquote type="cite" class="">Hi Chris,<br class=""><br class="">thanks for the tip about Hirundo app!<br class=""><br class="">A positive side-effect of removing the classical for;; loop<br class="">(yes, it’s me saying this :o) is that it forces me to find<br class="">a good and generic equivalent for it, <br class="">making the conversion of my for;;s to 3.0 easier.<br class="">which is *not* based on collections or sequences and<br class="">does not rely on deeper calls to Sequence etc.<br class=""><br class="">so, I’ve made the functions [iterator, iterator2D, iterator3D] (hereunder)<br class="">wich btw clearly demonstrate the power and flexibility of Swift. <br class=""><br class="">Very straightforward, and efficient (i assume) just like the classical for;; loop. <br class="">It works quite well in my sources.<br class=""><br class="">As a spin-off, I’ve extended these to iterators for matrices 2D and cubes? 3D...<br class=""><br class="">Question: <br class="">Perhaps implementing “multi dimensional iterator functions<br class="">in Swift might be a good idea. so that is no longer necessary to nest/nest/nest iterators. <br class=""><br class="">Met vriendelijke groeten, sorry for my “intensity” in discussing the classical for;; <br class="">I'll have to rethink this for;; again.. <br class="">Thanks, Ted.<br class=""><br class="">Any remarks ( all ), suggestions about the code hereunder: ? <br class=""><br class="">protocol NumericType<br class="">{<br class=""> func +(lhs: Self, rhs: Self) -> Self<br class=""> func -(lhs: Self, rhs: Self) -> Self<br class=""> func *(lhs: Self, rhs: Self) -> Self<br class=""> func /(lhs: Self, rhs: Self) -> Self<br class=""> func %(lhs: Self, rhs: Self) -> Self<br class="">}<br class=""><br class="">extension Double : NumericType { }<br class="">extension Float : NumericType { }<br class="">extension CGFloat: NumericType { }<br class="">extension Int : NumericType { }<br class="">extension Int8 : NumericType { }<br class="">extension Int16 : NumericType { }<br class="">extension Int32 : NumericType { }<br class="">extension Int64 : NumericType { }<br class="">extension UInt : NumericType { }<br class="">extension UInt8 : NumericType { }<br class="">extension UInt16 : NumericType { }<br class="">extension UInt32 : NumericType { }<br class="">extension UInt64 : NumericType { }<br class=""><br class=""><br class="">/// Simple iterator with generic parameters, with just a few lines of code.<br class="">/// for most numeric types (see above)<br class="">/// Usage Example:<br class="">///<br class="">/// iterate(xmax, { $0 > xmin}, -xstep,<br class="">/// {x in<br class="">/// print("x = \(x)")<br class="">/// return true // returning false acts like a break<br class="">/// } )<br class="">///<br class="">/// -Parameter vstart: Initial value<br class="">/// -Parameter step: The iteration stepping value.<br class="">/// -Parameter test: A block with iteration test. e.g. {$0 > 10}<br class="">///<br class="">/// -Parameter block: A block to be executed with each step.<br class="">/// The block must include a return true (acts like "continue")<br class="">/// or false (acts like "break")<br class="">/// -Please Note: <br class="">/// There is minor precision loss ca: 1/1000 ... 1/500 <br class="">/// when iterating with floating point numbers.<br class="">/// However, in most cases this can be safely ignored.<br class="">/// made by ted van gaalen.<br class=""><br class=""><br class="">func iterate<T:NumericType> (<br class=""> vstart: T,<br class=""> _ vstep: T,<br class=""> _ test: (T) -> Bool,<br class=""> _ block: (T) -> Bool )<br class="">{<br class=""> var current = vstart<br class=""><br class=""> while test(current) && block(current)<br class=""> {<br class=""> current = current + vstep<br class=""> }<br class="">}<br class=""><br class=""><br class="">/// X,Y 2D matrix (table) iterator with generic parameters<br class="">func iterate2D<T:NumericType> (<br class=""> xstart: T, _ xstep: T, _ xtest: (T) -> Bool,<br class=""> _ ystart: T, _ ystep: T, _ ytest: (T) -> Bool,<br class=""> _ block: (T,T) -> Bool )<br class="">{<br class=""> var xcurrent = xstart<br class=""> var ycurrent = ystart<br class=""><br class=""> var dontStop = true<br class=""><br class=""> while xtest(xcurrent) && dontStop<br class=""> {<br class=""> ycurrent = ystart<br class=""> while ytest(ycurrent) && dontStop<br class=""> {<br class=""> dontStop = block(xcurrent, ycurrent)<br class=""> ycurrent = ycurrent + ystep<br class=""> }<br class=""> xcurrent = xcurrent + xstep<br class=""> }<br class="">}<br class=""><br class=""><br class="">/// X,Y,Z 3D (cubic) iterator with generic parameters:<br class=""><br class="">func iterate3D<T:NumericType> (<br class=""> xstart: T, _ xstep: T, _ xtest: (T) -> Bool,<br class=""> _ ystart: T, _ ystep: T, _ ytest: (T) -> Bool,<br class=""> _ zstart: T, _ zstep: T, _ ztest: (T) -> Bool,<br class=""> _ block: (T,T,T) -> Bool )<br class="">{<br class=""> var xcurrent = xstart<br class=""> var ycurrent = ystart<br class=""> var zcurrent = zstart<br class=""><br class=""> var dontStop = true<br class=""><br class=""> while xtest(xcurrent) && dontStop<br class=""> {<br class=""> ycurrent = ystart<br class=""> while ytest(ycurrent) && dontStop<br class=""> {<br class=""> zcurrent = zstart<br class=""> while ztest(zcurrent) && dontStop<br class=""> {<br class=""> dontStop = block(xcurrent, ycurrent, zcurrent)<br class=""> zcurrent = zcurrent + zstep<br class=""> }<br class=""> ycurrent = ycurrent + ystep<br class=""> }<br class=""> xcurrent = xcurrent + xstep<br class=""> }<br class="">}<br class=""><br class=""><br class="">func testIterator()<br class="">{<br class=""> iterate(0.0, 0.5, {$0 < 1000.00000} ,<br class=""> { value in<br class=""> print("Value = \(value) ")<br class=""> return true<br class=""> } )<br class=""><br class=""> let startv: CGFloat = -20.0<br class=""> let stepv: CGFloat = 0.5<br class=""><br class=""> iterate(startv, stepv, {$0 < 1000.00000} ,<br class=""> { val in<br class=""> print("R = \(val)")<br class=""> return true<br class=""> } )<br class=""><br class=""> let tolerance = 0.01 // boundary tolerance for floating point type<br class=""><br class=""> iterate2D( 0.0, 10.0, { $0 < 100.0 + tolerance } ,<br class=""> 0.0, 5.0, { $0 < 50.0 + tolerance } ,<br class=""> {x,y in<br class=""> print("x = \(x) y = \(y)")<br class=""> return true // false from block stops iterating ( like break)<br class=""> } )<br class=""><br class=""> iterate3D( 0.0, 10.0, { $0 < 30.0 } , // x<br class=""> 0.0, 5.0, { $0 < 20.0 } , // y<br class=""> 10.0, -5.0, { $0 > -10.0 } , // z<br class=""> {x,y,z in<br class=""> print("x = \(x) y = \(y) z = \(z)")<br class=""> if z < 0.0<br class=""> {<br class=""> print ( "** z value \(z) is below zero! **" )<br class=""><br class=""> return false // (acts as break in for;;)<br class=""> }<br class=""> return true // return stmt is obligatory (continue)<br class=""> } )<br class="">}<br class=""><br class=""><br class=""></blockquote></blockquote></blockquote></blockquote><blockquote type="cite" class=""><blockquote type="cite" class=""></blockquote></blockquote></div></div></body></html>