<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=""><br class=""><div class="">
<div class="">-Dave</div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Dec 30, 2015, at 8:48 PM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class=""><div class="">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 class=""></div></div></div></blockquote><div><br class=""></div>Those are two of the reasons. But the reason that put forEach over the line and convinced me to add it, just slightly, was syntactic:</div><div><br class=""></div><div><font face="Menlo" class="">for x in some.very.long[chain]</font></div><div><font face="Menlo" class=""> .of.map { $0 }</font></div><div><font face="Menlo" class=""> .filter { something }.whatever {</font></div><div><font face="Menlo" class=""> ...</font></div><div><font face="Menlo" class="">}</font></div><div><br class=""></div><div>reads "inside-out," like nested(free(function(calls())))) vs.</div><div><br class=""></div><div><div><font face="Menlo" class="">some.very.long[chain]</font></div><div><font face="Menlo" class=""> .of.map { $0 }</font></div><div><font face="Menlo" class=""> .filter { something }.whatever</font></div><div><font face="Menlo" class=""> .forEach { x in</font></div><div><font face="Menlo" class=""> ...</font></div><div><font face="Menlo" class=""> }</font></div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class="">
<div class=""> </div>
<div class="">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 class=""> </div>
<div class="">-Kevin Ballard<br class=""></div>
<div class=""> </div>
<div class="">On Wed, Dec 30, 2015, at 04:50 PM, Craig Cruden via swift-evolution wrote:<br class=""></div>
<blockquote type="cite" class=""><div class="">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 class=""></div>
<div class=""> </div>
<div class=""><div class=""> </div>
<div class=""><blockquote type="cite" class=""><div class="">On 2015-12-31, at 5:10:22, Howard Lovatt via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div>
<div class=""> </div>
<div class=""><div class=""><div class=""> </div>
<div class=""><div class=""><span class="">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 class=""></div>
<div class=""><div class=""> </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;" class=""><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">public</span> <span class="">final</span> <span class="">class</span> MapController<E, R> {</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">var</span> results = [R]()</span><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"></span><br class=""></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">var</span> isContinuing = <span class="">true</span></span><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"></span><br class=""></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">init</span><C: CollectionType <span class="">where</span> C.Generator.Element == E>(<span class="">_</span> collection: C, sizeEstimate: Int = <span class="">0</span>, <span class="">@noescape</span> mapper: (controller: MapController<E, R>, element: E) <span class="">throws</span> -> R?) <span class="">rethrows</span> {</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> results.reserveCapacity(sizeEstimate)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">for</span> <span class="">var</span> generator = collection.generate(), element = generator.next(); element != <span class="">nil</span> && isContinuing; element = generator.next() {</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">let</span> result = <span class="">try</span> mapper(controller: <span class="">self</span>, element: element!)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">if</span> <span class="">let</span> actualResult = result {</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> results.append(actualResult)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;" class=""> </div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">extension</span><span class=""></span>CollectionType<span class=""> {</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span>/// Controllable `map`, additional controls beyond simple `map` are:</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">///</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span>/// 1. Continue without returning a result (`return nil`)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span>/// 2. Return multiple results (`control.results += [...]` then `return nil`)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span>/// 3. Break (`control.isContinuing = false` then `return nil`)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">///</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></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 class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">@warn_unused_result</span> <span class="">func</span> map<R>(sizeEstimate sizeEstimate: Int = <span class="">0</span>, <span class="">@noescape</span> mapper: (controller: MapController<<span class="">Self</span>.Generator.Element, R>, element: <span class="">Self</span>.Generator.Element) <span class="">throws</span> -> R?) <span class="">rethrows</span> -> [<span class="">R</span>] {</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">return</span> <span class="">try</span> MapController(<span class="">self</span>, sizeEstimate: sizeEstimate, mapper: mapper).results</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;" class=""> </div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">// Demonstration of full functionality including continue, break, and multiple returns</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">var</span> result = (<span class="">0</span> ..< <span class="">5</span>).map { (control, index) -> <span class="">Int</span>? <span class="">in</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">switch</span> index {</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">case</span> <span class="">1</span>:</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span><span class="">return</span><span class=""></span><span class="">nil</span><span class=""></span>// Continue - skip 1 (`filter`)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">case</span> <span class="">2</span>:</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""> control.results.append(</span><span class="">2</span><span class="">) </span>// Yield two results - this one and the 'return’ yield (`flatMap`)</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">case</span> <span class="">3</span>:</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""> control.isContinuing = </span><span class="">false</span><span class=""></span>// Break after next yield - which could be `return nil` if there are no more results</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span>default<span class="">:</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">break</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> }</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span><span class="">return</span><span class=""> index </span>// Yield next result - except for case 1 all the above yield `index`</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">print(result) </span>// prints `[0, 2, 2, 3]` note missing "1", double "2", and last is "3"</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;min-height:13px;" class=""> </div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">// Demonstration of `repeat`/`forEach`/`times` like usage - note `(_, _) -> Void?`</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">result = [Int]()</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">(<span class="">0</span> ..< <span class="">3</span>).map { (<span class="">_</span>, <span class="">_</span>) -> <span class="">Void</span>? <span class="">in</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"> result.append(<span class="">1</span>) <span class="">// Do whatever - in this case append to a global</span></span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class=""></span><span class="">return</span><span class=""></span><span class="">nil</span><span class=""></span>// Don't yield any results</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)">}</span><br class=""></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;line-height:normal;" class=""><span class="highlight" style="background-color: rgba(255, 255, 255, 0)"><span class="">print(result) </span>// prints `[1, 1, 1]`</span><br class=""></div>
</blockquote><div class=""> </div>
</div>
<div class=""><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 class=""></div>
<div class=""> </div>
<div class=""><span class="">Sent from my iPad</span><br class=""></div>
</div>
<div class=""><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" class=""><br class=""></div>
</div>
<div class="">_______________________________________________<br class=""></div>
<div class="">swift-evolution mailing list<br class=""></div>
<div class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""></div>
<div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div>
</div>
</blockquote></div>
</div>
<div class=""><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" class=""><br class=""></div>
<div class=""><u class="">_______________________________________________</u><br class=""></div>
<div class="">swift-evolution mailing list<br class=""></div>
<div class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""></div>
<div class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div>
</blockquote><div class=""> </div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=JfMPa-2F7wwZPzsZ3QKA8NjtONIYX4SjbWuUxtpfsTY2jhBVOZsUrpUSCCviMJlR-2Bsz5q5jhE0adZU5MXVGCDmB-2B0EEVg3aOGA1akyqseQdkWMPW-2BHSwUtEJIUFlODPImVwbKCH3UAiFlQejJ7x-2FWz-2FTYQvPlPo-2FGBmeQgMpDM4VNSNAYY526onB3F-2BJVyJ3xofyYJp-2B7-2F0LfBhmA62PeDfuIA806IORG-2FntvxNG7ypgA-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>