<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><span>You could replace `forEach` with a supped up `map` that also allowed `break` and `continue`. The following&nbsp;</span><font class="" style="background-color: rgba(255, 255, 255, 0);">library function gives `continue` and `break` and also combines `repeat`,&nbsp;`times`, `forEach`, `filter`, `flatMap`, and `map` into one:</font><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);"><br class=""></font><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">public</span>&nbsp;<span class="">final</span>&nbsp;<span class="">class</span>&nbsp;MapController&lt;E, R&gt; {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">var</span>&nbsp;results = [R]()</span></div><p class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp;&nbsp; &nbsp;</span></p><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">var</span>&nbsp;isContinuing =&nbsp;<span class="">true</span></span></div><p class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp;&nbsp; &nbsp;</span></p><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">init</span>&lt;C: CollectionType&nbsp;<span class="">where</span>&nbsp;C.Generator.Element == E&gt;(<span class="">_</span>&nbsp;collection: C, sizeEstimate: Int =&nbsp;<span class="">0</span>,&nbsp;<span class="">@noescape</span>&nbsp;mapper: (controller: MapController&lt;E, R&gt;, element: E)&nbsp;<span class="">throws</span>&nbsp;-&gt; R?)&nbsp;<span class="">rethrows</span>&nbsp;{</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; results.reserveCapacity(sizeEstimate)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">for</span>&nbsp;<span class="">var</span>&nbsp;generator = collection.generate(), element = generator.next(); element !=&nbsp;<span class="">nil</span>&nbsp;&amp;&amp; isContinuing; element = generator.next() {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">let</span>&nbsp;result =&nbsp;<span class="">try</span>&nbsp;mapper(controller:&nbsp;<span class="">self</span>, element: element!)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">if</span>&nbsp;<span class="">let</span>&nbsp;actualResult = result {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.append(actualResult)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);"><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">extension</span><span class="">&nbsp;</span>CollectionType<span class="">&nbsp;{</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span>/// Controllable `map`, additional controls beyond simple `map` are:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">///</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span>/// &nbsp; 1. Continue without returning a result (`return nil`)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span>/// &nbsp; 2. Return multiple results (`control.results += [...]` then `return nil`)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span>/// &nbsp; 3. Break (`control.isContinuing = false` then `return nil`)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">///</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span>/// These additional controls allow this `map` to function like `repeat`, `times`, `forEach`, `filter`, `flatMap`, and `map` combined into one as well as providing an early termination (break).</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">@warn_unused_result</span>&nbsp;<span class="">func</span>&nbsp;map&lt;R&gt;(sizeEstimate sizeEstimate: Int =&nbsp;<span class="">0</span>,&nbsp;<span class="">@noescape</span>&nbsp;mapper: (controller: MapController&lt;<span class="">Self</span>.Generator.Element, R&gt;, element:&nbsp;<span class="">Self</span>.Generator.Element)&nbsp;<span class="">throws</span>&nbsp;-&gt; R?)&nbsp;<span class="">rethrows</span>&nbsp;-&gt; [<span class="">R</span>] {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">return</span>&nbsp;<span class="">try</span>&nbsp;MapController(<span class="">self</span>, sizeEstimate: sizeEstimate, mapper: mapper).results</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);"><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">// Demonstration of full functionality including continue, break, and multiple returns</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">var</span>&nbsp;result = (<span class="">0</span>&nbsp;..&lt;&nbsp;<span class="">5</span>).map { (control, index) -&gt;&nbsp;<span class="">Int</span>?&nbsp;<span class="">in</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">switch</span>&nbsp;index {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">case</span>&nbsp;<span class="">1</span>:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span class="">return</span><span class="">&nbsp;</span><span class="">nil</span><span class="">&nbsp;</span>// Continue - skip 1 (`filter`)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">case</span>&nbsp;<span class="">2</span>:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp; &nbsp; &nbsp; control.results.append(</span><span class="">2</span><span class="">)&nbsp;</span>// Yield two results - this one and the 'return’ yield (`flatMap`)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">case</span>&nbsp;<span class="">3</span>:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp; &nbsp; &nbsp; control.isContinuing =&nbsp;</span><span class="">false</span><span class="">&nbsp;</span>// Break after next yield - which could be `return nil` if there are no more results</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span>default<span class="">:</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">break</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span><span class="">return</span><span class="">&nbsp;index&nbsp;</span>// Yield next result - except for case 1 all the above yield `index`</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">print(result)&nbsp;</span>// prints `[0, 2, 2, 3]` note missing "1", double "2", and last is "3"</span></div><div class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);"><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">// Demonstration of `repeat`/`forEach`/`times` like usage - note `(_, _) -&gt; Void?`</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">result = [Int]()</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">(<span class="">0</span>&nbsp;..&lt;&nbsp;<span class="">3</span>).map { (<span class="">_</span>,&nbsp;<span class="">_</span>) -&gt;&nbsp;<span class="">Void</span>?&nbsp;<span class="">in</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; result.append(<span class="">1</span>)&nbsp;<span class="">// Do whatever - in this case append to a global</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">&nbsp; &nbsp;&nbsp;</span><span class="">return</span><span class="">&nbsp;</span><span class="">nil</span><span class="">&nbsp;</span>// Don't yield any results</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">print(result)&nbsp;</span>// prints `[1, 1, 1]`</span></div></blockquote><font class="" style="background-color: rgba(255, 255, 255, 0);"><br class=""></font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">Would this be a superior alternative to both `forEach` and `times` in the library and `repeat` as a language feature?</font></div><span></span><br><span>Sent from my iPad</span></div></body></html>