Jekyll2021-09-16T08:25:40-07:00https://ethyreal.com/Ethyreal: Mobile DevelopmentWritings about software craftsmanship. These days I mostly write about Swift, Objective-C and mobile artitecture.George WebsterForward Application2019-02-15T09:17:00-08:002019-02-15T09:17:00-08:00https://ethyreal.com/composition/functional/swift/forward-application<p>Previously we built up <a href="/composition/functional/swift/forward-composition/">compositions of functions</a>. These created piplines to pass data through. The data though is always an after thought though. Semantically we describe an action or series of actions then we say what those actions are going to apply to.</p>
<p>Using the previous example we had a list of moves</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code> <span class="k">let</span> <span class="nv">moves</span> <span class="o">=</span> <span class="p">[</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">]</span>
</code></pre>
</div>
<p>We want to loudly declare a move, in this case the last one:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code> <span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">reverse</span>
<span class="o">>>></span> <span class="n">head</span>
<span class="o">>>></span> <span class="n">toUpperCase</span>
<span class="o">>>></span> <span class="n">exclaim</span>
<span class="nf">loudLastUpper</span><span class="p">(</span><span class="n">moves</span><span class="p">)</span> <span class="c1">//"UPPERCUT!"</span>
</code></pre>
</div>
<p>This is a familiar paradigm: <code class="highlighter-rouge">action</code> => <code class="highlighter-rouge">data</code>. Action or function is applied to some data. Programming 101 right? But what if we thought about our data first? Could we apply our moves to the <code class="highlighter-rouge">loudLastUpper</code> action? We could define an <code class="highlighter-rouge">apply</code> function:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">func</span> <span class="n">apply</span><span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="o">></span><span class="p">(</span><span class="n">_</span> <span class="nv">x</span><span class="p">:</span> <span class="kt">A</span><span class="p">,</span> <span class="n">to</span> <span class="nv">f</span><span class="p">:</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span> <span class="p">{</span>
<span class="k">return</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Now let’s <em>apply</em> it to the previous example:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="nf">apply</span><span class="p">(</span><span class="n">moves</span><span class="p">,</span> <span class="nv">to</span><span class="p">:</span> <span class="n">loudLastUpper</span><span class="p">)</span> <span class="c1">//"UPPERCUT!"</span>
</code></pre>
</div>
<p>This is different, but still understandable right? Apply these moves to the <code class="highlighter-rouge">loudLastUpper</code> action.</p>
<p>We could inline the moves list and it still reads well:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="nf">apply</span><span class="p">([</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">],</span>
<span class="nv">to</span><span class="p">:</span> <span class="n">loudLastUpper</span><span class="p">)</span>
</code></pre>
</div>
<p>This is almost what we are after. What if we infixed this apply function so we had somthing like <code class="highlighter-rouge">moves applyTo loudLastUpper</code>. This seems similar to what unix does with the pipe (<code class="highlighter-rouge">|</code>) operator:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="c1">// find all the `swift` files on a computer:</span>
<span class="n">locate</span> <span class="s">"*.swift"</span> <span class="o">|</span> <span class="n">grep</span> <span class="n">swift</span>
</code></pre>
</div>
<p>Unix pipes or <code class="highlighter-rouge">applies</code> the results of <code class="highlighter-rouge">locate</code> to <code class="highlighter-rouge">grep</code> two totally seperate commands are chained or composed together!</p>
<p>How about a similar oppertor for swift, we can borrow from other and use the <code class="highlighter-rouge">|></code> operator used in F# and Haskel:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">precedencegroup</span> <span class="kt">ForwardApplication</span> <span class="p">{</span>
<span class="nv">associativity</span><span class="p">:</span> <span class="k">left</span>
<span class="p">}</span>
<span class="k">infix</span> <span class="k">operator</span> <span class="o">|></span><span class="p">:</span> <span class="kt">ForwardApplication</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="o">|></span> <span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="o">></span><span class="p">(</span><span class="nv">a</span><span class="p">:</span> <span class="kt">A</span><span class="p">,</span> <span class="nv">f</span><span class="p">:</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span> <span class="p">{</span>
<span class="k">return</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Same goodness of <code class="highlighter-rouge">apply(to:)</code> but now are an use it <code class="highlighter-rouge">infix</code>!</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">moves</span> <span class="o">|></span> <span class="n">loudLastUpper</span> <span class="c1">//"UPPERCUT!"</span>
</code></pre>
</div>
<p>While this is a new operator to get used to, it is easy to undersand even thought knowing F# or Haskel, we are just piping data through!</p>
<p>We might not even need our <code class="highlighter-rouge">loudLastUpper</code> function any more, if it’s only used here it seems a waste. Could we just inline that:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">moves</span> <span class="o">|></span> <span class="n">reverse</span>
<span class="o">>>></span> <span class="n">head</span>
<span class="o">>>></span> <span class="n">toUpperCase</span>
<span class="o">>>></span> <span class="n">exclaim</span>
</code></pre>
</div>
<p>Out of the box this isn’t going to work. We have conflicting precedence groups. We need to define an order of operations (just like math!). Here we have both <code class="highlighter-rouge">ForwardComposition</code> and <code class="highlighter-rouge">ForwardApplication</code> with <code class="highlighter-rouge">left</code> associativity. We could use our friend parenthesis to group the composition first:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">moves</span> <span class="o">|></span> <span class="p">(</span><span class="n">reverse</span>
<span class="o">>>></span> <span class="n">head</span>
<span class="o">>>></span> <span class="n">toUpperCase</span>
<span class="o">>>></span> <span class="n">exclaim</span><span class="p">)</span>
</code></pre>
</div>
<p>This works, but adds noise and cognative overhead to our smoothly read composition. In some situations we may want that, but in others we might want a rule. If we are doing forward application and composition in the same place it might make sense to compose first as a rule. We can change our<code class="highlighter-rouge">ForwardComposition</code> precedence group to be placed higher than our <code class="highlighter-rouge">ForwardApplication</code>:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">precedencegroup</span> <span class="kt">ForwardComposition</span> <span class="p">{</span>
<span class="nv">associativity</span><span class="p">:</span> <span class="k">left</span>
<span class="nv">higherThan</span><span class="p">:</span> <span class="kt">ForwardApplication</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Now this works with no complaints:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">moves</span> <span class="o">|></span> <span class="n">reverse</span>
<span class="o">>>></span> <span class="n">head</span>
<span class="o">>>></span> <span class="n">toUpperCase</span>
<span class="o">>>></span> <span class="n">exclaim</span>
</code></pre>
</div>
<p>Now you might be wondinging how different this <code class="highlighter-rouge">apply</code> (<code class="highlighter-rouge">|></code>) function is from <code class="highlighter-rouge">compose</code> (<code class="highlighter-rouge">>>></code>)? On the surface they are very similar, apply puts a function’s argument in front where as compose calles one function with the result of another:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">func</span> <span class="o">|></span> <span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="o">></span><span class="p">(</span><span class="nv">a</span><span class="p">:</span> <span class="kt">A</span><span class="p">,</span> <span class="nv">f</span><span class="p">:</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span> <span class="p">{</span>
<span class="k">return</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Vs:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">func</span> <span class="o">>>></span> <span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="p">,</span> <span class="kt">C</span><span class="o">></span><span class="p">(</span><span class="n">_</span> <span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">,</span> <span class="n">_</span> <span class="nv">g</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">x</span> <span class="k">in</span> <span class="nf">g</span><span class="p">(</span><span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>In some cases (like our example) you could easily replace one with the other:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="p">[</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">]</span>
<span class="o">|></span> <span class="n">reverse</span>
<span class="o">|></span> <span class="n">head</span>
<span class="o">|></span> <span class="n">toUpperCase</span>
<span class="o">|></span> <span class="n">exclaim</span>
<span class="c1">// "UPPERCUT!"</span>
<span class="c1">// same result as:</span>
<span class="p">[</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">]</span>
<span class="o">|></span> <span class="n">reverse</span>
<span class="o">>>></span> <span class="n">head</span>
<span class="o">>>></span> <span class="n">toUpperCase</span>
<span class="o">>>></span> <span class="n">exclaim</span>
<span class="c1">// "UPPERCUT!"</span>
</code></pre>
</div>
<p>In this use case I personally find the piped version easier to read than the composed. For inline logic or one offs this is great, easy to visualize. This doesn’t mean compose doesn’t have its uses too. Being able to form new functions, save them, pass them around is an incredibly powerful thing.</p>
<blockquote>
<p>Working code can be found in the Chapter 5 page of the <a href="https://github.com/ethyreal/mostly-adequate-guide-swift">Mostly Adequate Swift Playground</a></p>
</blockquote>George WebsterPreviously we built up compositions of functions. These created piplines to pass data through. The data though is always an after thought though. Semantically we describe an action or series of actions then we say what those actions are going to apply to.Forward Composition2019-02-12T09:32:00-08:002019-02-12T09:32:00-08:00https://ethyreal.com/composition/functional/swift/forward-composition<p>In the previous post I “remixed” a portion of the <a href="/composition/functional/functional-husbandry/">composition chapter</a> in the <a href="https://github.com/MostlyAdequate/mostly-adequate-guide">Mostly Adequate Guide</a>. While reading that chapter and later translating the direction of composition kept confusing me. I wanted to convey the idea’s as close as possible.. keeping all the great humor like:</p>
<blockquote>
<p>Instead of inside to outside, we run right to left, which I suppose is a step in the left direction (boo!).</p>
</blockquote>
<p>And this does make sense, it reads right to left:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">exclaim</span> <span class="o"><<<</span> <span class="n">toUpperCase</span> <span class="o"><<<</span> <span class="n">head</span> <span class="o"><<<</span> <span class="n">reverse</span>
<span class="nf">loudLastUpper</span><span class="p">([</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">])</span> <span class="c1">//"UPPERCUT!"</span>
</code></pre>
</div>
<p>But it doesn’t feel like a huge readability gain from:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="p">{</span> <span class="p">(</span><span class="nv">x</span><span class="p">:[</span><span class="kt">String</span><span class="p">])</span> <span class="k">in</span> <span class="nf">exclaim</span><span class="p">(</span> <span class="nf">toUpperCase</span><span class="p">(</span> <span class="nf">head</span><span class="p">(</span> <span class="nf">reverse</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">)</span> <span class="p">)</span> <span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>Or the more common:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">func</span> <span class="nf">loudLastUpper</span><span class="p">(</span><span class="n">_</span> <span class="nv">x</span><span class="p">:[</span><span class="kt">String</span><span class="p">])</span> <span class="o">-></span> <span class="kt">String</span> <span class="p">{</span>
<span class="k">return</span> <span class="nf">exclaim</span><span class="p">(</span> <span class="nf">toUpperCase</span><span class="p">(</span> <span class="nf">head</span><span class="p">(</span> <span class="nf">reverse</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">)</span> <span class="p">)</span> <span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>It’s less parentheses and I do like the direction arrows pointing out the flow of data. We are still reading right to left which for me doesn’t feel natual in a language that is written left to right.</p>
<p>What I’ve been usually using is a <code class="highlighter-rouge">ForwardComposition</code> like this:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">precedencegroup</span> <span class="kt">ForwardComposition</span> <span class="p">{</span>
<span class="nv">associativity</span><span class="p">:</span> <span class="k">left</span>
<span class="p">}</span>
<span class="k">infix</span> <span class="k">operator</span> <span class="o">>>></span><span class="p">:</span> <span class="kt">ForwardComposition</span>
<span class="kd">func</span> <span class="o">>>></span> <span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="p">,</span> <span class="kt">C</span><span class="o">></span><span class="p">(</span><span class="n">_</span> <span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">,</span> <span class="n">_</span> <span class="nv">g</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">x</span> <span class="k">in</span> <span class="nf">g</span><span class="p">(</span><span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>I’ve heard this called <code class="highlighter-rouge">composeRight</code> or it could be a regular function called <code class="highlighter-rouge">forwardCompose</code> instead of an infix:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">func</span> <span class="n">forwardCompose</span><span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="p">,</span> <span class="kt">C</span><span class="o">></span><span class="p">(</span><span class="n">_</span> <span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">,</span> <span class="n">_</span> <span class="nv">g</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">x</span> <span class="k">in</span>
<span class="nf">g</span><span class="p">(</span><span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Either way we can chain or pipe the logic from the left to the right, the same direction as the language we are writing in!</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="c1">// same result as before!</span>
<span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">reverse</span> <span class="o">>>></span> <span class="n">head</span> <span class="o">>>></span> <span class="n">toUpperCase</span> <span class="o">>>></span> <span class="n">exclaim</span>
<span class="nf">loudLastUpper</span><span class="p">([</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">])</span> <span class="c1">//"UPPERCUT!"</span>
</code></pre>
</div>
<p>Lastly we can break them up with line breaks to increase readability further… especially in cases of longer method names:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">reverse</span>
<span class="o">>>></span> <span class="n">head</span>
<span class="o">>>></span> <span class="n">toUpperCase</span>
<span class="o">>>></span> <span class="n">exclaim</span>
</code></pre>
</div>
<p>All the goodness from <a href="/composition/functional/functional-husbandry/">functional husbandry</a> still applies, but now we are moving in the <code class="highlighter-rouge">right</code> direction (boo!).</p>
<blockquote>
<p>Working code can be found in the Chapter 5 page of the <a href="https://github.com/ethyreal/mostly-adequate-guide-swift">Mostly Adequate Swift Playground</a></p>
</blockquote>George WebsterIn the previous post I “remixed” a portion of the composition chapter in the Mostly Adequate Guide. While reading that chapter and later translating the direction of composition kept confusing me. I wanted to convey the idea’s as close as possible.. keeping all the great humor like: Instead of inside to outside, we run right to left, which I suppose is a step in the left direction (boo!).Functional Husbandry2019-02-09T18:28:00-08:002019-02-09T18:28:00-08:00https://ethyreal.com/composition/functional/swift/functional-husbandry<blockquote>
<p>This is part of a remix of the <a href="https://github.com/MostlyAdequate/mostly-adequate-guide">Mostly Adequate Guide</a> (Chapter 5). I have translated examples into swift and updated text where appropriate.</p>
</blockquote>
<p>Here’s <em>compose</em> :</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">precedencegroup</span> <span class="kt">BackwardsComposition</span> <span class="p">{</span>
<span class="nv">associativity</span><span class="p">:</span> <span class="k">left</span>
<span class="p">}</span>
<span class="k">infix</span> <span class="k">operator</span> <span class="o"><<<</span><span class="p">:</span> <span class="kt">BackwardsComposition</span>
<span class="kd">func</span> <span class="o"><<<</span> <span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="p">,</span> <span class="kt">C</span><span class="o">></span><span class="p">(</span><span class="n">_</span> <span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span><span class="p">,</span> <span class="n">_</span> <span class="nv">g</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">x</span> <span class="k">in</span> <span class="nf">f</span><span class="p">(</span><span class="nf">g</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>… Don’t be scared! This is the level-9000-super-Saiyan-form of <em>compose</em>. For the sake of reasoning, let’s drop the infix implementation and consider a simpler form that can compose two functions together. Once you get your head around that, you can push the abstraction further and consider it simply works for any number of functions (we could even prove that)!</p>
<p>Here’s a more friendly <em>compose</em> for you my dear readers:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">func</span> <span class="n">compose</span><span class="o"><</span><span class="kt">A</span><span class="p">,</span> <span class="kt">B</span><span class="p">,</span> <span class="kt">C</span><span class="o">></span><span class="p">(</span><span class="n">_</span> <span class="nv">f</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span><span class="p">,</span> <span class="n">_</span> <span class="nv">g</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">B</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="kt">A</span><span class="p">)</span> <span class="o">-></span> <span class="kt">C</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">{</span> <span class="n">x</span> <span class="k">in</span>
<span class="nf">f</span><span class="p">(</span><span class="nf">g</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p><code class="highlighter-rouge">f</code> and <code class="highlighter-rouge">g</code> are functions and <code class="highlighter-rouge">x</code> is the value being “piped” through them. The generics <code class="highlighter-rouge">A</code>, <code class="highlighter-rouge">B</code> and <code class="highlighter-rouge">C</code> allow both <code class="highlighter-rouge">f</code> and <code class="highlighter-rouge">g</code> to transform any type into any other preserving type safety.</p>
<p>Composition feels like function husbandry. You, breeder of functions, select two with traits you’d like to combine and mash them together to spawn a brand new one. Usage is as follows:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">toUpperCase</span> <span class="o">=</span> <span class="p">{</span> <span class="p">(</span><span class="nv">x</span><span class="p">:</span><span class="kt">String</span><span class="p">)</span> <span class="k">in</span> <span class="n">x</span><span class="o">.</span><span class="nf">uppercased</span><span class="p">()</span> <span class="p">}</span>
<span class="k">let</span> <span class="nv">exclaim</span> <span class="o">=</span> <span class="p">{</span> <span class="p">(</span><span class="nv">x</span><span class="p">:</span><span class="kt">String</span><span class="p">)</span> <span class="k">in</span> <span class="s">"</span><span class="se">\(</span><span class="n">x</span><span class="se">)</span><span class="s">!"</span> <span class="p">}</span>
<span class="k">let</span> <span class="nv">shout</span> <span class="o">=</span> <span class="nf">compose</span><span class="p">(</span><span class="n">exclaim</span><span class="p">,</span> <span class="n">toUpperCase</span><span class="p">)</span>
<span class="nf">shout</span><span class="p">(</span><span class="s">"send in the clowns"</span><span class="p">)</span> <span class="c1">// "SEND IN THE CLOWNS!"</span>
</code></pre>
</div>
<p>The composition of two functions returns a new function. This makes perfect sense: composing two units of some type (in this case function) should yield a new unit of that very type. You don’t plug two legos together and get a lincoln log. There is a theory here, some underlying law that we will discover in due time.</p>
<p>In our definition of <code class="highlighter-rouge">compose</code>, the <code class="highlighter-rouge">g</code> will run before the <code class="highlighter-rouge">f</code>, creating a right to left flow of data. This is much more readable than nesting a bunch of function calls. Without compose, the above would read:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">shout</span> <span class="o">=</span> <span class="p">{</span> <span class="p">(</span><span class="nv">x</span><span class="p">:</span><span class="kt">String</span><span class="p">)</span> <span class="k">in</span> <span class="nf">exclaim</span><span class="p">(</span><span class="nf">toUpperCase</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="p">}</span>
</code></pre>
</div>
<p>Instead of inside to outside, we run right to left, which I suppose is a step in the left direction (boo!). Let’s look at an example where sequence matters:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">func</span> <span class="nf">head</span><span class="p">(</span><span class="n">_</span> <span class="nv">x</span><span class="p">:[</span><span class="kt">String</span><span class="p">])</span> <span class="o">-></span> <span class="kt">String</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="n">first</span> <span class="p">??</span> <span class="s">""</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">reverse</span><span class="p">(</span><span class="n">_</span> <span class="nv">x</span><span class="p">:[</span><span class="kt">String</span><span class="p">])</span> <span class="o">-></span> <span class="p">[</span><span class="kt">String</span><span class="p">]</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="nf">reversed</span><span class="p">()</span>
<span class="p">}</span>
<span class="k">let</span> <span class="nv">last</span> <span class="o">=</span> <span class="nf">compose</span><span class="p">(</span><span class="n">head</span><span class="p">,</span> <span class="n">reverse</span><span class="p">)</span>
<span class="nf">last</span><span class="p">([</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">])</span> <span class="c1">// "uppercut"</span>
</code></pre>
</div>
<p><code class="highlighter-rouge">reverse</code> will turn the list around while <code class="highlighter-rouge">head</code> grabs the initial item. This results in an effective, albeit inefficient, <code class="highlighter-rouge">last</code> function. The sequence of functions in the composition should be apparent here. We could define a left to right version, however, we mirror the mathematical version much more closely as it stands. That’s right, composition is straight
from the math books. In fact, perhaps it’s time to look at a property that holds for any composition.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code> <span class="c1">// associativity</span>
<span class="nf">compose</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nf">compose</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">h</span><span class="p">))</span> <span class="o">===</span> <span class="nf">compose</span><span class="p">(</span><span class="nf">compose</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">g</span><span class="p">),</span> <span class="n">h</span><span class="p">)</span>
</code></pre>
</div>
<p>Composition is associative, meaning it doesn’t matter how you group two of them. So, should we choose to uppercase the string, we can write:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="nf">compose</span><span class="p">(</span><span class="n">toUpperCase</span><span class="p">,</span> <span class="nf">compose</span><span class="p">(</span><span class="n">head</span><span class="p">,</span> <span class="n">reverse</span><span class="p">))</span>
<span class="c1">// or</span>
<span class="nf">compose</span><span class="p">(</span><span class="nf">compose</span><span class="p">(</span><span class="n">toUpperCase</span><span class="p">,</span> <span class="n">head</span><span class="p">),</span> <span class="n">reverse</span><span class="p">)</span>
</code></pre>
</div>
<p>Since it doesn’t matter how we group our calls to compose, the result will be the same. That allows us to use our infix verson of our compose method:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="c1">// previously we'd have to write two composes, but since it's associative,</span>
<span class="c1">// we can give compose as many fn's as we like and let it decide how to group them.</span>
<span class="k">let</span> <span class="nv">arg</span> <span class="o">=</span> <span class="p">[</span><span class="s">"jumpkick"</span><span class="p">,</span> <span class="s">"roundhouse"</span><span class="p">,</span> <span class="s">"uppercut"</span><span class="p">]</span>
<span class="k">let</span> <span class="nv">lastUpper</span> <span class="o">=</span> <span class="n">toUpperCase</span> <span class="o"><<<</span> <span class="n">head</span> <span class="o"><<<</span> <span class="n">reverse</span>
<span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">exclaim</span> <span class="o"><<<</span> <span class="n">toUpperCase</span> <span class="o"><<<</span> <span class="n">head</span> <span class="o"><<<</span> <span class="n">reverse</span>
<span class="nf">lastUpper</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="c1">// 'UPPERCUT'</span>
<span class="nf">loudLastUpper</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="c1">// 'UPPERCUT!</span>
</code></pre>
</div>
<p>Applying the associative property gives us this flexibility and peace of mind that the result will be equivalent.</p>
<p>One pleasant benefit of associativity is that any group of functions can be extracted and bundled together in their very own composition. Let’s play with refactoring our previous example:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">exclaim</span> <span class="o"><<<</span> <span class="n">toUpperCase</span> <span class="o"><<<</span> <span class="n">head</span> <span class="o"><<<</span> <span class="n">reverse</span>
<span class="c1">// -- or ---------------------------------------------------------------</span>
<span class="k">let</span> <span class="nv">last</span> <span class="o">=</span> <span class="n">head</span> <span class="o"><<<</span> <span class="n">reverse</span>
<span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">exclaim</span> <span class="o"><<<</span> <span class="n">toUpperCase</span> <span class="o"><<<</span> <span class="n">last</span>
<span class="c1">// -- or ---------------------------------------------------------------</span>
<span class="k">let</span> <span class="nv">last</span> <span class="o">=</span> <span class="n">head</span> <span class="o"><<<</span> <span class="n">reverse</span>
<span class="k">let</span> <span class="nv">angry</span> <span class="o">=</span> <span class="n">exclaim</span> <span class="o"><<<</span> <span class="n">toUpperCase</span>
<span class="k">let</span> <span class="nv">loudLastUpper</span> <span class="o">=</span> <span class="n">angry</span> <span class="o"><<<</span> <span class="n">last</span>
</code></pre>
</div>
<p>There’s no right or wrong answers - we’re just plugging our legos together in whatever way we please. Usually it’s best to group things in a reusable way like <code class="highlighter-rouge">last</code> and <code class="highlighter-rouge">angry</code>. If familiar with Fowler’s “<a href="https://martinfowler.com/books/refactoring.html">Refactoring</a>”, one might recognize this process as “<a href="https://refactoring.com/catalog/extractFunction.html">extract function</a>” …except without all the object state to worry about.</p>
<blockquote>
<p>Working code can be found in the Chapter 5 page of the <a href="https://github.com/ethyreal/mostly-adequate-guide-swift">Mostly Adequate Swift Playground</a></p>
</blockquote>George WebsterThis is part of a remix of the Mostly Adequate Guide (Chapter 5). I have translated examples into swift and updated text where appropriate.Setup Gitlab Runner for an Xcode Project2018-08-31T08:24:00-07:002018-08-31T08:24:00-07:00https://ethyreal.com/continious-integration/gitlab/mac/xcode/gitlab-runner-setup-for-xcode-projects<p>Setting up a gitab runner on a mac is fairly straightforward and the <a href="https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/install/osx.md">official documentation</a> is actually quite good.
It does involve bouncing around a few places and looking someome things up.<br />
After setting up the nth runner myself I wanted to list all the steps I took in one place, and if I’m being honest I really should write a script automate this process if I keep doing it.</p>
<h3 id="download">Download</h3>
<p>Currently there is no easy one liner like <code class="highlighter-rouge">brew install gitab-runner</code> we can run to get this up and running.
But it’s easy to do what homebrew might do for us, we can download the latest gitab-runner binary to <code class="highlighter-rouge">/usr/local/bin</code> using curl:</p>
<div class="language-sh highlighter-rouge"><pre class="highlight"><code>sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
</code></pre>
</div>
<p>Then we need to allow it to run:</p>
<div class="language-sh highlighter-rouge"><pre class="highlight"><code>sudo chmod +x /usr/local/bin/gitlab-runner
</code></pre>
</div>
<h3 id="register">Register</h3>
<p>Now we can register our runner with our gitlab server.<br />
Before we do that we’ll need the url of our server a token which can be found
in our gitlab project main menu |> Settings |> CI/CD |> Runner Settings. <br />
There is a box labelled “Setup a specific Runner manually” which has all the goods.</p>
<p>Okay so let’s register:</p>
<div class="language-sh highlighter-rouge"><pre class="highlight"><code>gitlab-runner register
</code></pre>
</div>
<p>The first two items we just found in our runner settings, coordinator URL and gitlab runner CI token. <br />
Then give it a meaning give it a meaningful name and some tags.</p>
<p>Little bit about tags…tags are how gitlab will choose which runner to run for each of our CI jobs.<br />
Each job will have tags and each runner will have tags.<br />
When CI is triggered it runs down the list of jobs, if the job is able it run, it looks for all runners that have tags that match the tags for the job, which it finds one it starts the job on that runner.
So if we have a job call <code class="highlighter-rouge">unit_tests</code> and it has a tag <code class="highlighter-rouge">ios_ci</code> if we want this new runner to run that job should add the tag <code class="highlighter-rouge">ios_ci</code></p>
<p>Last step is choosing how the runner will execute. Currently the only way I know of to run CI is through executing <code class="highlighter-rouge">xcode-build</code> via the shell so we’ll choose <code class="highlighter-rouge">shell</code> from the list of options.</p>
<p>Now that its all registered we can start it up and it’s good to go:</p>
<div class="language-sh highlighter-rouge"><pre class="highlight"><code>gitlab-runner start
</code></pre>
</div>
<p>We can verify this by going back to our CI settings in our gitlab project.<br />
If we expand runners there we should see our new runner listed with a green light icon next to it.</p>George WebsterSetting up a gitab runner on a mac is fairly straightforward and the official documentation is actually quite good. It does involve bouncing around a few places and looking someome things up. After setting up the nth runner myself I wanted to list all the steps I took in one place, and if I’m being honest I really should write a script automate this process if I keep doing it.Development Tools2018-08-14T09:01:00-07:002018-08-14T09:01:00-07:00https://ethyreal.com/mac/software/development/tools/development-tools<p>I recently had to update my machine and realized I never really had a list of tools that I use. I would reach for a tool and when it wasn’t there go and install it. There are likely many more robust lists of ios/mac software tools out there but these are the ones I find myself using at least once a month if not daily.</p>
<h3 id="editors">Editors</h3>
<ul>
<li><a href="https://developer.apple.com/xcode/">Xcode</a> gets better and better each year. I’ve flirted with <a href="https://www.jetbrains.com/objc/">AppCode</a> a few times but always go back to Xcode. Since the rewrite of the core engine in swift the editor has really grown into a proper IDE with most of the conveniences of a Jetbrains quality product.</li>
<li><a href="https://code.visualstudio.com">VSCode</a> strikes that balance beteen text editor and IDE that I’m growing to appreciate more and more each day. For most of my non apple platform writing I tend to lean on VSCode.</li>
</ul>
<h3 id="source-control">Source Control</h3>
<ul>
<li><a href="https://www.git-tower.com">Tower</a> git client that has saved me countless hours.</li>
<li><a href="https://www.kaleidoscopeapp.com">Kaleidoscope</a> beautiful diff and merge tool.</li>
</ul>
<h3 id="api-toolsproxies">API Tools/Proxies</h3>
<ul>
<li><a href="https://www.charlesproxy.com">Charles</a> HTTP proxy, invaluable for debugging networking in apps. The addition of an <a href="https://itunes.apple.com/us/app/charles-proxy/id1134218562?mt=8">iOS app</a> makes on device debuging that much easier.</li>
<li><a href="https://www.getpostman.com">Postman</a> is a great for exploring, document and debuging API calls</li>
</ul>
<h3 id="utilities">Utilities</h3>
<ul>
<li><a href="https://simpholders.com">SimPholders</a> for easy inspecting of the installed app bundles on iOS simulators. Integrates nicely when used with <a href="osxfuse.github.io">FUSE</a>.</li>
<li><a href="https://acqualia.com/soulver/">Soulver</a> easy calculations, faster than a spreadsheet, more powerful than a calculator.</li>
<li><a href="https://bjango.com/mac/istatmenus/">iStat Menus</a> monitors and records your mac’s performance. Between Xcode and the legion of electron apps invading the mac its hard to know what is slowing your machine down.</li>
</ul>
<h3 id="fonts">Fonts</h3>
<ul>
<li><a href="https://github.com/tonsky/FiraCode">Fira Code</a> monospaced font with programming ligatures</li>
</ul>
<h3 id="presentations">Presentations</h3>
<ul>
<li><a href="https://www.deckset.com">Deckset</a> write presentations in markdown</li>
<li><a href="https://www.omnigroup.com/omnigraffle">OmniGraffle</a> crafting graphics, flow charts and wireframes</li>
</ul>George WebsterI recently had to update my machine and realized I never really had a list of tools that I use. I would reach for a tool and when it wasn’t there go and install it. There are likely many more robust lists of ios/mac software tools out there but these are the ones I find myself using at least once a month if not daily.Asynchronous Error Handling with Result2017-03-16T02:30:00-07:002017-03-16T02:30:00-07:00https://ethyreal.com/swift/error/functional/result/asynchronous-error-handling-with-result<p>Handling errors in swift is fairly straighforward. Let say we have an artist:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">struct</span> <span class="kt">Artist</span> <span class="p">{</span>
<span class="k">let</span> <span class="nv">name</span><span class="p">:</span><span class="kt">String</span>
<span class="k">let</span> <span class="nv">age</span><span class="p">:</span><span class="kt">Int</span>
<span class="p">}</span>
</code></pre>
</div>
<p>That artist needs to be persisted, so we add a service:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">class</span> <span class="kt">ArtistService</span> <span class="p">{</span>
<span class="kd">func</span> <span class="nf">save</span><span class="p">(</span><span class="n">_</span> <span class="nv">artist</span><span class="p">:</span><span class="kt">Artist</span><span class="p">)</span> <span class="k">throws</span> <span class="p">{</span>
<span class="c1">// persist somehow... </span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Because things can go wrong and need to be able handle those cases so we add an error:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">enum</span> <span class="kt">ArtistError</span><span class="p">:</span> <span class="kt">Error</span> <span class="p">{</span>
<span class="k">case</span> <span class="n">parsing</span>
<span class="k">case</span> <span class="n">network</span>
<span class="k">case</span> <span class="n">persistance</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Now we can create and save an artist all the while feeling comfortable in that we have handled our edge cases:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">service</span> <span class="o">=</span> <span class="kt">ArtistService</span><span class="p">()</span>
<span class="k">let</span> <span class="nv">bowie</span> <span class="o">=</span> <span class="kt">Artist</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"David Bowie"</span><span class="p">,</span> <span class="nv">age</span><span class="p">:</span> <span class="mi">69</span><span class="p">)</span>
<span class="k">do</span> <span class="p">{</span>
<span class="k">try</span> <span class="n">service</span><span class="o">.</span><span class="nf">save</span><span class="p">(</span><span class="n">bowie</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="kt">ArtistError</span><span class="o">.</span><span class="n">parsingError</span> <span class="p">{</span>
<span class="c1">// handle failed to parse</span>
<span class="p">}</span>
</code></pre>
</div>
<p>What happens though when we need to load artists? Most likely that will take time, either querying a local database or fetching from a network. Eitherway we would normally wind of with an API using a callback that provides the requested resource and an error:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">class</span> <span class="kt">ArtistService</span> <span class="p">{</span>
<span class="kd">func</span> <span class="nf">artists</span><span class="p">(</span><span class="nv">completion</span><span class="p">:</span> <span class="p">([</span><span class="kt">Artist</span><span class="p">]?,</span> <span class="kt">Error</span><span class="p">?)</span> <span class="o">-></span> <span class="kt">Void</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// fetch inspirational people </span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Safely handling this always feels awkward though because even since we are using two optional values to represent this we actually have four possible states:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">service</span><span class="o">.</span><span class="n">artists</span> <span class="p">{</span> <span class="p">(</span><span class="n">artists</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
<span class="k">if</span> <span class="n">error</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">,</span> <span class="n">artists</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="c1">// error and no artits cool, handle error</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="n">error</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">,</span> <span class="n">artists</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="c1">// error but we got artists...strange.. but lets handle error</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="n">artists</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">,</span> <span class="n">error</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="c1">// we got artists and no error yay!!</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="n">artists</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">,</span> <span class="n">error</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="c1">// no artists and no error...um..so now what?</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>This seems too complicated. What we really want to represent is just two states here: success and failure. Luckily swift provides enums to the reseue! Enums are basically swifts implemetation of a Sum Type or <a href="https://en.wikipedia.org/wiki/Tagged_union">Tagged Union</a>. We start simple and build one just for our artists request:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">enum</span> <span class="kt">ArtistsResult</span> <span class="p">{</span>
<span class="k">case</span> <span class="nf">failure</span><span class="p">(</span><span class="kt">Error</span><span class="p">)</span>
<span class="k">case</span> <span class="nf">success</span><span class="p">([</span><span class="kt">Artist</span><span class="p">])</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Update our service to use our fancy result:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">class</span> <span class="kt">ArtistService</span> <span class="p">{</span>
<span class="kd">func</span> <span class="nf">artists</span><span class="p">(</span><span class="nv">completion</span><span class="p">:</span> <span class="p">(</span><span class="kt">ArtistsResult</span><span class="p">)</span> <span class="o">-></span> <span class="kt">Void</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// fetch inspirational people </span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Now using it we can cleanly just deal with two possible outcomes:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">service</span><span class="o">.</span><span class="n">artists</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="k">switch</span> <span class="n">result</span> <span class="p">{</span>
<span class="k">case</span> <span class="o">.</span><span class="nf">failure</span><span class="p">(</span> <span class="k">let</span> <span class="nv">error</span> <span class="p">):</span>
<span class="c1">// oh no, total fail!! handle the error</span>
<span class="k">case</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span> <span class="k">let</span> <span class="nv">artists</span> <span class="p">):</span>
<span class="c1">// nice!</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>I feel good about this! no edge cases, cleanly handle two possible options. It doesn’t seem very resuable though, it only handles an array of artists. We could add another result for a single artist right?</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">enum</span> <span class="kt">ArtistResult</span> <span class="p">{</span>
<span class="k">case</span> <span class="nf">failure</span><span class="p">(</span><span class="kt">Error</span><span class="p">)</span>
<span class="k">case</span> <span class="nf">success</span><span class="p">(</span><span class="kt">Artist</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>But then we are defining an enum each time we want to handle another asynchronous operation that seems just as bad as handling all the cases of two seperatate types in the if/else soup. If the type could be generic over all possible successes than we’d be in business:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">enum</span> <span class="kt">Result</span><span class="o"><</span><span class="kt">T</span><span class="o">></span> <span class="p">{</span>
<span class="k">case</span> <span class="nf">failure</span><span class="p">(</span><span class="kt">Error</span><span class="p">)</span>
<span class="k">case</span> <span class="nf">success</span><span class="p">(</span><span class="kt">T</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Our signature and call site look similar:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">class</span> <span class="kt">ArtistService</span> <span class="p">{</span>
<span class="kd">func</span> <span class="nf">artists</span><span class="p">(</span><span class="nv">completion</span><span class="p">:</span> <span class="p">(</span><span class="kt">Result</span><span class="o"><</span><span class="p">[</span><span class="kt">Artist</span><span class="p">]</span><span class="o">></span><span class="p">)</span> <span class="o">-></span> <span class="kt">Void</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// fetch inspirational people </span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">service</span><span class="o">.</span><span class="n">artists</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="k">switch</span> <span class="n">result</span> <span class="p">{</span>
<span class="k">case</span> <span class="o">.</span><span class="nf">failure</span><span class="p">(</span> <span class="k">let</span> <span class="nv">error</span> <span class="p">):</span>
<span class="c1">// oh no, total fail!! handle the error</span>
<span class="k">case</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span> <span class="k">let</span> <span class="nv">artists</span> <span class="p">):</span>
<span class="c1">// nice!</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Now we have a <code class="highlighter-rouge">swift Result<T></code> type we can reuse for all our asynchronous error handling! It explicitly defines two possile options and each option can be clearly handled using a simple case statement.</p>
<p>Many others have <a href="https://www.cocoawithlove.com/blog/2016/08/21/result-types-part-one.html">written</a> and <a href="http://2014.funswiftconf.com/speakers/john.html">spoken</a> in great detail about using <code class="highlighter-rouge">Result</code> for fun an profit</p>George WebsterHandling errors in swift is fairly straighforward. Let say we have an artist:Say More With Less In Swift2017-03-01T09:43:00-08:002017-03-01T09:43:00-08:00https://ethyreal.com/swift/style/say-more-with-less-in-swift<p>For many years of iOS development each team I have worked with has adobted a style guide usually based off of Apple’s <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html">Cocoa Coding Guidelines</a>. Every team has their own style but mostly they all have been inspired by Cocoa and hence are very descriptive. Over time I grew to love this, it was clear and self documenting if at the cost of being especially verbose. More than anything I always wanted my code to not look and feel any different from Apple’s code. This changed last summer with the introduciton of swift 3 and Swift’s <a href="https://swift.org/documentation/api-design-guidelines/">API Design Guidelines</a>.</p>
<p>Key takeaways for me are:</p>
<ul>
<li>Favor clarity at the call site</li>
<li>Say more with less</li>
</ul>
<p>The second point for me is the trickiest. Comming from Cocoa API’s brevity and clarity seem in opposition. However by removing needless words, adding call site context and learning Swift’s new vocabulary we can actually “Say more with less”.</p>
<p>As an example let say as a user I want to see a list of great musicians that died last year.</p>
<p>First we might start with a simple model:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">struct</span> <span class="kt">Person</span> <span class="p">{</span>
<span class="k">let</span> <span class="nv">name</span><span class="p">:</span><span class="kt">String</span>
<span class="k">let</span> <span class="nv">age</span><span class="p">:</span><span class="kt">Int</span>
<span class="p">}</span>
</code></pre>
</div>
<p>We might need to load this model from a restful service that provides a JSON array. After decoding it might be represented like this:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">remoteData</span><span class="p">:[[</span><span class="kt">String</span><span class="p">:</span> <span class="kt">Any</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"David Bowie"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">69</span><span class="p">],</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"Prince"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">57</span><span class="p">],</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"Leonard Cohen"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">82</span><span class="p">],</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"George Michael"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">52</span><span class="p">]</span>
<span class="p">]</span>
</code></pre>
</div>
<p>This is straighforward enough, an array of dictionaries that have string keys and any value. Mentally though reading the desclaration we have to compute “remoteData” equals and array of “string:any” dictionaries. If all our JSON will look like this could this be made more clear with a <code class="highlighter-rouge">typealias</code> for our JSON:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">typealias</span> <span class="kt">JSONDictionary</span> <span class="o">=</span> <span class="p">[</span><span class="kt">String</span><span class="p">:</span> <span class="kt">Any</span><span class="p">]</span>
</code></pre>
</div>
<p>We could additionaly rename the variable to something that is more specific in descibeing its contents:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">jsonArray</span><span class="p">:[</span><span class="kt">JSONDictionary</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"David Bowie"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">69</span><span class="p">],</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"Prince"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">57</span><span class="p">],</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"Leonard Cohen"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">82</span><span class="p">],</span>
<span class="p">[</span><span class="s">"name"</span> <span class="p">:</span> <span class="s">"George Michael"</span><span class="p">,</span> <span class="s">"age"</span> <span class="p">:</span> <span class="mi">52</span><span class="p">]</span>
<span class="p">]</span>
</code></pre>
</div>
<p>We could extend our <code class="highlighter-rouge">Person</code> model to understand how to construct ifself from a <code class="highlighter-rouge">JSONDictionary</code>:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">extension</span> <span class="kt">Person</span> <span class="p">{</span>
<span class="nf">init</span><span class="p">?(</span><span class="nv">json</span><span class="p">:</span><span class="kt">JSONDictionary</span><span class="p">)</span> <span class="p">{</span>
<span class="k">guard</span> <span class="k">let</span> <span class="nv">name</span> <span class="o">=</span> <span class="n">json</span><span class="p">[</span><span class="s">"name"</span><span class="p">]</span> <span class="k">as?</span> <span class="kt">String</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">nil</span> <span class="p">}</span>
<span class="k">guard</span> <span class="k">let</span> <span class="nv">age</span> <span class="o">=</span> <span class="n">json</span><span class="p">[</span><span class="s">"age"</span><span class="p">]</span> <span class="k">as?</span> <span class="kt">Int</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">nil</span> <span class="p">}</span>
<span class="k">self</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span><span class="n">name</span><span class="p">,</span> <span class="nv">age</span><span class="p">:</span><span class="n">age</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>We do this inside an extension so we don’t loose our default struct initializer.</p>
<p>Now we use our trusty for loop to build and array of persons:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">people</span> <span class="o">=</span> <span class="p">[</span><span class="kt">Person</span><span class="p">]()</span>
<span class="k">for</span> <span class="n">json</span> <span class="k">in</span> <span class="n">jsonArray</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">person</span> <span class="o">=</span> <span class="kt">Person</span><span class="p">(</span><span class="nv">json</span><span class="p">:</span> <span class="n">json</span><span class="p">)</span> <span class="p">{</span>
<span class="n">people</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">person</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>This is familiar and clear. It’s clear we are looping through and array of JSON, and building an array of <code class="highlighter-rouge">Person</code>’s. We’ve all done this many many times.</p>
<p>This is infact done so much that there are actually methods to do exacly that. Transforming one collection into another. Enter our friends <code class="highlighter-rouge">map()</code> and <code class="highlighter-rouge">flatMap</code>. Each of these takes a transform closure to apply to each element of the collection being mapped over. So we can write the same logic:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">people</span><span class="p">:[</span><span class="kt">Person</span><span class="p">]</span> <span class="o">=</span> <span class="n">jsonArray</span><span class="o">.</span><span class="nf">flatMap</span><span class="p">({</span> <span class="p">(</span><span class="n">json</span><span class="p">)</span> <span class="o">-></span> <span class="kt">Person</span><span class="p">?</span> <span class="k">in</span>
<span class="k">return</span> <span class="kt">Person</span><span class="p">(</span><span class="nv">json</span><span class="p">:</span> <span class="n">json</span><span class="p">)</span>
<span class="p">})</span>
</code></pre>
</div>
<p>This still seems clear and DRY which is good. There is a lot of syntax in this that while clear could likely be assumed based on context. We could rewrite this using swift’s trailing closure syntax, and omit the return type:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">people</span><span class="p">:[</span><span class="kt">Person</span><span class="p">]</span> <span class="o">=</span> <span class="n">jsonArray</span><span class="o">.</span><span class="n">flatMap</span> <span class="p">{</span> <span class="p">(</span><span class="n">json</span><span class="p">)</span> <span class="k">in</span>
<span class="k">return</span> <span class="kt">Person</span><span class="p">(</span><span class="nv">json</span><span class="p">:</span> <span class="n">json</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Still clear right? We haven’t lost any meaning and we’ve reduced the ammount of symbols we need to process in our heads.</p>
<p>We see at the call site that we are mapping an array of JSON, then passing that JSON to an arguement called “json” so adding the <code class="highlighter-rouge">json</code> label in the closure seems like its duplicating words. In closures we do have the agrument list available with <code class="highlighter-rouge">$0, $1, etc</code>. On their own they seem opaque and terse but look at the call site now:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">people</span><span class="p">:[</span><span class="kt">Person</span><span class="p">]</span> <span class="o">=</span> <span class="n">jsonArray</span><span class="o">.</span><span class="n">flatMap</span> <span class="p">{</span>
<span class="k">return</span> <span class="kt">Person</span><span class="p">(</span><span class="nv">json</span><span class="p">:</span> <span class="nv">$0</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>If there is only one line in an expresion that returns a value then it can be assumed to return, could we remove the return? While we are at it, does this read well on one line?</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">people</span><span class="p">:[</span><span class="kt">Person</span><span class="p">]</span> <span class="o">=</span> <span class="n">jsonArray</span><span class="o">.</span><span class="n">flatMap</span> <span class="p">{</span> <span class="kt">Person</span><span class="p">(</span><span class="nv">json</span><span class="p">:</span> <span class="nv">$0</span><span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>We could even do a step further and drop the type from the instance variable:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">people</span> <span class="o">=</span> <span class="n">jsonArray</span><span class="o">.</span><span class="n">flatMap</span> <span class="p">{</span> <span class="kt">Person</span><span class="p">(</span><span class="nv">json</span><span class="p">:</span> <span class="nv">$0</span><span class="p">)</span> <span class="p">}</span>
</code></pre>
</div>
<p>Since we are flapMapping over a JSON array and returning an array of persons our arrays generic flatMap effectively looks like this:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">func</span> <span class="nf">flatMap</span><span class="p">(</span><span class="n">_</span> <span class="nv">transform</span><span class="p">:</span> <span class="p">(</span><span class="kt">JSONDictionary</span><span class="p">)</span> <span class="o">-></span> <span class="kt">Person</span><span class="p">?)</span> <span class="o">-></span> <span class="p">[</span><span class="kt">Person</span><span class="p">]</span> <span class="p">{}</span>
</code></pre>
</div>
<p>Notice the transform closure has the same signature as the Person initializer. They both take an <code class="highlighter-rouge">JSONDictionary</code> and return an optional <code class="highlighter-rouge">Person</code>:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kd">extension</span> <span class="kt">Person</span> <span class="p">{</span>
<span class="nf">init</span><span class="p">?(</span><span class="nv">json</span><span class="p">:</span><span class="kt">JSONDictionary</span><span class="p">)</span> <span class="p">{}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Because those signatures have the same argument types and return value regardless of argument labels we can use them interchangeably. So insteady of defining a block to pass to flatMap we can just pass the <code class="highlighter-rouge">Person.init</code> method directly!</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">people</span> <span class="o">=</span> <span class="n">jsonArray</span><span class="o">.</span><span class="nf">flatMap</span><span class="p">(</span><span class="kt">Person</span><span class="o">.</span><span class="kd">init</span><span class="p">)</span>
</code></pre>
</div>
<p>With good meaningful varivables and call site context this is still clear at first glance what is happening. The irony of using a lot of words to promote less words in code.</p>George WebsterFor many years of iOS development each team I have worked with has adobted a style guide usually based off of Apple’s Cocoa Coding Guidelines. Every team has their own style but mostly they all have been inspired by Cocoa and hence are very descriptive. Over time I grew to love this, it was clear and self documenting if at the cost of being especially verbose. More than anything I always wanted my code to not look and feel any different from Apple’s code. This changed last summer with the introduciton of swift 3 and Swift’s API Design Guidelines.Mac and iOS Developer Podcasts2014-07-09T10:43:00-07:002014-07-09T10:43:00-07:00https://ethyreal.com/ios/mac/podcasts/mac-ios-developer-podcasts<p>About the time I got into iOS development I also started listening to podcasts. There are many great podcasts out there but for an Apple developer there are some really exception ones.</p>
<p>For general tech news:</p>
<p>The <a href="http://atp.fm/">Accidental Tech Podcast</a> is a tech podcast accidentally created while Marco Arment, Casey Liss, and John Siracusa were attempting to to do a car show called <a href="http://neutral.fm/">Neutral</a> I discovered Marco and John while listening to there respective podcasts on the <a href="http://5by5.tv/">5by5 network</a>. I enjoyed their weekly take on tech news and development so much I was genuinely sad when they ended their respective shows. Thankfully they started podcasting again and were joined by Casey who I wasn’t familiar with but I have found adds a great balance to the strong opinions of Marco and John and usually keeps them on topic.</p>
<p>John Gruber’s <a href="http://daringfireball.net/thetalkshow/">The Talk Show</a> is probably the most well known of these. He’s been writing for many years on Apple and each weeks brings in interesting guests to discuss that weeks news topics of note.</p>
<p>There are several great interview format podcasts that focus on interesting aspects of software history and current projects around the Apple world:</p>
<p><a href="http://www.imore.com/debug">Debug</a> is a conversational interview show between <a href="http://kickingbear.com/blog/">Guy English</a> and various developers in the community. He is joined by Rene Ritchie from iMore who I believe produces the show and seems to serves as moderator incase Guy and their guest get too off track. It bills as being about developing software and services, primarily for iPhone, iPad, Mac, and gaming. However what I’ve experienced is wonderful conversations with developers about how they got started programming and their journeys through software development. In many cases fascinating historical accounts of the creation process behind many of the apps and tools I know and love.</p>
<p><a href="http://therecord.co/">The Record</a> is a similar show to Debug hosted by Chris Parrish and Brent Simmons. I only recently started listening to it but the episode with <a href="https://twitter.com/bmf">Mike Lee</a> was amazing! The journey he took to becoming a developer and helping to ship <a href="http://delicious-monster.com/">Delicious Library 2</a> was truly inspiring. I have a greater appreciation of why they call him the worlds toughest programmer. I look forward to more excellent historical interviews and going through the backlog of this one.</p>
<p><a href="http://bitsplitting.org/podcast/">Bitsplitting</a> is another interview podcast from Daniel Jalkut that focuses on the personal backgrounds of each guest. Like Debug and the Record each talk is an interesting walk through history and I always love listening to each persons discovery story of computers and eventually coming around to writing software. He ended season 1 back in August of 2013, while Debug and the Record have filled the gap of interesting historical conversations I certainly hope he picks up again and does more interviews in the future.</p>
<p>Saul Mora’s <a href="http://nsbrief.com/">NSBrief</a> is also an interview based podcast but it focuses more on current projects of note and the people behind them. I have learned about many cool projects from this podcasts as well as enjoyed hearing the developers behind some of my favorite servies like HockeyKit speak about them.</p>
<p>For more general Apple Software topics and developer discussion with occasional interviews:</p>
<p><a href="http://www.coreint.org/">Core Intuition</a> is a regular conversation between Daniel Jalkut of <a href="http://www.red-sweater.com/">Red Sweater Software</a> and <a href="http://www.manton.org/">Manton Reece</a>. They discuss indy Mac development, recent news and occasionally have guests on as well. I admittedly do not listen to this one as much as I used to but I’ve always enjoyed there conversations and insite into various topics.</p>
<p>iDeveloper featured Scotty and Jon Fox (sadly the website is no longer available). This was actually one of the first podcasts I ever listened too and I discovered many of the others through listening to them and their guests. They talk about indy development and have great interview guests as well.</p>
<p><a href="http://developingperspective.com/">Developing Perspective</a> is a great short podcast about iOS Development that is never longer than 15 minutes. David Smith shares his thoughts on development and some of the processes he goes through in shipping his many apps.</p>
<p>These last two are not about development but I find them both richly rewarding to listen to:</p>
<p><a href="http://5by5.tv/b2w">Back to Work</a> is podcast with Merlin Mann and Dan Benjamin who ostensibly talk about various issues relating to productivity. However in practice you’ll be entertained with talk about comics, parenting, personal hygiene and deep inside jokes. In the end they eventually come around to discussing aspects of work. All in all a joy to listen to weekly.</p>
<p><a href="http://www.merlinmann.com/roderick/">Roderick on the Line</a> is a weekly candid Skype call between Merlin Mann and John Roderick of the Long Winters. This show is all over the place, but in a good way. Discussions on Fatherhood, philosophy, music, culture, politics. These two friends have a great dynamic, I actually have laughed so hard during this that I have had to pull over while driving. John’s story telling is sheer joy to listen to.</p>
<p>I’m sure there are many other great developer podcasts out there that I haven’t discovered yet, but these have been some that I regularly enjoy.</p>George WebsterAbout the time I got into iOS development I also started listening to podcasts. There are many great podcasts out there but for an Apple developer there are some really exception ones.Playing Games Has Taught Me A Lot2014-06-28T10:43:00-07:002014-06-28T10:43:00-07:00https://ethyreal.com/development/gaming/playing-games-has-taught-me-a-lot<p>A topic came up on the accidental tech podcast about gaming, what the average age of a “gamer” is, does one grow out of it? and gaming being a rich experience in many cases more engaging than a movie or novel. I won’t rehash the discussion here but if you are curious checkout the after show of <a href="http://atp.fm/episodes/62">episode 62</a> and more discussion in the follow up of <a href="http://atp.fm/episodes/63">episode 63</a> and <a href="http://atp.fm/episodes/64">64</a>. These discussions got me thinking a lot about gaming and how it has effected my life.</p>
<p>I grew up with games, my father was a gamer and we had hundreds of board and video games around the house. Family board game nights were a regular thing, Talisman and Illuminati were regular favorites but we played so many I couldn’t list them all. <a href="http://en.wikipedia.org/wiki/German-style_board_game">Euro games</a> didn’t seem to be as popular at the time, or at least they weren’t in our local game shops and before the internet that basically meant they didn’t exist for us. This meant there was lots of late night dice rolling, whether it was were adventuring in <a href="http://www.boardgamegeek.com/boardgame/714/talisman">Talisman</a>, expanding our train empires in <a href="http://www.boardgamegeek.com/boardgame/420/rail-baron">Rail Baren</a> or blowing up ships in <a href="http://boardgamegeek.com/boardgame/1668/modern-naval-battles">Modern Navel Battles</a>. They were all challenging for my young mind and engaging. I think I even learned how to form and deliver an argument thanks to vague rules in certain games. I wasn’t always right and didn’t always win but I learned a lot and had great fun doing it.</p>
<p>Video games were no acception, in the early years it was hot seat games of <a href="http://en.wikipedia.org/wiki/Classic_Empire">empire</a> rotating between two computers. I had to learn how manage my building resources and have enough forces to crush my brother and maybe if I was lucky challenge my father for control of the map. Later we setup a null modem and took our military strategy real time with RTS games like <a href="http://en.wikipedia.org/wiki/Warcraft">Warcraft</a> and <a href="http://en.wikipedia.org/wiki/Command_%26_Conquer">Command & Conquer</a>. Resource management and quick reaction times were key, I learned how multi tasking that way. I had to juggle the competing priorities of resource gathering, base defense and mobilizing an offense all at the same time. If you didn’t you lost plain and simple.</p>
<p>Playing video games also landed me my first programming job. I had moved to Chicago on a whim during the late 90s with no guarantees. My best friend’s brother at the time was working for a firm there and had some idea’s for a personal project and figured I could help him out with it. So moved to Chicago with nothing but a laptop. My first week there I was hanging out at their offices at night playing RTS games like <a href="http://en.wikipedia.org/wiki/Age_of_Empires">Age of Empires</a> with the developers there. I remember we were deep into a multi game session and in walks the CEO fresh from a Duran Duran concert. He walks over to me and says they are looking bring on some more programmers and would I like to be a part of that. I was a little taken aback I had build my schools web site and had learned a little bit of Perl CGI but by no means felt I was a professional programmer yet. I was honestly a little scared, I remember saying that I didn’t want any pressure but I would love to try. He offered to give me a shot with a very modest hourly wage to start. I was thrilled! It wasn’t a lot of money but it was a opportunity of a life time right in the middle of the dot com boom!</p>
<p>I took to it right away. We had a CMS built in Perl that we customized for each client. There were many talented engineers there that taught me a tramendous amount about writing software the intricacies of the Perl language. I layout out interfaces with tables and spacer gifs and explored more rich interaction using <a href="http://en.wikipedia.org/wiki/Dynamic_HTML">DTHML</a>.</p>
<p>Before long I was given a substancial raise and was working with larger more strategic clients. I even took on the role of a lead developer and helped coach junior programmers when they came on board.</p>
<p>Many of these skills are basically archaic by today’s standards but they helped lay a solid foundation in a rich career in software development that I still enjoy doing to this day. Looking back on it all I’m not sure it would have happend if I wasn’t playing video games and developed a love of working with computers to solve problems.</p>George WebsterA topic came up on the accidental tech podcast about gaming, what the average age of a “gamer” is, does one grow out of it? and gaming being a rich experience in many cases more engaging than a movie or novel. I won’t rehash the discussion here but if you are curious checkout the after show of episode 62 and more discussion in the follow up of episode 63 and 64. These discussions got me thinking a lot about gaming and how it has effected my life.CoreText Bold2013-06-14T10:43:00-07:002013-06-14T10:43:00-07:00https://ethyreal.com/ios/fonts/coretext/coretext-bold<p>Simple text editor paradigm, user selects a font from a menu/list and you want to know if they have a bold font or not, so you can show them in the UI. Simply check the symbolic traits on the CTFontRef:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="o">-</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nv">isBoldFont</span><span class="p">:(</span><span class="kt">CTFontRef</span><span class="p">)</span><span class="n">font</span>
<span class="p">{</span>
<span class="kt">NSDictionary</span> <span class="o">*</span><span class="n">traits</span> <span class="o">=</span> <span class="p">(</span><span class="n">__bridge_transfer</span> <span class="kt">NSDictionary</span> <span class="o">*</span><span class="p">)</span><span class="kt">CTFontCopyTraits</span><span class="p">(</span><span class="n">font</span><span class="p">);</span>
<span class="kt">BOOL</span> <span class="n">hasBoldTrait</span> <span class="o">=</span> <span class="p">([[</span><span class="n">traits</span> <span class="nv">objectForKey</span><span class="p">:(</span><span class="n">id</span><span class="p">)</span><span class="n">kCTFontSymbolicTrait</span><span class="p">]</span> <span class="n">unsignedIntValue</span><span class="p">]</span> <span class="o">&</span> <span class="n">kCTFontBoldTrait</span><span class="p">)</span> <span class="o">==</span> <span class="n">kCTFontBoldTrait</span><span class="p">;</span>
<span class="kt">NSString</span> <span class="o">*</span><span class="n">styleAttribute</span> <span class="o">=</span> <span class="p">(</span><span class="n">__bridge_transfer</span> <span class="kt">NSString</span> <span class="o">*</span><span class="p">)</span><span class="kt">CTFontCopyAttribute</span><span class="p">(</span><span class="n">font</span><span class="p">,</span> <span class="p">(</span><span class="kt">CFStringRef</span><span class="p">)</span><span class="n">kCTFontStyleNameAttribute</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">hasBoldTrait</span> <span class="o">||</span> <span class="p">[</span><span class="n">styleAttribute</span> <span class="nv">isEqualToString</span><span class="p">:</span><span class="s">@"Bold"</span><span class="p">])</span> <span class="p">{</span>
<span class="k">return</span> <span class="kt">YES</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="kt">NO</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>
<p>If you want to change it to bold you can create a new font by copying symbolic traits:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="o">-</span><span class="p">(</span><span class="kt">CTFontRef</span><span class="p">)</span><span class="nv">createBoldFontFromFont</span><span class="p">:(</span><span class="kt">CTFontRef</span><span class="p">)</span><span class="n">fromFont</span>
<span class="p">{</span>
<span class="kt">CTFontCreateCopyWithSymbolicTraits</span><span class="p">(</span><span class="n">fromFont</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="kt">NULL</span><span class="p">,</span> <span class="n">kCTFontBoldTrait</span><span class="p">,</span> <span class="n">kCTFontBoldTrait</span><span class="p">);</span>
<span class="p">}</span>
</code></pre>
</div>
<p>This will return NULL if the font family does not have a bold font. You could use this to check the availability of bold or just wether or not you actually created a bold font.</p>
<p>If you want to force a font to render “bold” even though there is no actual bold font face you could change the text drawing mode in your draw rect:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kt">CGContextRef</span> <span class="n">context</span> <span class="o">=</span> <span class="kt">UIGraphicsGetCurrentContext</span><span class="p">();</span>
<span class="kt">CGContextSetTextDrawingMode</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">kCGTextFillStroke</span><span class="p">);</span>
</code></pre>
</div>
<p>That will stroke and fill the glyphs instead of just filling them ( kCGTextFill ) which I believe is the default.</p>George WebsterSimple text editor paradigm, user selects a font from a menu/list and you want to know if they have a bold font or not, so you can show them in the UI. Simply check the symbolic traits on the CTFontRef: