<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Posts on matthewburke.com</title>
        <link>http://matthewburke.com/posts/</link>
        <description>Recent content in Posts on matthewburke.com</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <copyright>Matthew M. Burke</copyright>
        <lastBuildDate>Tue, 05 Feb 2019 07:43:10 -0400</lastBuildDate>
        <atom:link href="http://matthewburke.com/posts/index.xml" rel="self" type="application/rss+xml" />
        
        <item>
            <title>Serverless Computing with Swift: Part II</title>
            <link>http://matthewburke.com/posts/serverless-swift-ii/</link>
            <pubDate>Tue, 05 Feb 2019 07:43:10 -0400</pubDate>
            
            <guid>http://matthewburke.com/posts/serverless-swift-ii/</guid>
            <description>The second part of my story on Swift and serverless computing was published on Hacker Noon. For various, uninteresting reasons, I wound up publishing there instead of on Medium with Part I.</description>
            <content type="html"><![CDATA[<p>The <a href="https://hackernoon.com/serverless-computing-with-swift-4fa27ab36161">second part</a> of my story on Swift and serverless computing was published on <a href="http://hackernoon.com">Hacker Noon</a>. For various, uninteresting reasons, I wound up publishing there instead of on <a href="http://medium.com">Medium</a> with <a href="/posts/serverless-swift-i/">Part I</a>.</p>
]]></content>
        </item>
        
        <item>
            <title>Serverless Computing with Swift: Part I</title>
            <link>http://matthewburke.com/posts/serverless-swift-i/</link>
            <pubDate>Wed, 04 Apr 2018 07:43:07 -0400</pubDate>
            
            <guid>http://matthewburke.com/posts/serverless-swift-i/</guid>
            <description>Update 2020-06-10: The linked article is possibly still of some value for the example of a lambda function in Swift (not to mention the world&amp;rsquo;s best bakery name). However, the convoluted steps necessary to compile Swift for the AWS environment are no longer necessary with the introduction of the Swift AWS Runtime.
 I wrote a short article on using Swift and AWS Lambda to write serveless functions. Directing you to another site is, I suppose, not a swift move from a social media perspective.</description>
            <content type="html"><![CDATA[<blockquote>
<p><strong>Update 2020-06-10:</strong> The linked article is possibly still of some value for the example of a lambda function in Swift (<em>not to mention the world&rsquo;s best bakery name</em>). However, the convoluted steps necessary to compile Swift for the AWS environment are no longer necessary with the introduction of the <a href="https://swift.org/blog/aws-lambda-runtime/">Swift AWS Runtime</a>.</p>
</blockquote>
<p>I wrote a short article on using Swift and AWS Lambda to write serveless functions.
Directing you to another site is, I suppose, not a <em>swift</em> move from a social media perspective. Nevertheless, go have a <a href="https://medium.com/capital-one-tech/serverless-computing-with-swift-f515ff052919">look</a>.</p>
]]></content>
        </item>
        
        <item>
            <title>Just Because You Can</title>
            <link>http://matthewburke.com/posts/just-because-you-can/</link>
            <pubDate>Sun, 12 Oct 2014 13:17:06 -0400</pubDate>
            
            <guid>http://matthewburke.com/posts/just-because-you-can/</guid>
            <description>The other day, I read the post Boolean on Apple&amp;rsquo;s Swift Blog. The post sparked a flash of inspiration, so I opened up Xcode and entered the following code:
import Foundationn if &amp;#34;this is a test&amp;#34; { println(&amp;#34;This can&amp;#39;t work.&amp;#34;) } Perhaps you are expecting a compiler error. Nope. The output is, of course,
 This can&#39;t work.
 Then I tried the following:
if &amp;#34;this is not a test&amp;#34; { println(&amp;#34;No, really, this can&amp;#39;t work.</description>
            <content type="html"><![CDATA[<p>The other day, I read the post <a href="https://developer.apple.com/swift/blog/?id=8">Boolean</a> on Apple&rsquo;s Swift Blog. The post sparked a flash of inspiration, so I opened up Xcode and entered the following code:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-swift" data-lang="swift"><span style="color:#66d9ef">import</span> <span style="color:#a6e22e">Foundationn</span>

<span style="color:#66d9ef">if</span> <span style="color:#e6db74">&#34;this is a test&#34;</span> {
   println(<span style="color:#e6db74">&#34;This can&#39;t work.&#34;</span>)
}
</code></pre></div><p>Perhaps you are expecting a compiler error. Nope. The output is, of course,</p>
<blockquote>
<p><code>This can't work.</code></p>
</blockquote>
<p>Then I tried the following:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-swift" data-lang="swift"><span style="color:#66d9ef">if</span> <span style="color:#e6db74">&#34;this is not a test&#34;</span> {
   println(<span style="color:#e6db74">&#34;No, really, this can&#39;t work.&#34;</span>)
}
</code></pre></div><p>Xcode displayed the output:</p>
<blockquote>
<p><code>No, really, this can't work.</code></p>
</blockquote>
<p>One last code snippet:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-swift" data-lang="swift"><span style="color:#66d9ef">if</span> <span style="color:#e6db74">&#34;you know what&#39;s going on&#34;</span> {
   println(<span style="color:#e6db74">&#34;You&#39;re a genius!&#34;</span>)
} <span style="color:#66d9ef">else</span> {
   println(<span style="color:#e6db74">&#34;So sad!&#34;</span>)
}
</code></pre></div><p>I&rsquo;ll let you determine for yourself the appropriate output.</p>
<h3 id="the-point-of-the-exercise">The point of the exercise</h3>
<p>To be honest, there&rsquo;s not much of an actual point here. I just found the Swift code amusing.</p>
<p>Sure, I could argue against using overly-clever code in production. And I would never use a trick like this in production code. But I don&rsquo;t need to argue against this practice, because nobody&rsquo;s arguing for it.</p>
<p>Now, before you repsond &ldquo;<em><a href="http://www.theatlantic.com/technology/archive/2013/11/english-has-a-new-preposition-because-internet/281601/">because DSL</a></em>&rdquo;, let me point out, I&rsquo;m all for <a href="http://c2.com/cgi/wiki?DomainSpecificLanguage">domain specific languages</a> (DSLs). I&rsquo;ve given talks on DSLs in Forth and TCL (such as <a href="http://tcl.tk/community/tcl2008/abstract/burke.pdf">this one</a>. I&rsquo;m jealous that Mattt (<em>not a typo</em>) Thompson beat me to the punch with <a href="https://github.com/mattt/Euler">Euler</a>. So, I&rsquo;m all in favor of DSLs, not to mention operator overloading, code generators, and metaprogramming. They&rsquo;re all good!</p>
<p>But the primary purpose of code is to <!-- raw HTML omitted -->clearly<!-- raw HTML omitted --> communicate with other programmers. (<em>What? You thought it was to tell the computer what to do? That&rsquo;s soooo quaint!</em>) One of my favorite CS-related quotations is from <a href="http://en.wikipedia.org/wiki/Brian_Kernighan">Brian Kernighan</a>:</p>
<blockquote>
<p>Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.</p>
</blockquote>
<p>Metaprogramming, operator overloading, etc. often times make our code harder to read (<!-- raw HTML omitted -->hence, harder to understand/debug<!-- raw HTML omitted -->) because they increase the amount of context we have to maintain. There are many situations where using macros (<em>real macros, Lisp-like macros, not puny C preprocessor macros</em>), or operator overloading, or creating a DSL is warranted because it clarifies our communication, but every time we are tempted to use such a technique, we have to ask ourselves are we using this technique because we need to, or are we using it <strong>because we can</strong>?</p>
<hr>
<h5 id="the-code-behind-the-curtain">The code behind the curtain</h5>
<p>First, I should fess up. In case you didn&rsquo;t notice it, at the beginning of the code listing, it&rsquo;s not <em>Foundation</em> that I&rsquo;m importing, it&rsquo;s <em>Foundation<!-- raw HTML omitted -->n<!-- raw HTML omitted --></em>. The former is Apple&rsquo;s module containing definitions for all the, <em>uhm, well</em>, foundational classes. The later is a module I cooked up to be deceptive^Wclever. I&rsquo;ll list the code below.</p>
<p>The main decision to make is how to map strings to boolean values. Once you&rsquo;ve decided on a mapping approach it&rsquo;s a simple matter of using an extension so that <code>String</code> implements the <code>BooleanType</code> protocol. I did it as follows:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-swift" data-lang="swift"><span style="color:#66d9ef">extension</span> <span style="color:#a6e22e">String</span>: BooleanType {
   <span style="color:#66d9ef">var</span> boolValue: Bool {
      <span style="color:#66d9ef">get</span> {
         <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">self</span>.contains(<span style="color:#e6db74">&#34;test&#34;</span>) {
            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
         } <span style="color:#66d9ef">else</span> {
           <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
         }
      }
   }
}
</code></pre></div><p><code>String</code> (<em>at least as of Beta 5 and, yes, I&rsquo;ve been a little slow posting this</em>) doesn&rsquo;t implement <code>contains</code>. I use code which I grabbed from <a href="http://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift">StackOverflow</a>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-swift" data-lang="swift"><span style="color:#66d9ef">extension</span> <span style="color:#a6e22e">String</span> {
    <span style="color:#66d9ef">func</span> <span style="color:#a6e22e">contains</span>(other: String) -&gt; Bool {
      <span style="color:#66d9ef">var</span> start = startIndex

      <span style="color:#66d9ef">do</span> {
        <span style="color:#66d9ef">var</span> subString = <span style="color:#66d9ef">self</span>[Range(start: start<span style="color:#f92672">++</span>, end: endIndex)]
        <span style="color:#66d9ef">if</span> subString.hasPrefix(other) {
            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
        }

    } <span style="color:#66d9ef">while</span> start <span style="color:#f92672">!=</span> endIndex

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
  }
}
</code></pre></div><p>I wound up with the above code to check string containment after trying a half-dozen different approaches that various people claimed worked. I suspect they all did for one beta or another. Presumably with the 1.0 release of Swift (and 1.1 around the corner), the pace of change in Swift will slow down (<em>but not stop, since much as I like Swift, it still has a lot of growing up to do</em>).</p>
<p>If you would like to play around with branching on strings, you can download the workspace I created from <!-- raw HTML omitted --><a href="https://github.com/profburke/ObscureSwift">https://github.com/profburke/ObscureSwift</a><!-- raw HTML omitted -->. I would like to collect more Swift oddities, so feel free to add an issue or send push requests if you have anything to contribute. Given the previous paragraph, YMMV.</p>
<h4 id="spanning-xcode-betas">Spanning Xcode betas</h4>
<p>One final note. I was working on this during some less-than-ample-spare time and new Xcode/Swift betas kept popping up before I was finished.</p>
<p>This was not fun.</p>
<p>In case this remains an issue with new releases, here&rsquo;s the little dance I had to perform for each new version in order to get Swift to recognize the Foundationn module:</p>
<ul>
<li>delete the derived directory for the project</li>
<li>remove the pcm file from the module cache (under the DerivedData directory)</li>
<li>delete the playground file</li>
<li>build the module</li>
<li>recreate the playground file</li>
</ul>
<p>Once the Xcode release for Yosemite comes out, I&rsquo;ll test this again, and submit a radar as necessary.</p>
]]></content>
        </item>
        
        <item>
            <title>Keeping Your Code DRY Through Data Abstraction and Metaprogramming</title>
            <link>http://matthewburke.com/posts/dry-code/</link>
            <pubDate>Fri, 30 Aug 2013 07:46:17 -0400</pubDate>
            
            <guid>http://matthewburke.com/posts/dry-code/</guid>
            <description>Recently I was working on a project that required keeping audit logs of changes to tables in the database. For each table that had to be logged, I attached INSERT, UPDATE and DELETE triggers to record all changes. The approach looked roughly like the following (using MySQL as the database):
CREATE TABLE members ( member_id INTEGER PRIMARY KEY AUTO_INCREMENT, member_name VARCHAR(128) NOT NULL, member_state CHAR(2) NOT NULL, INDEX state_idx (member_state), FOREIGN KEY (member_state) REFERENCES postal_abbreviations (abbreviation) ) ENGINE=INNODB; CREATE TABLE members_journal ( journal_id INTEGER PRIMARY KEY AUTO_INCREMENT, operation VARCHAR(16) NOT NULL, member_id INTEGER NOT NULL, member_name VARCHAR(128) NULL, member_state CHAR(2) NULL ) ENGINE=INNODB; DROP TRIGGER IF EXISTS members_insert_trigger; DELIMITER | CREATE TRIGGER members_insert_trigger AFTER INSERT ON members FOR EACH ROW BEGIN INSERT INTO members_journal (operation, member_id, member_name) VALUES (&amp;lt;span class=&amp;#34;s1&amp;#34;&amp;gt;&amp;#39;insert&amp;#39;, NEW.</description>
            <content type="html"><![CDATA[<p>Recently I was working on a project that required keeping audit logs of changes to tables in the database. For each table that had to be logged, I attached INSERT, UPDATE and DELETE triggers to record all changes. The approach looked roughly like the following (<em>using MySQL as the database</em>):</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TABLE</span> members (
   member_id INTEGER <span style="color:#66d9ef">PRIMARY</span> <span style="color:#66d9ef">KEY</span> AUTO_INCREMENT,
   member_name VARCHAR(<span style="color:#ae81ff">128</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span>,
   member_state CHAR(<span style="color:#ae81ff">2</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span>,
   <span style="color:#66d9ef">INDEX</span> state_idx (member_state),
   <span style="color:#66d9ef">FOREIGN</span> <span style="color:#66d9ef">KEY</span> (member_state) <span style="color:#66d9ef">REFERENCES</span> postal_abbreviations (abbreviation)
) ENGINE<span style="color:#f92672">=</span>INNODB;

<span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TABLE</span> members_journal (
  journal_id    INTEGER <span style="color:#66d9ef">PRIMARY</span> <span style="color:#66d9ef">KEY</span> AUTO_INCREMENT,
  <span style="color:#66d9ef">operation</span>     VARCHAR(<span style="color:#ae81ff">16</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span>,
  member_id     INTEGER <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span>,
  member_name   VARCHAR(<span style="color:#ae81ff">128</span>) <span style="color:#66d9ef">NULL</span>,
  member_state  CHAR(<span style="color:#ae81ff">2</span>) <span style="color:#66d9ef">NULL</span>
) ENGINE<span style="color:#f92672">=</span>INNODB;

<span style="color:#66d9ef">DROP</span> <span style="color:#66d9ef">TRIGGER</span> <span style="color:#66d9ef">IF</span> <span style="color:#66d9ef">EXISTS</span> members_insert_trigger;

<span style="color:#66d9ef">DELIMITER</span> <span style="color:#f92672">|</span>
<span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TRIGGER</span> members_insert_trigger <span style="color:#66d9ef">AFTER</span> <span style="color:#66d9ef">INSERT</span> <span style="color:#66d9ef">ON</span> members
<span style="color:#66d9ef">FOR</span> <span style="color:#66d9ef">EACH</span> <span style="color:#66d9ef">ROW</span>
<span style="color:#66d9ef">BEGIN</span>
  <span style="color:#66d9ef">INSERT</span> <span style="color:#66d9ef">INTO</span> members_journal (<span style="color:#66d9ef">operation</span>, member_id, member_name)
     <span style="color:#66d9ef">VALUES</span> (<span style="color:#f92672">&lt;</span>span <span style="color:#66d9ef">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;s1&#34;</span><span style="color:#f92672">&gt;</span><span style="color:#e6db74">&#39;insert&#39;</span>, <span style="color:#66d9ef">NEW</span>.member_id, <span style="color:#66d9ef">NEW</span>.member_name, <span style="color:#66d9ef">NEW</span>.member_state);
<span style="color:#66d9ef">END</span>;
<span style="color:#f92672">|</span>
<span style="color:#66d9ef">DELIMITER</span> ;

<span style="color:#75715e">-- more trigger definitions here. . .
</span></code></pre></div><p>Briefly: for each primary table, such as <em><strong>members</strong></em>, you create a table to record the changes, <em><strong>members_journal</strong></em>. The journal table should include all the columns from the primary table, without constraints, as well as its own primary key and a column to include the operation type (insert, update, delete). Typically, you would want to include a timestamp in the journal table, but I have not shown it, as the example source code is already fairly lengthy. Then you create triggers to insert information into the journal table each time the primary table is modified.</p>
<p>There are some things which you need to be concerned with if you use this technique. The first that comes to mind is performance. But in this particular case, it’s not too much of a concern because once we went into production, the content would change infrequently and only a handful of users would have permission to change the content.</p>
<p>More pressing for us was the fact that a) we were making fairly frequent and extensive schema changes during development and b) unlike the example above, our tables had lots of columns.</p>
<p>You can probably see the implication of the above. We would update our code and recreate the database only to find that the column lists in the table definitions and the column lists in the trigger definitions were out of sync. Keep in mind that for each table being audited we had four column lists to keep consistent: one table and three triggers.</p>
<p>Now let&rsquo;s change gears for a moment and consider software development principles. DRY is an acronym formed from the coding aphorism Don&rsquo;t Repeat Yourself (<em>also known as Single Source of Truth</em>). The essence of the principle is that, according to Hunt and Thomas in <em>The Pragmatic Programmer</em>, &ldquo;[e]very piece of knowledge must have a single, unambiguous, authoritative representation within a system.&rdquo;</p>
<p>Why keep your code DRY? Because if you do not, dire things happen! You find yourself having to fix the same bug in multiple places (<em>and always, always, always forgetting to correct one of the instances</em>), or introducing inconsistencies in your software, or not seeing opportunities to simplify your implementation because you&rsquo;re blinded by lots of excess code.</p>
<p>Now back to my database problem. Clearly, if you&rsquo;re not DRY, then you must be WET (<em>write everything twice</em>). Except in my case, I&rsquo;m beyond WET because I&rsquo;m writing everything four times!</p>
<p>I needed a mechanism that allowed me to avoid this duplication. There are several approaches I could have taken including the following:</p>
<ul>
<li>Write the SQL statements to create the tables then parse these SQL statements to auto-generate the trigger definitions.</li>
<li>Define the tables and then pull the necessary information to generate the triggers from the database&rsquo;s information schema.</li>
<li>Describe the table columns using something more abstract than SQL code and auto-generate <em>both</em> the table definitions and the trigger definitions from this same source.</li>
</ul>
<p>I chose the third option. There remained the decision of how to represent the column definitions and how to do the auto-generation. An obvious choice would be to use XML as the data representation and use XSLT to do the transformation. I leave it as an exercise for the reader to research this option.</p>
<p>Instead, I chose to use the language <a href="http://lua.org">Lua</a>. Lua is an elegant, powerful scripting language that began as a data description language in the early 1990s. Although it has evolved significantly since then, it has never lost its powerful data description capabilities. I’ll discuss Lua in more detail in a future blog post.</p>
<p>Using Lua, I created a data structure to describe each of the primary tables. Using the example schema from above, the data structure looks as follows:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-lua" data-lang="lua">schema <span style="color:#f92672">=</span> {
  members <span style="color:#f92672">=</span> {
     fields <span style="color:#f92672">=</span> {
        { <span style="color:#e6db74">&#39;member_id&#39;</span>, <span style="color:#e6db74">&#39;integer&#39;</span>, <span style="color:#e6db74">&#39;auto_increment&#39;</span>, },
        { <span style="color:#e6db74">&#39;member_name&#39;</span>, <span style="color:#e6db74">&#39;varchar(128)&#39;</span>, <span style="color:#e6db74">&#39;not null&#39;</span>, },
        { <span style="color:#e6db74">&#39;member_state&#39;</span>, <span style="color:#e6db74">&#39;char(2)&#39;</span>, <span style="color:#e6db74">&#39;not null&#39;</span>, },
     },

     primary_key <span style="color:#f92672">=</span> {
        <span style="color:#e6db74">&#39;member_id&#39;</span>,
     },

     foreign_keys <span style="color:#f92672">=</span> {
        { <span style="color:#e6db74">&#39;state&#39;</span>, <span style="color:#e6db74">&#39;postal_abbreviations&#39;</span>, <span style="color:#e6db74">&#39;abbrev&#39;</span>, },
     },

     indices <span style="color:#f92672">=</span> {
        <span style="color:#e6db74">&#39;state&#39;</span>,
     },
   }

   some_other_table <span style="color:#f92672">=</span> {
                    . . .
                    . . .
   }
</code></pre></div><p>A few points about the above code: in Lua, the primary data structure is the <em><strong>table</strong></em>, which can be treated as an array, an associative array (<em>hash</em>), or both at the same time. The <em>schema</em> table includes entries for each primary table in my database. Each table entry describes the fields and auxiliary information such as table constraints, keys, and indices to be created. As with many scripting languages, if you attempt to access a key in a table that doesn&rsquo;t exist, you get the special value, <em>nil</em>, returned. This means that your data definitions can be sparse.</p>
<p>With the above data structure in hand, we can now easily write different scripts to process the schema in different ways. For example, below is the start of a script to generate the table definition code.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-lua" data-lang="lua"><span style="color:#66d9ef">for</span> tableName, tableDefinition <span style="color:#66d9ef">in</span> pairs(schema) <span style="color:#66d9ef">do</span>
  print(string.format(<span style="color:#e6db74">&#34;CREATE TABLE %s(&#34;</span>, tableName)
  <span style="color:#66d9ef">for</span> _, fieldDefinition <span style="color:#66d9ef">in</span> pairs(tableDefinition[<span style="color:#e6db74">&#39;fields&#39;</span>]) <span style="color:#66d9ef">do</span>
    . . .
    . . .
<span style="color:#66d9ef">end</span>
</code></pre></div><p>Using the above, I have moved from being all WET to DRY. I use the same information to generate the tables, the journal tables, and the triggers and so I can be assured that everything is properly in sync. There is one small, remaining item. After I make a change to the field definitions, I need to remember to run all the generators. But that can be handled with proper use of build automation and will have to be left to another blog post!</p>
]]></content>
        </item>
        
    </channel>
</rss>
