<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Stack-Overflow on blog.iankulin.com</title><link>https://blog.iankulin.com/tags/stack-overflow/</link><description>Recent content in Stack-Overflow on blog.iankulin.com</description><generator>Hugo</generator><language>en-AU</language><lastBuildDate>Sat, 27 Aug 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.iankulin.com/tags/stack-overflow/index.xml" rel="self" type="application/rss+xml"/><item><title>Int.times()</title><link>https://blog.iankulin.com/int-times/</link><pubDate>Sat, 27 Aug 2022 00:00:00 +0000</pubDate><guid>https://blog.iankulin.com/int-times/</guid><description>&lt;p&gt;When writing &lt;a href="https://blog.iankulin.com/the-_-underscore/"&gt;yesterday&amp;rsquo;s post&lt;/a&gt; about iterating through a range or collection and using the underscore to throw away the item, I had in the back of my mind that there should be a more straightforward way of doing something a number of times.&lt;/p&gt;
&lt;p&gt;Just to re-iterate (lol), here&amp;rsquo;s the issue. If we want to print &amp;ldquo;Here&amp;rsquo;s the thing&amp;rdquo; three times, in Swift the simplest we can do is:&lt;/p&gt;
&lt;p&gt;for _ in 1&amp;hellip;3 {
print(&amp;ldquo;Here&amp;rsquo;s the thing&amp;rdquo;)
}&lt;/p&gt;
&lt;p&gt;I had the idea, that this should really be a method of the Int type. And in fact I could write it as an extension that took a closure. Then we could just do this:&lt;/p&gt;
&lt;p&gt;3.times {
print(&amp;ldquo;Here&amp;rsquo;s the thing&amp;rdquo;)
}&lt;/p&gt;
&lt;p&gt;That feels much more like the Swift way of doing things (although I probably picked it up during a brief flirtation with Ruby). Of course, I&amp;rsquo;d implement it with a while loop and a counter, so there&amp;rsquo;d still be the counter memory allocated, but only for an Int rather than the Array element type.&lt;/p&gt;
&lt;p&gt;With this system, the problem I was talking about yesterday:&lt;/p&gt;
&lt;p&gt;let thingStrings = [&amp;ldquo;Thing one&amp;rdquo;, &amp;ldquo;Thing two&amp;rdquo;, &amp;ldquo;Thing three&amp;rdquo;]&lt;/p&gt;
&lt;p&gt;for _ in thingStrings {
print(&amp;ldquo;Here&amp;rsquo;s the thing&amp;rdquo;)
}&lt;/p&gt;
&lt;p&gt;would become:&lt;/p&gt;
&lt;p&gt;let thingStrings = [&amp;ldquo;Thing one&amp;rdquo;, &amp;ldquo;Thing two&amp;rdquo;, &amp;ldquo;Thing three&amp;rdquo;]&lt;/p&gt;
&lt;p&gt;thingStrings.count.times {
print(&amp;ldquo;Here&amp;rsquo;s the thing&amp;rdquo;)
}&lt;/p&gt;
&lt;p&gt;Which is, I admit, not amazingly better, but better, especially if the compiler is allocating the memory and filling it with each array value in the first example (which I don&amp;rsquo;t know if it is, but am increasingly interested in finding out).&lt;/p&gt;
&lt;p&gt;Feeling pretty pleased with myself for inventing this new Int method, I had a extra thought that in fact, the Swift community may already of invented this and incorporated it in the language, so I should google it first. It turns out it&amp;rsquo;s not aprt of the official language, but neither (unsurprisingly) am I the first to think of it.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s a &lt;a href="https://stackoverflow.com/questions/30554013/what-is-the-shortest-way-to-run-same-code-n-times-in-swift"&gt;Stack Overflow answer&lt;/a&gt; to a question &amp;ldquo;What is the shortest way to run same code n times in Swift?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/30554013/what-is-the-shortest-way-to-run-same-code-n-times-in-swift"&gt;&lt;img src="https://blog.iankulin.com/images/screen-shot-2022-08-20-at-3.12.32-pm.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So shout out to &lt;a href="https://stackoverflow.com/users/4358829/matteo-piombo"&gt;Matteo Piombo&lt;/a&gt; for doing the work for my idea seven years before I had it! It&amp;rsquo;s still just for code clarity, but great use of extensions and closures.&lt;/p&gt;
&lt;p&gt;I still maintain that &lt;code&gt;for _ in&lt;/code&gt; is not great, and that &lt;code&gt;for each in&lt;/code&gt; where each was a synonym for the underscore would be the prettiest solution. A likely con of this proposal is that is would be a code breaking change for any code that has already uses &lt;code&gt;for each&lt;/code&gt; which could be quite common.&lt;/p&gt;</description></item></channel></rss>