<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>Swift didn't use to have forEach(). It was added fairly late, and I suspect (though I don't actually know) that it was done so to appease people who kept abusing map() for the same function, as well as the die-hard everything-must-be-functional crowd.<br></div>
<div> </div>
<div>Personally, I'd rather we didn't have it because it encourages people to use it, but I suppose it's better to give people an appropriate tool than to keep watching them abuse map().</div>
<div> </div>
<div>-Kevin Ballard<br></div>
<div> </div>
<div>On Wed, Dec 30, 2015, at 04:50 PM, Craig Cruden via swift-evolution wrote:<br></div>
<blockquote type="cite"><div>I don’t see the benefit of taking a simple declarative expression (map, flatMap, filter) and turning it into a complicated imperative/iterative loop. You already have the ability to iterate through a set and do whatever you want to do with with whatever logic you want to use using. I would have no problem for the most part removing foreach - it is more of a convenience method for doing an iterative loop through a collection - and to be quite honest rarely use outside of maybe putting in a print statement temporarily in there (but more often just turn the resulting set into comma delimited output and printing it). <br></div>
<div> </div>
<div><div> </div>
<div><blockquote type="cite"><div>On 2015-12-31, at 5:10:22, Howard Lovatt via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div>
<div> </div>
<div><div><div> </div>
<div><div><span>You could replace `forEach` with a supped up `map` that also allowed `break` and `continue`. The following </span><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">library function gives `continue` and `break` and also combines `repeat`, `times`, `forEach`, `filter`, `flatMap`, and `map` into one:</span><br></div>
<div><div> </div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:40px;border-top-style:none;border-right-style:none;border-bottom-style:none;border-left-style:none;border-top-width:initial;border-right-width:initial;border-bottom-width:initial;border-left-width:initial;border-top-color:initial;border-right-color:initial;border-bottom-color:initial;border-left-color:initial;border-image-source:initial;border-image-slice:initial;border-image-width:initial;border-image-outset:initial;border-image-repeat:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;"><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>public</span> <span>final</span> <span>class</span> MapController<E, R> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>var</span> results = [R]()</span><br></div>
<p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"></span><br></p><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>var</span> isContinuing = <span>true</span></span><br></div>
<p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"></span><br></p><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>init</span><C: CollectionType <span>where</span> C.Generator.Element == E>(<span>_</span> collection: C, sizeEstimate: Int = <span>0</span>, <span>@noescape</span> mapper: (controller: MapController<E, R>, element: E) <span>throws</span> -> R?) <span>rethrows</span> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> results.reserveCapacity(sizeEstimate)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>for</span> <span>var</span> generator = collection.generate(), element = generator.next(); element != <span>nil</span> && isContinuing; element = generator.next() {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>let</span> result = <span>try</span> mapper(controller: <span>self</span>, element: element!)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>if</span> <span>let</span> actualResult = result {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> results.append(actualResult)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;"> </div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>extension</span><span></span>CollectionType<span> {</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span>/// Controllable `map`, additional controls beyond simple `map` are:</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>///</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span>/// 1. Continue without returning a result (`return nil`)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span>/// 2. Return multiple results (`control.results += [...]` then `return nil`)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span>/// 3. Break (`control.isContinuing = false` then `return nil`)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>///</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></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><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>@warn_unused_result</span> <span>func</span> map<R>(sizeEstimate sizeEstimate: Int = <span>0</span>, <span>@noescape</span> mapper: (controller: MapController<<span>Self</span>.Generator.Element, R>, element: <span>Self</span>.Generator.Element) <span>throws</span> -> R?) <span>rethrows</span> -> [<span>R</span>] {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>return</span> <span>try</span> MapController(<span>self</span>, sizeEstimate: sizeEstimate, mapper: mapper).results</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;"> </div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">// Demonstration of full functionality including continue, break, and multiple returns</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>var</span> result = (<span>0</span> ..< <span>5</span>).map { (control, index) -> <span>Int</span>? <span>in</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>switch</span> index {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>case</span> <span>1</span>:</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span><span>return</span><span></span><span>nil</span><span></span>// Continue - skip 1 (`filter`)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>case</span> <span>2</span>:</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span> control.results.append(</span><span>2</span><span>) </span>// Yield two results - this one and the 'return’ yield (`flatMap`)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>case</span> <span>3</span>:</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span> control.isContinuing = </span><span>false</span><span></span>// Break after next yield - which could be `return nil` if there are no more results</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span>default<span>:</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>break</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span><span>return</span><span> index </span>// Yield next result - except for case 1 all the above yield `index`</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>print(result) </span>// prints `[0, 2, 2, 3]` note missing "1", double "2", and last is "3"</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;"> </div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">// Demonstration of `repeat`/`forEach`/`times` like usage - note `(_, _) -> Void?`</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">result = [Int]()</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">(<span>0</span> ..< <span>3</span>).map { (<span>_</span>, <span>_</span>) -> <span>Void</span>? <span>in</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> result.append(<span>1</span>) <span>// Do whatever - in this case append to a global</span></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span></span><span>return</span><span></span><span>nil</span><span></span>// Don't yield any results</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;"><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span>print(result) </span>// prints `[1, 1, 1]`</span><br></div>
</blockquote><div> </div>
</div>
<div><span class="highlight" 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?</span><br></div>
<div> </div>
<div><span>Sent from my iPad</span><br></div>
</div>
<div><img style="height:1px !important;width:1px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;margin-top:0px !important;margin-bottom:0px !important;margin-right:0px !important;margin-left:0px !important;padding-top:0px !important;padding-bottom:0px !important;padding-right:0px !important;padding-left:0px !important;" border="0" height="1" width="1" alt="" src="https://www.fastmailusercontent.com/proxy/7445381d9ca5b674d19103b63d224cc8c8cbaf32bf86ab5a54cd6f2e21081eeb/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d33474552323c4e6872695162354d43377b467a75734633797154477b4160347d437539497455376c41377a485237463267313654316c647d433d2236475c6a5a784e4a53723848785e6e624264353f4632583542376031575d60303873676550723f656a4444677f446d22364d2236495d22364442584d22364542626b6a6e687877574d22324c60577a46787262475e6d22324474374d22364c4379337430724572524234324447723276537a557766443d22324e4b624a716431577a44423d2236403a42713f657d643e446a734360705a40337e46777d4d223642665e6d66666f4d22364d443d223242733d223241377755486a5254375e67513757673d67515d22364f4d4757354d23344/open"><br></div>
</div>
<div>_______________________________________________<br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div>https://lists.swift.org/mailman/listinfo/swift-evolution<br></div>
</div>
</blockquote></div>
</div>
<div><img style="height:1px !important;width:1px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;margin-top:0px !important;margin-bottom:0px !important;margin-right:0px !important;margin-left:0px !important;padding-top:0px !important;padding-bottom:0px !important;padding-right:0px !important;padding-left:0px !important;" border="0" height="1" width="1" alt="" src="https://www.fastmailusercontent.com/proxy/9a35b4b1491caf9cffdfdc0437aba666d55c94e7c359e7b3f279bd3047125728/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d3148765176786c673171614a7d2236454230345272776e4159585a5a716d2236435231586a6a72415732616d22324a44495178523650376b6b4d4e4d40525641515054694d22364a5a625a795d435a645b4841497254393e4a767831387532687331785d6135603a4373415249647938353279784a6f44757d2232487a46613e407170793543395d623a786f6e60533a705261516b4347445d22364f4642716850585a737877463355525a6961527e463464377474665f494b474744577268405132417851675a665753584b665d44533642735f65514561773c44713861374832714d23344d23344/open"><br></div>
<div><u>_______________________________________________</u><br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
</blockquote><div> </div>
</body>
</html>