<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Daniel Inoa</title><description>Writing about Swift, iOS, and software engineering.</description><link>https://danielinoa.com/</link><language>en-us</language><item><title>SwiftUI Stacks Insist Upon Themselves</title><link>https://danielinoa.com/posts/swiftui-default-stack-spacing/</link><guid isPermaLink="true">https://danielinoa.com/posts/swiftui-default-stack-spacing/</guid><description>SwiftUI sneaks a little breathing room between views and calls it a default.</description><pubDate>Tue, 14 Apr 2026 00:00:00 GMT</pubDate><content:encoded>import InlineQuiz from &apos;../../components/InlineQuiz.astro&apos;;
&lt;p&gt;As a UIKit-first iOS developer, I&apos;ve been gradually adopting SwiftUI. Some friction is expected when moving from an imperative framework to a declarative one, and not every surprise is evidence of bad design. But &quot;the SwiftUI way&quot; is sometimes just a polite way of saying &quot;the framework made a design choice for you.&quot; &lt;code&gt;HStack&lt;/code&gt; and &lt;code&gt;VStack&lt;/code&gt; are a good example.&lt;/p&gt;
&lt;h2&gt;A Stack Should Stack&lt;/h2&gt;
&lt;p&gt;Consider this code:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;HStack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;The obvious reading is simple: place these views next to each other horizontally. However, that is &lt;strong&gt;not&lt;/strong&gt; quite what happens. SwiftUI slips a gap in there even though the call site never asked for one.&lt;/p&gt;
&lt;p&gt;That gap often looks like &lt;code&gt;8&lt;/code&gt; points, but the number is not really the issue. The issue is that the stack chooses one at all. &lt;a href=&quot;https://developer.apple.com/documentation/swiftui/hstack/init(alignment:spacing:content:)&quot;&gt;Apple&apos;s own initializer&lt;/a&gt; makes the delegation explicit: &lt;code&gt;spacing&lt;/code&gt; is optional, so leaving it out hands the distance back to SwiftUI. The code stops fully describing the layout and starts handing part of it back to the framework.&lt;/p&gt;
&lt;h2&gt;Too Helpful Is Still Wrong&lt;/h2&gt;
&lt;p&gt;A primitive should not smuggle presentation choices into an API that looks neutral at the call site.&lt;/p&gt;
&lt;p&gt;For a layout primitive, the neutral default should be &lt;code&gt;0&lt;/code&gt;. A view without &lt;code&gt;padding&lt;/code&gt; has no padding. A view without &lt;code&gt;offset&lt;/code&gt; stays where it is. Those defaults make sense because they do not invent presentation. Stack spacing should work the same way. Any nonzero spacing is a design choice, and design choices should be explicit.&lt;/p&gt;
&lt;p&gt;The obvious defense is that SwiftUI is trying to encode platform conventions. That is understandable. Apple wants common interfaces to breathe a little by default, and higher-level components can reasonably reflect those conventions. But &lt;code&gt;HStack&lt;/code&gt; and &lt;code&gt;VStack&lt;/code&gt; are not high-level components. They are primitives. A stack should stack, not quietly pick a visual rhythm for you.&lt;/p&gt;
&lt;p&gt;That is why this matters beyond one initializer. SwiftUI often looks concise because behavior has been moved out of the call site and into framework interpretation. That can feel elegant, but it is often less honest. The code looks more concise while saying less.&lt;/p&gt;
&lt;h2&gt;The Practical Rule&lt;/h2&gt;
&lt;p&gt;My practical rule is simple: always set &lt;code&gt;spacing&lt;/code&gt;, especially when you intend for it to be &lt;code&gt;0&lt;/code&gt;. Use a linter if you have to. If the framework wants breathing room, your code should say so.&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;HStack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;spacing&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zero&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token class-name&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content:encoded></item><item><title>Ternary Unwrapping in Swift</title><link>https://danielinoa.com/posts/ternary-unwrapping/</link><guid isPermaLink="true">https://danielinoa.com/posts/ternary-unwrapping/</guid><description>A custom Swift operator that combines ternary conditional and nil-coalescing for elegant optional unwrapping.</description><pubDate>Mon, 20 Nov 2017 00:00:00 GMT</pubDate><content:encoded>&lt;h2&gt;The Issue&lt;/h2&gt;
&lt;p&gt;Often enough I find myself writing something like this:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; foo&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; unwrappedDependency &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optionalDependency &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    foo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unwrappedDependency&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    foo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fallback&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here I have a &lt;code&gt;foo&lt;/code&gt; that needs to be instantiated but its dependency is an optional that has to be dealt with first. This is easily solved by unwrapping said optional through an &lt;code&gt;if-let&lt;/code&gt; or &lt;code&gt;guard-let&lt;/code&gt; statement. &lt;strong&gt;Simple, but it takes just too many lines (depending how you count) to express such a simple flow of statements&lt;/strong&gt;, which could potentially be repeated many times throughout a codebase. This calls for a more elegant solution.&lt;/p&gt;
&lt;p&gt;Swift already comes with some great constructs like the ternary and nil-coalescing operators that help in writing more expressive code and reducing verbosity for code as the one described above.&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Nil Coalescing Operator&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; optionalNumber&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optionalNumber &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; nonOptionalfallback&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Ternary Conditional Operator&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; condition&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; condition &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, neither of these operators would be helpful in satisfying the requirements for an inline statement that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;evaluates an optional&lt;/li&gt;
&lt;li&gt;provides a path for a successful unwrapping&lt;/li&gt;
&lt;li&gt;provides a path if the unwrapping fails&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The Solution&lt;/h2&gt;
&lt;p&gt;Since there&apos;s no operator that meets those requirements, we can then leverage Swift&apos;s ability to define and implement a new operator that would allow us to do just that by combining the ternary conditional and nil-coalescing operators.
I&apos;m going to call it &lt;strong&gt;ternary unwrapping operator&lt;/strong&gt;, and it looks like this:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;optional&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; unwrapped &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and it allows us to simplify the aforementioned flow like this:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; foo&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optionalDependency &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token short-argument&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fallback&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&apos;s dissect this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Between &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;??&lt;/code&gt; we have an optional that&apos;s to be evaluated.&lt;/li&gt;
&lt;li&gt;If said optional can be safely unwrapped then the statement between &lt;code&gt;??&lt;/code&gt; and &lt;code&gt;|&lt;/code&gt; is executed. Here we have a closure that forwards the unwrapped optional and is used to create an instance of the expected return type (in this case &lt;code&gt;Foo&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The statement after &lt;code&gt;|&lt;/code&gt; serves as a fallback and must also resolve to an instance of the expected return type.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Use Case&lt;/h2&gt;
&lt;p&gt;Out of all the many scenarios where this operator could be useful, I&apos;ve found that it is especially useful when doing string interpolation with an optional.&lt;/p&gt;
&lt;p&gt;Instead of doing this:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// This approach is overwrought.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; optionalNumber&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; sentence&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; number &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optionalNumber &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    sentence &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;My favorite number is &lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    sentence &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token nil constant&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or this:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// This approach force unwraps which isn&apos;t ideal.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; optionalNumber&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; sentence&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optionalNumber &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token nil constant&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;My favorite number is &lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;optionalNumber&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token nil constant&quot;&gt;nil&lt;/span&gt; &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We could do this instead:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// This approach is concise and leverages the ternary unwrapping operator.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; optionalNumber&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; sentence &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optionalNumber &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;My favorite number is &lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token short-argument&quot;&gt;$0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token nil constant&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Under The Hood&lt;/h2&gt;
&lt;p&gt;This ternary unwrapping operator is in fact 2 &lt;code&gt;infix&lt;/code&gt; operators used in conjunction.&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;precedencegroup&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Group&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;associativity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;infix&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Group&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; input&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;               &lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; handler&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; input &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; handler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;rhs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; handler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;lhs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;infix&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Group&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; lhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token attribute atrule&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;              &lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; rhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token attribute atrule&quot;&gt;@autoclosure&lt;/span&gt; &lt;span class=&quot;token attribute atrule&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;This first operator, &lt;code&gt;func ??&lt;/code&gt;, takes 2 parameters: an optional and a tuple. This function &lt;code&gt;??&lt;/code&gt; by itself is in fact sufficient as an operator. However, &lt;code&gt;func |&lt;/code&gt; is needed to improve the style that match that of a ternary operator.&lt;/li&gt;
&lt;li&gt;The second operator, &lt;code&gt;func |&lt;/code&gt;, acts as a façade by taking 2 closures and returning them in a tuple; later used by &lt;code&gt;??&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;I&lt;/code&gt; (input) and &lt;code&gt;O&lt;/code&gt; (output) describe the type of the optional argument and that of the returning value (used in the assignment), respectively.&lt;/p&gt;
&lt;h2&gt;Closing Thoughts&lt;/h2&gt;
&lt;p&gt;One of my main priorities as a developer is to improve the readability of the code I write, especially if it is to be reviewed by my peers. I&apos;d like to believe that what the ternary conditional operator has done for &lt;code&gt;if-else&lt;/code&gt; statement, this new operator could for the &lt;code&gt;if-let-else&lt;/code&gt; unwrapping statement; helping developers write code that will be easy for others to understand.&lt;/p&gt;
&lt;p&gt;This ternary unwrapping operator, however, is best suited for cases such as when the instantiation or assignment of an object/value depends on 1 optional dependency needing to be unwrapped. For more complex cases it might be wiser to stick with &lt;code&gt;if-let&lt;/code&gt; and &lt;code&gt;guard-let&lt;/code&gt;. As always, all the operators described here are best used sparingly when readability of code can truly be improved, and not just to write fancy or unreasonably terse code.&lt;/p&gt;
&lt;p&gt;Did you find this interesting and would you like to experiment with it? Check out the sample &lt;a href=&quot;https://github.com/danielinoa/Ternary-Unwrapping&quot;&gt;playground&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thanks for reading!&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;See Also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID72&quot;&gt;Nil-Coalescing Operator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID71&quot;&gt;Ternary Conditional Operator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-ID41&quot;&gt;Custom Operators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title>Function Composition in Swift</title><link>https://danielinoa.com/posts/function-composition/</link><guid isPermaLink="true">https://danielinoa.com/posts/function-composition/</guid><description>Learn how to chain functions together using custom operators in Swift for cleaner, more readable code.</description><pubDate>Tue, 31 Oct 2017 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;It&apos;s common to perform an operation on a value and use the resulting value of that operation as the argument for a subsequent operation, and so on.&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Definitions&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; x&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; val&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CustomStringConvertible&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-literal&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;The resulting value is: &lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Usage&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;half&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &quot;The resulting value is: 10&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works fine but the code quickly starts looking like &lt;a href=&quot;https://racket-lang.org/&quot;&gt;Racket&lt;/a&gt;, and becomes hard to unpack.
Another way to do this same thing is by leveraging function composition, which entails chaining many functions together into one. This idea of concatenating functions can be accomplished in Swift by taking advantage of generics and the infix operator.&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Custom Operator Definition&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;precedencegroup&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Group&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;associativity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;infix&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Group&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; lhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token attribute atrule&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;                   &lt;span class=&quot;token omit keyword&quot;&gt;_&lt;/span&gt; rhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token attribute atrule&quot;&gt;@escaping&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rhs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;lhs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token short-argument&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we have a function that takes 2 parameters and returns a closure.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The first parameter in &lt;code&gt;&gt;&gt;&gt;&lt;/code&gt; is a function that takes 1 parameter of type &lt;code&gt;A&lt;/code&gt; and returns an instance of type &lt;code&gt;B&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The second parameter in &lt;code&gt;&gt;&gt;&gt;&lt;/code&gt; is a function that takes 1 parameter of type &lt;code&gt;B&lt;/code&gt; (this is where the concatenation occurs) and returns an instance of type &lt;code&gt;C&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Lastly, &lt;code&gt;&gt;&gt;&gt;&lt;/code&gt; returns yet another function which takes 1 parameter of type &lt;code&gt;A&lt;/code&gt; and returns an instance of type &lt;code&gt;C&lt;/code&gt;. Type &lt;code&gt;B&lt;/code&gt;, being both the output for the 1st parameter and input for the 2nd one, serves as the knot between &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The body of the function runs the result of &lt;code&gt;lhs&lt;/code&gt; on &lt;code&gt;rhs&lt;/code&gt; and returns. These functions are nested internally so that the function parameters, &lt;code&gt;lhs&lt;/code&gt; and &lt;code&gt;rhs&lt;/code&gt;, can be described as a sequence externally.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We could then do the following:&lt;/p&gt;
&lt;pre class=&quot;language-swift has-line-numbers&quot; data-language=&quot;swift&quot;&gt;&lt;code is:raw=&quot;&quot; class=&quot;language-swift&quot;&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Usage With Custom Operator&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;line&quot;&gt; &lt;/span&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;half &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&gt;&lt;/span&gt; square &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&gt;&lt;/span&gt; increment &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&gt;&lt;/span&gt; describe&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &quot;The resulting value is: 10&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This then becomes a better and more legible approach of describing a sequence of operations, where functions are chained instead of being nested.&lt;/p&gt;
&lt;hr&gt;
&lt;ul&gt;
&lt;li&gt;Shout-out to &lt;a href=&quot;https://twitter.com/mbrandonw&quot;&gt;Brandon Williams&lt;/a&gt; for showing me this stuff at Swift Summit 2017.&lt;/li&gt;
&lt;li&gt;If you find this interesting, check out this &lt;a href=&quot;https://en.wikipedia.org/wiki/Function_composition_(computer_science)&quot;&gt;Wikipedia article on Function Composition&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title>Rendering Markdown in Xcode 9</title><link>https://danielinoa.com/posts/markdown-in-xcode9/</link><guid isPermaLink="true">https://danielinoa.com/posts/markdown-in-xcode9/</guid><description>Discover how to enable full Markdown rendering in Xcode 9 projects using a hidden Apple configuration file.</description><pubDate>Tue, 19 Sep 2017 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Recently while exploring Apple&apos;s new sample projects for iOS 11, I stumbled upon this sample &lt;a href=&quot;https://developer.apple.com/sample-code/wwdc/2017/PlacingObjects.zip&quot;&gt;ARKit project&lt;/a&gt;. Aside from the cool ARKit stuff going on, I saw how the README.md would actually get rendered.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/images/markdown-in-xcode9/xcode9_markdown_render.png&quot; alt=&quot;Xcode Markdown Render&quot;&gt;&lt;/p&gt;
&lt;p&gt;This has been possible in Playgrounds for a while but never in an actual Xcode project, until now. This markdown rendition is different from the one advertised, where the Markdown is partially rendered and the syntax is included (see image below).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/images/markdown-in-xcode9/xcode9_markdown_testproject_norender.png&quot; alt=&quot;Xcode Markdown No Render&quot;&gt;&lt;/p&gt;
&lt;p&gt;After some &quot;extensive&quot; research and excavation I found that &lt;code&gt;.xcodesamplecode.plist&lt;/code&gt; is what triggers Xcode to render the Markdown as seen in their sample. This file can be found under the &lt;code&gt;.xcodeproj&lt;/code&gt; (within Package Contents).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/images/markdown-in-xcode9/xcodesamplecodeplist.png&quot; alt=&quot;xcodesamplecode.plist location&quot;&gt;&lt;/p&gt;
&lt;p&gt;It seems Apple intended for Markdown files to be rendered only if they are under a sample project. Ideally there would be a way to toggle when it renders, similar to Playgrounds, but for now the simplest way is to drop the &lt;code&gt;.xcodesamplecode.plist&lt;/code&gt; file under your &lt;code&gt;.xcodeproj&lt;/code&gt; project.&lt;/p&gt;
&lt;p&gt;After dropping &lt;code&gt;.xcodesamplecode.plist&lt;/code&gt; under the TestProject.xcodeproject I created it now renders the Markdown as expected. &lt;a href=&quot;/images/markdown-in-xcode9/xcodesamplecode.plist.zip&quot;&gt;Download &lt;code&gt;.xcodesamplecode.plist&lt;/code&gt; here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TADA!!!&lt;/strong&gt; 🎉&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/images/markdown-in-xcode9/xcode9_markdown_testproject_render.png&quot; alt=&quot;Xcode Markdown Rendered&quot;&gt;&lt;/p&gt;
&lt;p&gt;I think having renderable README.md files within Xcode could go a long way in helping make large projects easier to document and unpack, without the need to have external Markdown renderers. In addition to code comments this could greatly improve discoverability for new people coming into your project, as long as your project is well structured.&lt;/p&gt;
&lt;p&gt;Thanks for reading!&lt;/p&gt;
&lt;p&gt;Questions about any of this? Ask me on &lt;a href=&quot;https://twitter.com/danielinoa_&quot;&gt;Twitter&lt;/a&gt; or email.&lt;/p&gt;</content:encoded></item></channel></rss>