<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="">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). <div class=""><br class=""></div><div class=""> <br class=""><div><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:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><span class=""></span></div><div class=""><span class="">You could replace `forEach` with a supped up `map` that also allowed `break` and `continue`. The following </span><font class="" 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:</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);" class=""><span class="">public</span> <span class="">final</span> <span class="">class</span> MapController<E, R> {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">var</span> results = [R]()</span></div><p class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> </span><br class="webkit-block-placeholder"></p><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">var</span> isContinuing = <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);" class=""> </span><br class="webkit-block-placeholder"></p><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <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></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> results.reserveCapacity(sizeEstimate)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">for</span> <span class="">var</span> generator = collection.generate(), element = generator.next(); element != <span class="">nil</span> && isContinuing; element = generator.next() {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">let</span> result = <span class="">try</span> mapper(controller: <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);" class=""> <span class="">if</span> <span class="">let</span> actualResult = result {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> results.append(actualResult)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">}</span></div><div class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class="">extension</span><span class=""> </span>CollectionType<span class=""> {</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class=""> </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);" class=""> <span class="">///</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class=""> </span>/// 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);" class=""><span class=""> </span>/// 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);" class=""><span class=""> </span>/// 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);" class=""> <span class="">///</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><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></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <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></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">return</span> <span class="">try</span> 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);" class=""> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">}</span></div><div class="" style="margin: 0px; line-height: normal; min-height: 13px;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">// 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);" class=""><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></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">switch</span> index {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">case</span> <span class="">1</span>:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class=""> </span><span class="">return</span><span class=""> </span><span class="">nil</span><span class=""> </span>// Continue - skip 1 (`filter`)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">case</span> <span class="">2</span>:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class=""> control.results.append(</span><span class="">2</span><span class="">) </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);" class=""> <span class="">case</span> <span class="">3</span>:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><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></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class=""> </span>default<span class="">:</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">break</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class=""> </span><span class="">return</span><span class=""> index </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);" class="">}</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class="">print(result) </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);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">// Demonstration of `repeat`/`forEach`/`times` like usage - note `(_, _) -> Void?`</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">result = [Int]()</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">(<span class="">0</span> ..< <span class="">3</span>).map { (<span class="">_</span>, <span class="">_</span>) -> <span class="">Void</span>? <span class="">in</span></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""> result.append(<span class="">1</span>) <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);" class=""><span class=""> </span><span class="">return</span><span class=""> </span><span class="">nil</span><span class=""> </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);" class="">}</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class="">print(result) </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 class=""></span><br class=""><span class="">Sent from my iPad</span></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=CGU22LnxbYa2EM3wKvzuC6syQDwKa0tMs5IyT5gL1wJX2G6b71V4altM3-2FWlZzHNZs2HxXnnBb45O6R8E2g0QWm00xcgUp2oeJDdwOd-2F-2FY-2FDRH-2FEbbkjnxxWG-2BlPwJvxbBWn-2Bt4G-2FLs9s4pBuRB24BDw2rVsZugF4-2BNkBza4QwJD2-2F0Jr1oum4NdzCcpPJ0sNvwM-2FbVnmffO-2FM4-2Br3-2B1wWEhZR4WnW1Wg7mWQ-2FOMW7E-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=""></div></body></html>