<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="">In most programming languages, map does not have any side-effect. This is may be the reason forEach was added.<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 30, 2015, at 2:10 PM, 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=AdkfTiApI80cNEyortTzHb2fcfA247JB3qOAr5hAOEH4QfJLAaMHqR9-2F73u1HcU5UvYftXFz6Rs-2B0sjZJfOAonVd6RgRNHj2T-2BmfdCi45PGAS85R1S7kbyOe7qPd5vSubNU1iKO0oRxcZH-2F4u88CEtmJzYw2aIofPSjz-2BftW9NQq-2BOOvm0So4r7lHO6-2Bd-2BML5DcuoGtUCUYQh5HF7NxPIQ-3D-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>