<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=""><div class="">You can already write this:</div><div class=""><br class=""></div><div class="">extension IntegerType {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func times(block: () -&gt; ()) {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>for _ in 0..&lt;self {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>block()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class="">}</div><div class=""><br class=""></div><div class="">10.times { print("hello") }</div><div class=""><br class=""></div><div class="">I don't know how I feel about adding that to the standard library.</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Le 18 déc. 2015 à 13:25:59, Cihat Gündüz via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Dear Swift-Community,<div class=""><br class=""></div><div class="">I’d like to propose an <b class="">addition of a useful method</b>, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).</div><div class=""><br class=""></div><div class="">For example recently in one of my projects I wanted to test the scalability of an important piece of code and wrote this method:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> testPerfQualityInPercentWithoutQualityImprovements() {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="" class="">.</span>measureBlock<span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> expectedQuality = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">33.33</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">stride</span>(to: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5_000</span>, by: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>).<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">forEach&nbsp;</span>{ <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">XCTAssertEqualWithAccuracy</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">crossword</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">qualityInPercent</span>, expectedQuality, accuracy: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0.1</span>) &nbsp;&nbsp;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;&nbsp;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div></div><div class=""><br class=""></div><div class="">As you can see what I basically wanted was to repeat the test some thousand times. I also like to use the Ruby language and one thing I love about it is that it has some really handy methods integrated to the language in situations like this which make the code very readable and therefore fun to use.</div><div class=""><br class=""></div><div class="">I’m an even bigger fan of Swift so I’d love to see such useful methods appear in Swift, too and this is the first I came across that I really missed. So I’m asking myself, what if I could write the same code above like this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">func</span>&nbsp;testPerfQualityInPercentWithoutQualityImprovements() {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: rgb(187, 44, 162);" class="">self</span><span style="" class="">.</span>measureBlock<span style="" class="">&nbsp;{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(187, 44, 162);" class="">let</span>&nbsp;expectedQuality =&nbsp;<span style="color: rgb(39, 42, 216);" class="">33.33</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(39, 42, 216);" class="">5_000.times {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(49, 89, 93);" class="">XCTAssertEqualWithAccuracy</span>(<span style="color: rgb(187, 44, 162);" class="">self</span>.<span style="color: rgb(79, 129, 135);" class="">crossword</span>.<span style="color: rgb(79, 129, 135);" class="">qualityInPercent</span>, expectedQuality, accuracy:&nbsp;<span style="color: rgb(39, 42, 216);" class="">0.1</span>) &nbsp;&nbsp;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;&nbsp;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">I think it could be added to the Swift standard library very easily (for example by using the .stride method like I used) without any side effects and has enough advantages to be part of Swift itself. What do you think?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">I wish you all the best,</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Cihat</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class="">P.S.: This is my very first mail in such a mailing list so I did everything correctly. ^.^</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k73-2FrQmsAlH8i0FWHjnC4LSFNp-2B8UyclW1aaDciyYN2jS0o683gPe4Bi6w0-2B3-2BPVDgU5QU-2BeN7HRusLR7zUeLafH439V6JkCfh8mrpx4OyPeiRc1GEMKdM-2FgpvsZYm9Lx0F0R606O8Hh-2BesvAhV3twsd1ny2sICNyyOQKxf5Y-2F0SoihEG2kYmUrHfVjHMHW9Cg-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>