<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>0xDEADBEEF</title>
    <description>Hi, I&apos;m Mike. Here, I mainly write about Swift, Go, and random computer musings.  
</description>
    <link>https://www.deadbeef.me/</link>
    <atom:link href="https://www.deadbeef.me/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 04 Jan 2026 02:54:47 +0000</pubDate>
    <lastBuildDate>Sun, 04 Jan 2026 02:54:47 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Material Objects I Enjoyed in 2025</title>
        <description>&lt;p&gt;Sat at a coffee shop in Wichita, Kansas for Thanksgiving and I thought I’d write something that isn’t an email, a Slack message, a line of code, or a comment on a Notion doc for once. So, as a proud consumer of the world, here’s a list of material things that brought me joy this year.&lt;/p&gt;

&lt;h3 id=&quot;small-coffee-cups&quot;&gt;Small Coffee Cups&lt;/h3&gt;

&lt;p&gt;This year, I have been exploring new ways to drink coffee. I wanted it become more of a ritual than a habit. Small cups have been very helpful with this process. They slow things down. They ask you to pay attention to the drug you are ingesting.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;https://www.orea.uk/sense-cup-150ml&quot;&gt;two&lt;/a&gt; &lt;a href=&quot;https://kinto-usa.com/products/17144?variant=22206798463024&quot;&gt;cups&lt;/a&gt; below feel almost like wine glasses. They hold the aroma in a way that invites you in. Before you take the sip, the cup first greets with you a pleasant scent.&lt;/p&gt;

&lt;p&gt;Lately, I’ve been leaning into lighter, funkier, juicier coffees. Turns out they get even tastier as they start to cool. These small cups help with that.&lt;/p&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/A700B15E-C5DC-4B60-884C-36410A699B58_1_105_c.jpeg&quot; alt=&quot;Orea sense cup&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/8010AC60-D4BB-4F8C-82F7-C74D3CD60E2E_1_105_c.jpeg&quot; alt=&quot;Kinto cup&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;Contax T2&lt;/h3&gt;
    &lt;p&gt;This thing is built like a tank. But takes photos that feel delicate and precise.&lt;/p&gt;
    &lt;p&gt;Early this year, I dropped it on concrete while one too many pints in and wrapped the front cover. After a massive bill and a flight to Brooklyn, it&apos;s healthier than ever.. thankfully.&lt;/p&gt;
    &lt;p&gt;Lesson learned that despite the titanium housing and me using the word &quot;tank&quot; to describe it 3 sentences ago, it still is a camera that is decades old and should be handled with maximum respect.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/8673DA83-45D7-4FCB-BD4F-592792BB9916_1_105_c.jpeg&quot; alt=&quot;Contax T2&quot; /&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/CleanShot_2025-11-28_at_11.32.412x.png&quot; alt=&quot;Contax T2 photo&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/6CBCDD78-44E5-4AF8-A1ED-71E5E7FF1323_1_105_c.jpeg&quot; alt=&quot;My glasses&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;My Glasses&lt;/h3&gt;
    &lt;p&gt;I&apos;ve been wearing glasses since elementary school and I&apos;ve genuinely only had two pairs that I LOVED. These are one of them.&lt;/p&gt;
    &lt;p&gt;Made by a Korean brand, named lash | retrospect, they probably remind you of Moscots. But as an Asian, it&apos;s been very hard to find a pair of Moscots that fit me well. So think of these as Moscots, made by folks who have low nose bridges (Asians).&lt;/p&gt;
    &lt;p&gt;They&apos;re sturdy and they almost act like horse blinders. They frame my peripheral vision and make me feel more focused and contained. The world is already too stimulating. These help narrow things just enough so I can stay present.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;1461, Made in England&lt;/h3&gt;
    &lt;p&gt;These boots make me feel ready for the day. I&apos;m on my third pair of &lt;a href=&quot;https://www.drmartens.com/us/en/1461-vintage-made-in-england-oxford-shoes-black/p/12877001&quot;&gt;Docs&lt;/a&gt; - same model - now. This time, I treated myself by getting the &quot;Made in England&quot; version of the boots.&lt;/p&gt;
    &lt;p&gt;Breaking them in was tough. A few blisters. A few slow walks. I&apos;m only a month in so it&apos;s too early to speak in absolutes. But so far, they feel sturdier than my &quot;Made in China&quot; boots.&lt;/p&gt;
    &lt;p&gt;I almost strayed toward a pair of Solovairs, but the moment I put them on, my feet did not like them.&lt;/p&gt;
    &lt;p&gt;For now, I&apos;m sticking with the Docs.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/image.png&quot; alt=&quot;Dr. Martens 1461&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/8196F62F-2E0F-4088-811A-AECFC992DEC6_1_105_c.jpeg&quot; alt=&quot;Coffee scoop&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;The Coffee Scoop&lt;/h3&gt;
    &lt;p&gt;This thing does one thing and it does it well. I use it every morning and the amount of joy &lt;a href=&quot;https://kinto-usa.com/products/27672?variant=22206759665712&quot;&gt;this tiny scoop&lt;/a&gt; from Kinto has brought me is remarkable.&lt;/p&gt;
    &lt;p&gt;It scoops the perfect amount of coffee beans and feels great to hold with two fingers. Its size lets me maneuver it into any coffee bag or storage opening.&lt;/p&gt;
    &lt;p&gt;For me, this was a great reminder that material objects that bring you joy can be as simple as a scoop.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;American Beaujolais&lt;/h3&gt;
    &lt;p&gt;Yes, American Beaujolais is a thing. Made by a semi-controversial(??) winery Las Jaras, &lt;a href=&quot;https://lasjaraswines.com/products/slipper-sippers&quot;&gt;this thing&lt;/a&gt; goes down like juice.&lt;/p&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/image-1.png&quot; alt=&quot;Las Jaras wine&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;A Matter of Concrete&lt;/h3&gt;
    &lt;p&gt;From packaging, to their bean selection, you can&apos;t go wrong with them. &lt;a href=&quot;https://amatterofconcrete.com/product/pinkbourbon/&quot;&gt;Pink bourbon&lt;/a&gt; is their specialty. If you are in SF, you can sometimes find them at &lt;a href=&quot;https://maps.app.goo.gl/CfabnCSEbgHa2LRAA&quot;&gt;The Coffee Movement&lt;/a&gt;.&lt;/p&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/image-2.png&quot; alt=&quot;A Matter of Concrete&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;Glow-in-the-Dark Nalgene Bottle&lt;/h3&gt;
    &lt;p&gt;Hello fellow hydration homies. I discovered that Nalgene makes a &lt;a href=&quot;https://museumstore.sfmoma.org/products/sfmoma-glow-green-bottle?variant=45668272930994&quot;&gt;glow-in-the-dark bottle&lt;/a&gt; a couple years ago, and I&apos;ve never looked back.&lt;/p&gt;
    &lt;p&gt;This thing looks great during the day. But it truly shines - ha - at night. Leave it beside your bed and it acts as a tiny beacon of light that helps you find your way back after using the restroom in the middle of the night.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/image-3.png&quot; alt=&quot;Nalgene bottle&quot; /&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/image-4.png&quot; alt=&quot;Bellroy weekender bag&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;The Weekender Bag&lt;/h3&gt;
    &lt;p&gt;I daily drive &lt;a href=&quot;https://bellroy.com/products/classic-duffel&quot;&gt;this bag&lt;/a&gt; and it fits everything I needs to carry. Sneakers, dirty gym clothes, a protein shake that doesn&apos;t smell great, emergency Vitamin C pills, and a laptop. The only downside is that people keep asking what I&apos;m hauling around in a huge bag.&lt;/p&gt;
    &lt;p&gt;Whenever they do, I give a cheeky &quot;you wouldn&apos;t want to know - winky face&quot; and it always turns into a fun conversation starter.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;column-list&quot;&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;img src=&quot;https://www.deadbeef.me/assets/material-objects-2025/CleanShot_2025-11-28_at_13.47.542x.png&quot; alt=&quot;Merz B. Schwanen t-shirt&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column&quot;&gt;
    &lt;h3&gt;The Perfect White T-shirt&lt;/h3&gt;
    &lt;p&gt;Every guy is probably always looking for the &quot;perfect white t-shirt&quot;. For me, &lt;a href=&quot;https://www.merzbschwanen.com/products/sturdy-jersey-t-shirt-side-seams-6-8oz-sq-yd-cropped-classic-fit-1&quot;&gt;this is it&lt;/a&gt;.&lt;/p&gt;
    &lt;p&gt;Merz B. Shwanen is a brand that became famous thanks to the TV show &quot;The Bear&quot; and I have to admit it, I fell for the hype. But I am glad I did. There, I said it.&lt;/p&gt;
    &lt;p&gt;The German attention to detail and their pride in making physical things well really comes through here.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
</description>
        <pubDate>Fri, 28 Nov 2025 18:00:00 +0000</pubDate>
        <link>https://www.deadbeef.me/2025/11/material-objects</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2025/11/material-objects</guid>
        
        
      </item>
    
      <item>
        <title>Man in the Arena</title>
        <description>&lt;p&gt;Hello, Internet. It’s been a while since I have posted anything here - precisely 1,634 days or 53.68 months - but I am back. A lot has happened during this time; some notable events include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I moved countries twice&lt;/li&gt;
  &lt;li&gt;Worked at 2 of my dream companies&lt;/li&gt;
  &lt;li&gt;Left both of those companies&lt;/li&gt;
  &lt;li&gt;Solved my immigration puzzle, enabling me to finally start a company&lt;/li&gt;
  &lt;li&gt;Joined SPC, created a &lt;a href=&quot;https://www.studiolanes.com&quot;&gt;company&lt;/a&gt;, and started working with my co-founder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And here we are. During these 1,634 days, I’ve encountered many interesting experiences and insights. I’ve decided to resume writing on this blog to share these experiences and thoughts with you, one small piece at a time. If you resonate with or learn something new from these pieces, fantastic. If not, I encourage you to message me on &lt;a href=&quot;https://x.com/guard_if&quot;&gt;X&lt;/a&gt;. Let’s have a pleasant conversation and enlighten each other.&lt;/p&gt;

&lt;p&gt;As a prelude to upcoming posts, here’s a sample of topics and thoughts currently on my mind:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Startups can take many forms. What is the ideal form that I want to create and nurture with a co-founder?&lt;/li&gt;
  &lt;li&gt;If ideas are seeds, what is my preferred approach to produce, nurture, and grow them into self-sustaining &amp;amp; profitable entities?&lt;/li&gt;
  &lt;li&gt;The AI golden age is here. What will living in the future look like? How can I ride the wave of this exciting technology?&lt;/li&gt;
  &lt;li&gt;and more…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before I go, here are some quotes that deeply resonated with me.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;It is not the critic who counts: not the man who points out how the strong man stumbles or where the doer of deeds could have done better. The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood, who strives valiantly, who errs and comes up short again and again, because there is no effort without error or shortcoming, but who knows the great enthusiasms, the great devotions, who spends himself in a worthy cause;&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;Teddy Roosevelt&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Turning something from an idea into a reality can make it seem smaller.
It changes from unearthly to earthly.
The imagination has no limits. The physical world does.
The work exists in both.&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;Rick Rubin&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Curious about what I am working on? Stay up to date &lt;a href=&quot;https://www.studiolanes.com&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Thu, 25 Jan 2024 06:23:11 +0000</pubDate>
        <link>https://www.deadbeef.me/2024/01/man-in-the-arena</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2024/01/man-in-the-arena</guid>
        
        
      </item>
    
      <item>
        <title>Xcode Auto Generated Interfaces</title>
        <description>&lt;p&gt;If you have ever called Objective-C code from Swift, you may have noticed the “Generated Interfaces” feature in Xcode. But have you noticed the details of the said “generated interfaces”?&lt;/p&gt;

&lt;p&gt;For those of you who don’t know what I’m talking about, Xcode has a feature where you can view the automatically generated Swift interfaces of Objective-C files from the editor. This is way, you can see how your Objective-C expressions are parsed into their Swift counterparts, &lt;strong&gt;before compiling anything.&lt;/strong&gt;&lt;/p&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;https://www.deadbeef.me/assets/auto-gen-interface/howto.png&quot; alt=&quot;How to bring up Generated Interfaces&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;look-closely-&quot;&gt;Look closely 👓&lt;/h2&gt;

&lt;p&gt;That’s cool and all but did you notice how objc expressions were exactly parsed into their Swift counterparts? If you didn’t, let me point them out to you.&lt;/p&gt;

&lt;h2 id=&quot;initializers&quot;&gt;Initializers&lt;/h2&gt;

&lt;div class=&quot;language-objc highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instancetype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;initWithName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instancetype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;initName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSString&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instancetype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;initFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;years&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;🔻 Generated Interfaces&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;years&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;initWithName&lt;/code&gt; is automatically shrunk to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init(name:)&lt;/code&gt; to follow Swift’s naming style 😱. This allows you to name your objc functions without worrying about how they are going to look in Swift.&lt;/p&gt;

&lt;p&gt;Also, notice how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;initFor&lt;/code&gt; is separated into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init(for:)&lt;/code&gt;. Cool, eh?&lt;/p&gt;

&lt;h2 id=&quot;functions&quot;&gt;Functions&lt;/h2&gt;

&lt;p&gt;Assuming the above initializers are for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt; class, let’s make our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt; do something by adding some functions.&lt;/p&gt;

&lt;div class=&quot;language-objc highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sleepFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;playAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;location&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;playWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Toy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;toy&lt;/span&gt;                         
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;🔻 Generated Interfaces&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;toy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Toy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, things seem pretty straightforward but once again, notice the details! The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleepFor:&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;playAt:&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;playWith:&lt;/code&gt; have been parsed as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sleep(for:)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;play(at:)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;play(with:)&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;enums&quot;&gt;enums&lt;/h2&gt;

&lt;p&gt;Now that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;s can do something, let’s make some &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Location&lt;/code&gt;s where we can play with our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Animal&lt;/code&gt;s.&lt;/p&gt;

&lt;div class=&quot;language-objc highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NS_ENUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LocationPark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LocationHome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LocationLake&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice how I named the enum cases like how any good objc developer should; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TypeNameCaseName&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That’s cool but Swift favors simplicity and readability when it comes to naming. Thankfully, Xcode automatically takes care of that for you, again.&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Location&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;park&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;home&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lake&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now look at the below Swift code that is calling objc code. Doesn’t it look like you are calling native Swift code?&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;🦄&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Shadowfax&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;fatalError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;neighhhhh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        
&lt;span class=&quot;n&quot;&gt;🦄&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boomerang&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;🦄&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;park&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h2&gt;

&lt;p&gt;Well, I hope you found that to be some what interesting. In the next post, I will maybe look into how this is done behind the scenes 😎&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Here’s a &lt;a href=&quot;https://www.dropbox.com/sh/vjth76ug53mq5fa/AABn5ukKNuVszOXAEugy7mh_a?dl=0&quot;&gt;sample Xcode project&lt;/a&gt; for you to play around with.&lt;/p&gt;
&lt;/blockquote&gt;
</description>
        <pubDate>Wed, 18 Jul 2018 06:23:10 +0000</pubDate>
        <link>https://www.deadbeef.me/2018/07/auto-generated-interfaces</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2018/07/auto-generated-interfaces</guid>
        
        <category>swift</category>
        
        <category>objc</category>
        
        
      </item>
    
      <item>
        <title>Open-source self-diagnostics</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;☠️ Warning: This post may be too much for the faint hearted. Please proceed with caution. Viewer discretion is advised.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Open source is great. You get great software for free and meet great people while doing it. However, like most things, open source has its dark-sides.&lt;/p&gt;

&lt;p&gt;Because the world of open source is purely online and takes place on the fricking internet, it’s very tempting for people to turn into assholes. &lt;em&gt;But please, please, don’t be an asshole.&lt;/em&gt; We have a good thing going on and it would be great if we can keep that going.&lt;/p&gt;

&lt;p&gt;Now, you may be thinking, &lt;em&gt;“Hey asshole, I’m not an asshole. Who are you calling an asshole, asshole?”&lt;/em&gt;. Well, consider this blog post as a self-diagnostic test to find out if you are truly an asshole or not.&lt;/p&gt;

&lt;h2 id=&quot;symptom-1--do-you-use-words-such-as-simply-or-just&quot;&gt;Symptom #1.  Do you use words such as “simply” or “just”?&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;e.g. &lt;strong&gt;&lt;u&gt;Just&lt;/u&gt; integrate X with Y. After that, &lt;u&gt;simply&lt;/u&gt; do X on Y to do Z.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hey asshole, not everyone is a superstar programmer like you. While it may be easy for you, it may be hard for others. Using the &lt;em&gt;j-word&lt;/em&gt; and the &lt;em&gt;s-word&lt;/em&gt; is the easiest way to make other people shitty.&lt;/p&gt;

&lt;h4 id=&quot;prescription-&quot;&gt;Prescription 💊&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Just dont use the &lt;em&gt;j-word&lt;/em&gt; and the &lt;em&gt;s-word&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If that’s too much for you, just point them to a stackoverflow answer, blog post, or a reference material. Let other people be the asshole for you.&lt;/p&gt;

&lt;h2 id=&quot;symptom-2-do-you-give-out-orders&quot;&gt;Symptom #2. Do you give out orders?&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;e.g. &lt;strong&gt;&lt;u&gt;Do&lt;/u&gt; this and that&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;No one likes being ordered to do something. And considering the nature of open source, there is no reason why that person should listen to you; an asshole. It’s a miracle in the first place that these people made their code open to the public for everyone to use. So, if you want them to do something for you, try being nice pretty please.&lt;/p&gt;

&lt;p&gt;You may now be saying &lt;em&gt;“I said those things to help this repo get better.”&lt;/em&gt; That’s very nice of you but you are doing the exact opposite because this is how things usually go down.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;You give out orders &lt;em&gt;(like an asshole)&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Repo maintainer reads your shitty comment&lt;/li&gt;
  &lt;li&gt;Maintainer gets pissed&lt;/li&gt;
  &lt;li&gt;Maintainer gets sad&lt;/li&gt;
  &lt;li&gt;Maintainer tries to keep up with all your shitty comments&lt;/li&gt;
  &lt;li&gt;Maintainer gets burnt out and stops giving any shits&lt;/li&gt;
  &lt;li&gt;All pull requests are ignored&lt;/li&gt;
  &lt;li&gt;Repo goes to shit&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;prescription--1&quot;&gt;Prescription 💊&lt;/h4&gt;

&lt;p&gt;Try to format your shitty comments into less-shittier comments. In other words, &lt;strong&gt;ask nicely.&lt;/strong&gt; Once again, if that’s too hard for you, try using these pre-constructed sentences.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;What do you think about doing ___?&lt;/li&gt;
  &lt;li&gt;How do you feel about doing ___?&lt;/li&gt;
  &lt;li&gt;Have you thought about doing ___?&lt;/li&gt;
  &lt;li&gt;If I were you, I would do ___.&lt;/li&gt;
  &lt;li&gt;That’s a great idea but I would do ___ if I were you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If saying these make you feel weird or uncomfortable on the inside, try using these things called emojis. They make you seem like less of an asshole with minimum effort. If you are new to the whole thing, here are some popular emojis people often use to make their comments seem less shitty.&lt;/p&gt;

&lt;p&gt;🎉 😄 🙏 ❤️  😍 👍 🤔 🙅‍♂&lt;/p&gt;

&lt;h2 id=&quot;symptom-3-do-you-feel-entitled&quot;&gt;Symptom #3. Do you feel entitled?&lt;/h2&gt;

&lt;p&gt;Of course these people should make their code open source. Of course these people should implement the features you request them. Of course they should fix this bug ASAP because they don’t have anything better to do. Right?&lt;/p&gt;

&lt;p&gt;Hate to break it to you but people in the open source community are real people. It may not seem like it, but they are. It’s very likely that these people have jobs, commitments, and other stuff to do. So don’t be surprised if they can’t fix that one bug you found right away or refuse to implement a very specific feature only a small percentage of users would use. That’s why Github has that little “Fork” button at the top right corner.&lt;/p&gt;

&lt;h4 id=&quot;prescription--2&quot;&gt;Prescription 💊&lt;/h4&gt;

&lt;p&gt;This one is hard. All I can tell you is that you aren’t entitled to anything. If you really want something, open up a pull request and ask nicely, pretty please.&lt;/p&gt;

&lt;h2 id=&quot;to-wrap-up&quot;&gt;To wrap up…&lt;/h2&gt;

&lt;p&gt;I just counted and this blog post contains 17 assholes. What I’ve said may have rubbed off in a wrong way for some of you and I apologize. But I felt the need to share this because some people just need the rude awakening; including me.&lt;/p&gt;

&lt;p&gt;It’s been a year I started to really contribute to open source projects and I always catch myself being an asshole. It’s definitely not easy to come off as being asshole online but hey, you can at least try 😄.&lt;/p&gt;

</description>
        <pubDate>Sat, 23 Jun 2018 02:49:23 +0000</pubDate>
        <link>https://www.deadbeef.me/2018/06/oss</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2018/06/oss</guid>
        
        <category>OSS</category>
        
        
      </item>
    
      <item>
        <title>Go test your tests in Go with go test</title>
        <description>&lt;p&gt;Today, we are going to talk about something that is boring… but necessary; tests.&lt;/p&gt;

&lt;p&gt;But you are probably tired of reading blog posts that talk about &lt;em&gt;“Why testing is important”, “Why your thing should have tests”, and “Top 5 reasons you should adopt test-driven development”&lt;/em&gt;. So, instead of writing about the usual “You need tests because it will blah your blah with blah because of blah”, I want to talk about something else; &lt;strong&gt;how to make writing tests not suck as much.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are some tips and tricks I picked up while writing tests for &lt;a href=&quot;https://github.com/mkchoi212/fac&quot;&gt;fac&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;1-current-testing-directory&quot;&gt;1. Current Testing Directory&lt;/h2&gt;

&lt;p&gt;Here is a fun fact; when you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go test&lt;/code&gt; for a package, the test binary will be run within the package being tested.&lt;/p&gt;

&lt;p&gt;Allow me to demonstrate. Assume your project directory is setup like this&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;project
└── server
    ├── server.go
    └── server_test.go
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server_test.go&lt;/code&gt; looks like this&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
	&lt;span class=&quot;s&quot;&gt;&quot;os&quot;&lt;/span&gt;
	&lt;span class=&quot;s&quot;&gt;&quot;testing&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCoolServerStuff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;wd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Getwd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you run this from your root &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;project&lt;/code&gt; directory, you will get the following output&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;➜ &lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;
/Users/mikechoi/src/go/src/github.com/mkchoi212/project

➜ go &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; RUN   TestCoolServerStuff
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; PASS: TestCoolServerStuff &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0.00s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
          server_test.go:10: github.com/mkchoi212/project/server
PASS
ok        github.com/mkchoi212/project/server        0.007s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that the log says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;github.com/mkchoi212/project/server&lt;/code&gt; and not whatever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pwd&lt;/code&gt; said.&lt;/p&gt;

&lt;h2 id=&quot;2-testdata-is-ignored&quot;&gt;2. testdata is ignored&lt;/h2&gt;

&lt;p&gt;According to &lt;a href=&quot;https://golang.org/cmd/go/#hdr-Package_lists&quot;&gt;this Go documentation&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Directory and file names that begin with “.” or “_” are ignored by the go tool, as are directories named “testdata”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, if you need mock files to test something - such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;response.json&lt;/code&gt; for your networking functions - you can place it in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package_directory/testdata/response.json&lt;/code&gt;. Now, you can easily load the mock files like so&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestServerResponse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;testdata/response.json&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;doStuffWithNetworkResponse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;🎉&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that we are able to simple say &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;testdata/response.json&lt;/code&gt; because of #1.&lt;/p&gt;

&lt;h2 id=&quot;3-testhelper&quot;&gt;3. testhelper&lt;/h2&gt;

&lt;p&gt;This is a simple concept but is very helpful when writing tests in Go. When writing code in Go, you probably have written thousands of lines of code that look like this&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doSomethingCool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;doMoreCoolThingsHere&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The trouble is that this is what 80% of statements you are going to tests are going to look like. After all, you are testing if something either didn’t return an error, is equal to something, and or didn’t crap out. Eventually, your testsuite will get filled with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if data, err := ....; err != nil&lt;/code&gt; statements. So, the brilliant solution to this is &lt;strong&gt;to make helper functions&lt;/strong&gt;.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/mkchoi212/730f09c9a2676803d34138da6d923db0.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Instead of writing the code you saw previously with bunch of if-statements, you can now write the following.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCoolThing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doSomethingCool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;// Awesome&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;testhelper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;otherData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doSomethingAmazing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;testhelper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;// This is really useful as well&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;testhelper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;otherData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;	
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And if a test fails for some reason, the helper functions will print out the error messages in red and pin-point the location where the test failed.&lt;/p&gt;

&lt;h2 id=&quot;3-go-test-tricks&quot;&gt;3. go test tricks&lt;/h2&gt;

&lt;p&gt;Here are some additional tips and tricks you can use while using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go test&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;test-all-packages-in-project-directory&quot;&gt;Test all packages in project directory&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./...&lt;/code&gt; is the magic argument.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;➜ go &lt;span class=&quot;nb&quot;&gt;test
&lt;/span&gt;ok      github.com/mkchoi212/project              &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;no &lt;span class=&quot;nb&quot;&gt;test &lt;/span&gt;files]

➜ go &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; ./...
ok      github.com/mkchoi212/project              &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;no &lt;span class=&quot;nb&quot;&gt;test &lt;/span&gt;files]
ok      github.com/mkchoi212/project/server       0.012s
ok      github.com/mkchoi212/project/testhelper   &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;no &lt;span class=&quot;nb&quot;&gt;test &lt;/span&gt;files]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;ignore-package-while-testing&quot;&gt;Ignore package while testing&lt;/h4&gt;

&lt;p&gt;You might need this when you don’t necessarily need to test something or because you just want a higher code coverage percentage 😄&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Notice &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;testhelper&lt;/code&gt; is not being tested any more&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;➜ go &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;go list ./... | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;testhelper&apos;&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
ok      github.com/mkchoi212/project              &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;no &lt;span class=&quot;nb&quot;&gt;test &lt;/span&gt;files]
ok      github.com/mkchoi212/project/server       0.012s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;code-coverage-statistics&quot;&gt;Code coverage statistics&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;➜ go &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--cover&lt;/span&gt;
PASS
coverage: 100.0% of statements
ok      github.com/mkchoi212/project/server  0.011s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Mon, 28 May 2018 01:17:11 +0000</pubDate>
        <link>https://www.deadbeef.me/2018/05/go-test</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2018/05/go-test</guid>
        
        <category>go</category>
        
        <category>testing</category>
        
        
      </item>
    
      <item>
        <title>Common Gotchas in Go</title>
        <description>&lt;p&gt;First thing is first. Happy New Years 🎉🎉&lt;/p&gt;

&lt;p&gt;Now that’s out of the way, let’s talk about Go. I recently finished making my first real Go program. It’s called “Fix All Conflicts” or &lt;a href=&quot;https://github.com/mkchoi212/fac&quot;&gt;fac&lt;/a&gt; for short. It’s an easy-to-use console user interface for fixing git merge conflicts. I made it because I never found a merge tool that was intuitive enough to use.&lt;/p&gt;

&lt;p&gt;The process was quite fun and I learned a lot about Go in the process. So, to wrap up my first official foray into Rob Pike’s mystical land of gophers, I decided to write down some of the common “Gotchas!” that any beginning Gopher - me - can run into.&lt;/p&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;http://www.confusedcoders.com/wp-content/uploads/2016/10/golang-1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Gophers can be quite aggressive sometimes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1 id=&quot;️-1-range&quot;&gt;⚠️ 1.) Range&lt;/h1&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range&lt;/code&gt; function is one of the most commonly used functions in Go. Here’s a sample use case of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range&lt;/code&gt; function. Note that for some demented reason, we decided to make all the animals in the zoo have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;999&lt;/code&gt; legs.&lt;/p&gt;
&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fmt&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Animal&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;legs&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;zoo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Dog&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Chicken&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Snail&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-&amp;gt; Before update %v&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zoo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;animal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zoo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;c&quot;&gt;// 🚨 Oppps! `animal` is a copy of an element 😧&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;999&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-&amp;gt; After update %v&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zoo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The above code looks innocent enough. However, you may be surprised to find that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animals.legs = 999&lt;/code&gt; didn’t do anything.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Before update [{Dog 4} {Chicken 2} {Snail 0}]
-&amp;gt; After update  [{Dog 4} {Chicken 2} {Snail 0}] 🚨🚨🚨 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;lesson&quot;&gt;Lesson&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Value property of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range&lt;/code&gt; (stored here as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animal&lt;/code&gt;) is a &lt;strong&gt;copy of the value from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo&lt;/code&gt;, not a pointer to the value in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zoo&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;the-fix&quot;&gt;The Fix&lt;/h3&gt;

&lt;p&gt;In order to modify an element within the array, we must change the element via its &lt;strong&gt;pointer&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zoo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;zoo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;999&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This may look quite trivial but you may be surprised to find this as a one of the most common source of bugs; at least for me!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://play.golang.org/p/jhL_MNbXnPC&quot;&gt;» Go playground #1 for you to play around in&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;️-2-the--thingy&quot;&gt;⚠️ 2.) The … thingy&lt;/h1&gt;

&lt;p&gt;You may have used the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;…&lt;/code&gt; keyword in the C programming language to create a &lt;a href=&quot;https://www.gnu.org/software/libc/manual/html_node/Variadic-Functions.html&quot;&gt;variadic function&lt;/a&gt;; variadic function is a function that takes a variable number or type of arguments.&lt;/p&gt;

&lt;p&gt;In C, you have to successively call the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;va_arg&lt;/code&gt; macro in order to access the optional arguments. And if you use the variadic argument in any other way, the compiler will throw an error.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add_em_up&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,...)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;va_start&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;         &lt;span class=&quot;cm&quot;&gt;/* Initialize the argument list */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;va_arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;cm&quot;&gt;/* Get the next argument value */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;va_end&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                  &lt;span class=&quot;cm&quot;&gt;/* Clean up */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Go however, things are similar but quite different at the same time. Here is a variadic function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myFprint&lt;/code&gt; in Go. Notice how the variadic argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; is being used.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myFprint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;c&quot;&gt;// ⚠️ `a` should be `a...`&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;c&quot;&gt;// ✅&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;myFprint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s : line %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;file.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;49&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[file.txt %!s(int=49)] : line %!d(MISSING)
file.txt : line 49
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You’d think that the compiler would throw an error here for using the variadic parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; in a wrong way. But notice how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fmt.Sprintf&lt;/code&gt; just used the first argument in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a &lt;/code&gt; without throwing a fit.&lt;/p&gt;

&lt;h3 id=&quot;lesson-1&quot;&gt;Lesson&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;In Go, &lt;strong&gt;variadic parameters are converted to slices by the compiler&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that the variadic argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; is in fact, just a slice. Because of this, the code below is completely valid.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;// `a` is just a slice!&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;elem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;the-fix-1&quot;&gt;The Fix&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Remember to type ALL THREE DOTS whenever using variadic parameters!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href=&quot;https://play.golang.org/p/303g8_1IVFD&quot;&gt;» Go playground #2 for you to play around in&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;️-3-slicing&quot;&gt;⚠️ 3.) Slicing&lt;/h1&gt;
&lt;p&gt;If you have done your fair share of slicing in Python, you may remember that slicing in Python gives you a new list with just the references to the elements copied over. This property allows for code like this in Python.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;			&lt;span class=&quot;c1&quot;&gt;# 👀 a completely new list!
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;999&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However if you try the same thing in Go, you get something else.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;slice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;999&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[999 2 3]
[999 2]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;lesson-2&quot;&gt;Lesson&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;In Go, &lt;strong&gt;a slice shares the same backing array and capacity as the original.&lt;/strong&gt; So if you change an element in the slice, the original contents are modified as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;the-fix-2&quot;&gt;The Fix&lt;/h4&gt;

&lt;p&gt;If you want to get an independent slice, you have two options.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;// Option #1&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;// appending elements to a nil slice&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;// `...` changes slice to arguments for the variadic function `append`&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// Option #1&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;// Create slice with length of 2&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;// copy(dest, src)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And according to &lt;a href=&quot;https://stackoverflow.com/a/44337887/4064189&quot;&gt;StackOverflow&lt;/a&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;append&lt;/code&gt; option is slightly faster than the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make. + copy&lt;/code&gt; option!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://play.golang.org/p/HvVFmQZTcjp&quot;&gt;» Go playground #3 for you to play around in&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://news.ycombinator.com/item?id=16048206&quot;&gt;Hacker News thread 🤩&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 01 Jan 2018 06:55:03 +0000</pubDate>
        <link>https://www.deadbeef.me/2018/01/go-gotchas</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2018/01/go-gotchas</guid>
        
        <category>go</category>
        
        
      </item>
    
      <item>
        <title>Things we can learn from Microsoft</title>
        <description>&lt;p&gt;Let me tell you something about me. I was &lt;a href=&quot;https://deadbeef.me/2017/12/rss&quot;&gt;obsessed with HackerNews&lt;/a&gt; for about three months. Not a single day went by where I didn’t check the front-page.&lt;/p&gt;

&lt;p&gt;Anyways, during those three months, hundreds of articles went past my fingers. I admit I don’t remember all of them but there were few articles that lingered in the back of my head. And surprisingly, a big chunk of those articles had to do with Microsoft during its early days. So, this is me writing them down to see what I can learn from them.&lt;/p&gt;

&lt;p&gt;So… here are &lt;strong&gt;three lessons we can all learn from Microsoft during the 1980’s&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;lesson-1-optimize-the-most-common-operations&quot;&gt;Lesson #1. Optimize the most common operations&lt;/h2&gt;

&lt;p&gt;Before you quote Don Knuth, here is a story of how Microsoft Word became the most popular word processor in the world.&lt;/p&gt;

&lt;p&gt;In 1983, PC Word 1.0 and Mac Word 1.0 shipped with a feature called “Piece Table”. The piece table wasn’t an official feature that was printed on the floppy disk but it was the data structure that enabled many of the listed features.&lt;/p&gt;

&lt;p&gt;For example, it allowed for super fast copy and paste, undo and redo operations. You can go &lt;a href=&quot;https://web.archive.org/web/20160308183811/http://1017.songtrellisopml.com/whatsbeenwroughtusingpiecetables&quot;&gt;here&lt;/a&gt; to see how they were actually implemented but here is the TL;DR version of it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Instead of storing text in the document as a single long string of characters, one can maintain a small set of records; a piece table. This table can then hold a collection of data that is only a few bytes long - a piece - that describes how a string is fragmented into pieces.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Although this sounds quite mundane now, super fast undo/redo and paste operations were like black magic to people in the 80’s. This is because back then, you had to go grab a cup of coffee and take a stroll around the park in your Reeboks while waiting for your word processor to finish copying and pasting a paragraph from the “American Psycho”. Nevertheless, people loved these features they allowed them to write without being interrupted and therefore save countless hours.&lt;/p&gt;

&lt;p&gt;Later when Word 3.0 launched, Microsoft revolutionized the word processing industry once again with a feature called fast save. I won’t describe how they managed to implement &lt;a href=&quot;https://web.archive.org/web/20160308183811/http://1017.songtrellisopml.com/whatsbeenwroughtusingpiecetables&quot;&gt;fast save&lt;/a&gt; but I think that this feature is the best thing Microsoft has ever shipped… ever.&lt;/p&gt;

&lt;h2 id=&quot;lesson-2-make-it-backwards-compatible&quot;&gt;Lesson #2. Make it backwards compatible&lt;/h2&gt;

&lt;p&gt;Microsoft has shipped many versions of their Windows operating system but here’s a lesson we can all learn from them.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;It takes only one program to sour an upgrade.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, allow me to elaborate.&lt;/p&gt;

&lt;p&gt;Let’s say you are an IT manager of a company and you find out that if you upgrade to a new fancy version of the OS, program X the entire company uses won’t work anymore. You really want that one feature that comes with the new OS so you desperately make the call to the company that makes program X to request for an upgrade. But it turns out, the upgrade won’t be free. At this point, would you upgrade to the new OS?&lt;/p&gt;

&lt;p&gt;Microsoft’s Setup/Upgrade team figured out that every single user had a “deal-breaker” program, a program that must run or they won’t upgrade. This is why Microsoft Windows has that one mysterious directory &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C:\WINDOWS\AppPatch&lt;/code&gt; that stores all the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.dll&lt;/code&gt;s to support application backwards compatibility; this is why you can still play Sims 1 on your computer.&lt;/p&gt;

&lt;h2 id=&quot;lesson-3-abc-always-be-considerate&quot;&gt;Lesson #3. A.B.C. Always Be Considerate&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;https://media.giphy.com/media/nAZ3JTRUYiis0/giphy.gif&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Excuse my poor attempt to reference Glengarry Glen Ross but the message still stands; Always Be Considerate! Unlike Alec Baldwin here, I am not saying you should be considerate to the people you are trying to sell houses to. Instead, you should be considerate to the end users of your software.&lt;/p&gt;

&lt;p&gt;When Microsoft wanted to make a scripting language for Excel users, they started a project called Visual Basic for Applications. In the beginning, the developers had to decide whether if they wanted the language to be &lt;em&gt;statically (strong) typed&lt;/em&gt; or &lt;em&gt;dynamically (weak) typed&lt;/em&gt;. Personally, I am in the statically typed languages camp as I love using Swift and Golang. These &lt;a href=&quot;http://blog.cleancoder.com/uncle-bob/2017/01/11/TheDarkPath.html&quot;&gt;two&lt;/a&gt; &lt;a href=&quot;http://elbenshira.com/blog/the-end-of-dynamic-languages/&quot;&gt;posts&lt;/a&gt; do a better job than me describing each side of the argument so I highly recommend you to read those when you have the time.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;TL;DR The main gist of the argument is that statically typed languages allow you to find errors at compile time while dynamically typed languages allow you to find errors during runtime.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We could go on for days arguing over which one is better than the other and keep furiously typing away on our keyboards until our butterfly mechanism keys break. &lt;strong&gt;However, the end-user, who is most likely to be an accountant at a paper company like Dunder Mifflin, won’t care if it’s statically typed or dynamically typed.&lt;/strong&gt; What that accountant cares about the most is whether if he can easily whip up a script for Excel that will let him automate repetitive tasks so he can go home to his kids a bit sooner.&lt;/p&gt;

&lt;p&gt;If you decide to make the language statically typed, the intern that is in charge of writing the manual will have to describe to the accountant what a variable is and most importantly, what types are and why they are so important. Or you could make the language dynamically typed and let the accountant just start coding away immediately. Yes, the accountant will get runtime errors but he probably isn’t writing a program for the Apollo 11 lunar module. All the accountant wants to do is add up column A through Z.&lt;/p&gt;

&lt;p&gt;You may have strong beliefs about a certain decision that has to be made in a project. However, are you being considerate of the user?&lt;/p&gt;
</description>
        <pubDate>Sat, 23 Dec 2017 06:22:11 +0000</pubDate>
        <link>https://www.deadbeef.me/2017/12/learn-from-microsoft</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2017/12/learn-from-microsoft</guid>
        
        <category>type-system</category>
        
        <category>algorithms</category>
        
        
      </item>
    
      <item>
        <title>Taming the Feed</title>
        <description>&lt;p&gt;I don’t know about you or what other cool cats are doing nowadays, but I use RSS (Really Simple Syndication) to suck in all the content from the Internet. To get specific, I use &lt;a href=&quot;http://reederapp.com/mac/&quot;&gt;Reeder&lt;/a&gt; as my primary RSS application and &lt;a href=&quot;https://www.instapaper.com&quot;&gt;Instapaper&lt;/a&gt; as my goto save-for-later bucket.&lt;/p&gt;

&lt;p&gt;During the summer of 2016, I became obsessed with HackerNews. In the beginning, it was nice because I was reading and absorbing anything and everything interesting that was going on the Internet. I was learning something new everyday and was becoming a better developer. In addition, I was up-to-date with the latest gossips and practically heard of every “new cool language/framework/testing tool”. However, this came with a price. I was checking HackerNews about 10-15 times a day. No article went by me unread. Every single article had that faint light gray color as they went past my fingers of fury. At a certain point, it seemed like I was reading more than I was coding.&lt;/p&gt;

&lt;p&gt;Then school started again.&lt;/p&gt;

&lt;p&gt;What used to be a morning ritual of checking HackerNews became impossible. If I was lucky, I got to check the feed once or twice a day. Even then, I would only be able to read one or two articles and save the rest into Instapaper. And eventually, Instapaper became a place where essays and blog posts go to die and rot in a pile of their own digital waste.&lt;/p&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;https://www.deadbeef.me/assets/blogging/insta.jpeg&quot; alt=&quot;My Insta&quot; /&gt;&lt;/p&gt;

&lt;p&gt;RSS was even worse. I had subscribed to about 30+ RSS sources and was getting up to 60 new articles per day. After about a week into school, I didn’t even try to catch up on these. In fact, Reeder ended up losing its spot on my Dock.&lt;/p&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;https://www.deadbeef.me/assets/blogging/rss.png&quot; alt=&quot;Oh god..&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I bet 5000 unread articles is some sort of new record. I also bet 5000 is the highest number of unread articles Feedly will let you have.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s Winter Break now and it’s about 55 degrees in Texas. I’m currently sitting at a cafe drinking a latte that is way too sweet and trying to fix the mess I have found myself in. To do this, I wrote myself a little prescription. If you are experiencing the same symptoms as I am, I recommend you do the same.&lt;/p&gt;

&lt;h2 id=&quot;rules-of-engagement-with-articles-from-the-interwebhackernews&quot;&gt;Rules of Engagement with articles from the Interweb/HackerNews&lt;/h2&gt;

&lt;h3 id=&quot;1-read-at-least-5-articles-from-my-saved-instapaper-feed-every-day&quot;&gt;&lt;strong&gt;1. Read at least 5 articles from my saved Instapaper feed every day&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;DO NOT ADD NEW ARTICLES UNTIL THE RED BUBBLE THINGY REACHES ZERO!&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;2-limit-reading-hackernews-to-only-twice-a-day&quot;&gt;&lt;strong&gt;2. Limit reading HackerNews to only TWICE a day&lt;/strong&gt;&lt;/h3&gt;

&lt;h3 id=&quot;3-only-keep-4-5-most-interesting-subscriptions-in-reeders-rss-feed&quot;&gt;&lt;strong&gt;3. Only keep 4-5 most interesting subscriptions in Reeder’s RSS feed&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;I know there are lots of interesting articles out there but you will never get to all of them.&lt;/p&gt;
</description>
        <pubDate>Fri, 15 Dec 2017 03:14:13 +0000</pubDate>
        <link>https://www.deadbeef.me/2017/12/rss</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2017/12/rss</guid>
        
        <category>blogging</category>
        
        
      </item>
    
      <item>
        <title>Monoids deployed</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;First of all, an explanation of the title is in order. I used the word “deployed” just because it kind of rhymed with “Monoid”. It’s probably a better choice than what I was originally going to use; “Monoids on Steroids”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Anyways, this post will be an attempt to explain what monoids are, why they are useful and most importantly, how they can be used. We will also build a simple shapes library in Haskell and Swift as an example.&lt;/p&gt;

&lt;p&gt;To start off with, here is an example of a monoid. Ready?&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1 + 0 = 1
0 + 1 = 1
1 + (2 + 3) = 6
(1 + 2) + 3 = 6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you understand these highly complex mathematical equations, you understand what monoids are. Congratulations 🎉🎉🎉.&lt;/p&gt;

&lt;h1 id=&quot;formal-definition&quot;&gt;Formal Definition&lt;/h1&gt;

&lt;h2 id=&quot;mathematical&quot;&gt;Mathematical&lt;/h2&gt;

&lt;p&gt;A monoid is a tuple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(M, op, e)&lt;/code&gt; where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;M is a set of element&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;op&lt;/code&gt; is an associative binary operation on two elements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M&lt;/code&gt;, returning a new element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; is an element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M&lt;/code&gt;, neutral for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;op&lt;/code&gt; on both left and right side&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So for example, the above addition operator would form the following tuple of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Int, +, 0)&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;statically-typed-languages&quot;&gt;Statically typed languages&lt;/h2&gt;

&lt;p&gt;Since we are programmers, let us translate the above definition to code. A monoid consists of a type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; and a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; where&lt;/p&gt;

&lt;h2 id=&quot;f-takes-an-instance-of-t-and-returns-an-instance-of-t&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; takes an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; and returns an instance of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func foobar(_ in: String, _in2: String) -&amp;gt; String&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above statement says that a binary operation of monoids takes two elements of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt; and will always return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;. The input type is same as the output type. This property is very important as it will &lt;strong&gt;allows us to chain operations indefinitely.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;f-is-associative&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; is associative&lt;/h2&gt;

&lt;p&gt;Associative property is when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(c, f(a, b)) == f(f(c, a), b)&lt;/code&gt; holds true.&lt;/p&gt;

&lt;p&gt;In other words, we are looking for the situation where something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 + (2 + 3) == (1 + 2) + 3&lt;/code&gt; makes sense. This allows us to not worry about the ordering when we compose functions. We will see examples of this later.&lt;/p&gt;

&lt;h2 id=&quot;f-has-a-neutral-element-e-where-fe-a--fa-e&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; has a neutral element &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(e, a) == f(a, e)&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;We are looking for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; that allows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foobar(NEUTRAL_ELEMENT, 5) == foobar(5, NEUTRAL_ELEMENT)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; is the neutral element for the addition operator since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0 + 3 == 3 + 0&lt;/code&gt;&lt;/p&gt;

&lt;h1 id=&quot;monoids-everywhere&quot;&gt;Monoids everywhere&lt;/h1&gt;

&lt;p&gt;By now, you hopefully have a sense of what monoids are. And the funny thing is, you probably have been using monoids all along without evening knowing it. Here are some examples of commonly used monoids that are unfortuantley not acknowledged as monoids most of the time.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Integers under addition with neutral element zero&lt;/li&gt;
  &lt;li&gt;Integers under multiplication with neutral element one&lt;/li&gt;
  &lt;li&gt;Sequential containers under concatenation (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;Associative containers under union (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;but-why-should-i-care&quot;&gt;But why should I care?&lt;/h1&gt;

&lt;p&gt;Th idea of monoids come from the realms of functional programming where the ultimate goal is to come up with simple abstractions that can later be composed to create complex behaviors. &lt;strong&gt;Monoids are a great representation of the FP mindset as it gives us a way to build complexity out of simplicity.&lt;/strong&gt; This means that before you dive into a problem, you can start by formulating a very simple idea. Once you solidify this idea, you can then use it to build complex objects that will eventually solve your problem.&lt;/p&gt;

&lt;h1 id=&quot;ascii-art-generator&quot;&gt;ASCII Art Generator&lt;/h1&gt;

&lt;p&gt;To see how monoids can be used, we will create a library with power enough abstractions that will allows us to create some interesting ASCII art. Our library will mainly deal with creating geometric shapes.&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; As we go through the code, I will demonstrate each example in &lt;em&gt;Haskell and then follow up with Swift.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;abstracting-a-shape&quot;&gt;Abstracting a “Shape”&lt;/h2&gt;

&lt;p&gt;Here, we will &lt;strong&gt;define a shape as a function that takes a coordinate point as an input and returns a boolean as an output&lt;/strong&gt;. The idea is simple; you ask the function if the point &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(x, y)&lt;/code&gt; is contained in the shape and, if  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; is returned, we then know it’s part of the shape.&lt;/p&gt;

&lt;p&gt;To define a shape, we will make a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shape&lt;/code&gt; type with the function - &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isInShape&lt;/code&gt; - I just described above. We will also define some typealiases to make life easier. Notice that we are using a generic struct. This is so that we can use not only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Coord2D&lt;/code&gt; but also &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Coord3D&lt;/code&gt;,  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Coord4D&lt;/code&gt; in the future.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;	  
  &lt;span class=&quot;n&quot;&gt;isInShape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt;	
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;	
&lt;span class=&quot;kr&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape2D&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;isInShape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;typealias&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;typealias&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape2D&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because we are describing shapes as functions, we can easily define the complement of a shape. For example, we can make the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;outside&lt;/code&gt;. All it would have to do is negate the predicate of the input shape.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;outside&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;outside&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isInShape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Thanks to u/thisischemistry, code is now more understandable and self-documenting&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;outside&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;notInOriginalShape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isInShape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;isInShape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notInOriginalShape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;creating-shapes&quot;&gt;Creating Shapes&lt;/h2&gt;

&lt;p&gt;To define a shape, we need to specify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isInShape&lt;/code&gt; function to describe whether a point belongs in the shape or not. Here’s how we would define a disk.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;disk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Radius&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape2D&lt;/span&gt;	
&lt;span class=&quot;n&quot;&gt;disk&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
	&lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;euclidianDistance&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;isInShape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;guard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as?&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fatalError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Center must be a 2D point&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;euclidianDistance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, we can experiment and have some fun with some sample coordinates&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;disk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;8.00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;disk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isInShape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;13.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;13.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And with a simple loop, we can print the disk out. The output looks a bit off because the line widths are larger than the character spacings. But hey, still pretty cool, eh?&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;      *******                                     
     ***********                                   
    *************                                  
    *************                                  
   ***************                                 
   ***************                                 
   ***************                                 
   ***************                                 
   ***************                                 
   ***************                                 
   ***************                                 
    *************                                  
    *************                                  
     ***********                                   
       *******                                     
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;https://media.giphy.com/media/6pSJGZW0ZmbUA/giphy.gif&quot; alt=&quot;Awesome!&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;complex-shapes&quot;&gt;Complex shapes&lt;/h2&gt;

&lt;p&gt;Now that we have our basic idea down, let’s stat complicating things. And since we are in the realm of FP, what would this post be without some composition!? Let’s try to build cooler shapes via composition.&lt;/p&gt;

&lt;p&gt;We can first start by defining the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;intersect&lt;/code&gt; function which defines a new shape within the region of intersection.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;intersect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;	
&lt;span class=&quot;n&quot;&gt;intersect&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;	  
	&lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isInShape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isInShape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;intersect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isInShape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isInShape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;disk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;8.00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Coord2D&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ring&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;disk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;intersect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;outside&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Printing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ring&lt;/code&gt; yields the below result&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;      ********                                     
     ***********                                   
    *************                                  
    *************                                  
   ****       ****                                 
   ****       ****                                 
   ****       ****                                 
   ****       ****                                 
   ****       ****                                 
   ****       ****                                 
   ****       ****                                 
    *************                                  
    *************                                  
     ***********                                   
       *******      
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Kind of looks like those rings from Sonic, eh?&lt;/p&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;https://media.giphy.com/media/2mmYAQ23kZ3XO/giphy.gif&quot; alt=&quot;S&apos;cute!&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h1&gt;

&lt;p&gt;I hope the shapes example demonstrated how great monoids are when it comes to composition and abstraction. Personally, I have constantly failed to recognize the fact that I have been using something people have discovered, named and implemented. I try to recognize them and utilize them when I code but it can be very hard to do so.&lt;/p&gt;

&lt;p&gt;Monoids are a great example of this. It’s something functional programmers use all the time but is something that never gets called out explicitly in imperative programming languages. I think this is a shame because once you recognize the fact that something is a monoid, you get access to some incredible perspectives from where you can look at your code.&lt;/p&gt;

&lt;p&gt;Anyways, the one thing I wish you got away from this post is that after recognizing a good use case for a monoid, you can start with something really simple. Then, as the logic grows, you can build something very complex on top of a very firm foundation.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;http://conal.net&quot;&gt;Conal Elliott&lt;/a&gt; &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sat, 23 Sep 2017 04:34:45 +0000</pubDate>
        <link>https://www.deadbeef.me/2017/09/monoids</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2017/09/monoids</guid>
        
        <category>haskell</category>
        
        <category>swift</category>
        
        <category>fp</category>
        
        
      </item>
    
      <item>
        <title>Reentrant and Threadsafe Code</title>
        <description>&lt;p&gt;If you have programmed in C, you probably have typed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man strtok&lt;/code&gt; on the terminal. Ah yes, the infmaous &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok&lt;/code&gt; function. The internet seems to really hate it but you decide to give the function a chance and try to read through the man page. Just in case none of what I said applies to you, here’s the first sentence in the description section of the man page.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;This interface is obsoleted by&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strsep(3)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p class=&quot;center&quot;&gt;&lt;img src=&quot;https://media.giphy.com/media/3o7TKQ8kAP0f9X5PoY/giphy.gif&quot; alt=&quot;Well then...&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Uh ok… Even the folks at FreeBSD seem to hate it so much that they just completely replaced it with a different function. But you keep reading to see what’s so bad about it and you come across the following sentence.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok_r()&lt;/code&gt; function is a reentrant version of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok()&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point, you could asking &lt;em&gt;“what does it mean by a function to be reentrant?”&lt;/em&gt;. If you know the answer to this question, you should stop reading this post and move on to a &lt;a href=&quot;https://www.deadbeef.me/2017/08/contributing-to-swift&quot;&gt;different&lt;/a&gt; post.&lt;/p&gt;

&lt;p&gt;For those who don’t know what reentrant functions are, this is how the man page describes it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The context pointer last must be provided on each call.  The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok_r()&lt;/code&gt; function may also be used to nest two parsing loops within one another, as long as separate context pointers are used.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Does it mean that the reentrant function is some how better than the non-reentrant version because it can be used to nest two parsing loops? Well, before we can answer that question, we first need to know what makes a function reentrant than its non-reentrant counterparts.&lt;/p&gt;

&lt;h1 id=&quot;threadsafe-code&quot;&gt;Threadsafe Code&lt;/h1&gt;

&lt;p&gt;From the word &lt;em&gt;“reentrant”&lt;/em&gt;, you can kind of guess that it is probably going to be some piece of code that is going to be &lt;em&gt;re-entered&lt;/em&gt;. And you would be right. Reentrant code is mostly used in multithreaded programs to maintain its integrity and keep programmers from going insane from mysterious race conditions and pure hell. And now, you could asking &lt;em&gt;“Hey! Don’t we already have a word for that? That kind of code is called threadsafe!”&lt;/em&gt; And , you would be right, once again. &lt;strong&gt;So what’s the fricking difference???&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;reentrant-code-vs-threadsafe-code&quot;&gt;Reentrant Code VS. Threadsafe Code&lt;/h2&gt;

&lt;p&gt;Let’s do some definitions.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Thread safe code is one that can be &lt;strong&gt;performed from multiple threads safely&lt;/strong&gt;, even if the calls happen &lt;strong&gt;simultaneously&lt;/strong&gt; on multiple threads.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Reentrant code is one that &lt;strong&gt;can be entered by another actor before an earlier invocation has finished&lt;/strong&gt;, without affecting the path the first action would have taken through the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Did you catch the difference? Thread safe code means you can call the function on multiple threads. Reentrant code means that you can do all the things thread safe code can do but also gurantee safety &lt;strong&gt;even if you call the same function within the same thread.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, reentrant code can be thread safe but thread safe code can’t be reentrant? Not necessarily… Before we complicate things, let’s go and look at some code.&lt;/p&gt;

&lt;h1 id=&quot;show-me-the-code&quot;&gt;Show me the code&lt;/h1&gt;

&lt;p&gt;Just like how most books do it, we will first start off with bad code. Then, we will slowly make it better, worse again, and eventually make it immune to any convoluted multithreaded code.&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;h2 id=&quot;reentrant---thread-safe-&quot;&gt;Reentrant ❌ | Thread-safe ❌&lt;/h2&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// `my_func()` could be called here&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;❌  Thread-safe because
    &lt;ul&gt;
      &lt;li&gt;Global variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt; is constantly mutating within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;swap&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;❌  Reentrant because
    &lt;ul&gt;
      &lt;li&gt;Global varaible &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my_func()&lt;/code&gt; could be called while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;swap()&lt;/code&gt; is running in the same context. If so, value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;  would be unpredictable&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This code can easily be made to be thread-safe though. All we need to do is make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt; a thread local variable.&lt;/p&gt;

&lt;h2 id=&quot;reentrant---thread-safe--1&quot;&gt;Reentrant ❌ | Thread-safe ✅&lt;/h2&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;threads.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// `t` is now local to each thread&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;thread_local&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// `my_func()` could be called here&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;✅  Thread-safe because&lt;/li&gt;
  &lt;li&gt;Variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt; is local to each thread&lt;/li&gt;
  &lt;li&gt;❌  Reentrant because&lt;/li&gt;
  &lt;li&gt;Threads are safe from each other but if multiple calls to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my_func()&lt;/code&gt; happen within a single thread, value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt; is still unpredictable&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;reentrant---thread-safe--2&quot;&gt;Reentrant ✅ | Thread-safe ❌&lt;/h2&gt;

&lt;p&gt;No one would do this in real life but for the sake of example, here’s some very convoluted code.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// save global variable&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// `my_func()` could be called here&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// restore global variable&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;❌ Thread-safe because
    &lt;ul&gt;
      &lt;li&gt;Global data can’t be guaranteed to be the same at all times&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;✅ Reentrant because
    &lt;ul&gt;
      &lt;li&gt;Funky but global data is the same when the program enters and or leaves &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;swap&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;reentrant---thread-safe--3&quot;&gt;Reentrant ✅ | Thread-safe ✅&lt;/h2&gt;

&lt;p&gt;The solution was just staring at us from the beginning. That one professor from CPSC 101 was right all along; global variables are bad.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// `my_func()` could be called here&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;swap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;✅ Thread-safe and  ✅ reentrant because&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt; is now allocated on the stack&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;rules-of-thumb&quot;&gt;Rules of Thumb&lt;/h1&gt;

&lt;p&gt;To write a block of code that is reentrant, you should adhere to the following rules of thumb&lt;sup id=&quot;fnref:1:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Don’t use global or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt; variables&lt;/li&gt;
  &lt;li&gt;Don’t let it modify its own code&lt;/li&gt;
  &lt;li&gt;Don’t call other non-entrant functions&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;back-to-strtok&quot;&gt;Back to strtok&lt;/h1&gt;

&lt;p&gt;So, let’s apply what we learned here to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok&lt;/code&gt;. Usually, when we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok&lt;/code&gt;, we do the following&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strtok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;separators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strtok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;separators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is completely reasonable code besides the fact that it is using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok&lt;/code&gt; and is neither reentrant nor thread safe. However, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok_r&lt;/code&gt; can come in and save the day with only minor changes to the code&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strtok_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;separators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strtok_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;separators&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, thanks to the reentrant version of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok&lt;/code&gt;, this code block can now be used in multithreaded programs.&lt;/p&gt;

&lt;h1 id=&quot;so&quot;&gt;So…&lt;/h1&gt;

&lt;p&gt;the next time you are stuck in constant deadlock, data mutating, blood boiling mutiprogramming hell, first ask yourself, &lt;em&gt;“Hey, is this code reentrant and or threadsafe?”&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Reentrancy_(computing)&quot;&gt;Wikipedia&lt;/a&gt; &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt; &lt;a href=&quot;#fnref:1:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.genprogc/writing_reentrant_thread_safe_code.htm#writing_reentrant_thread_safe_code__mfr&quot;&gt;IBM Knowledge Center&lt;/a&gt; &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sun, 17 Sep 2017 04:28:03 +0000</pubDate>
        <link>https://www.deadbeef.me/2017/09/reentrant-threadsafe</link>
        <guid isPermaLink="true">https://www.deadbeef.me/2017/09/reentrant-threadsafe</guid>
        
        <category>c</category>
        
        <category>multithreaded</category>
        
        
      </item>
    
  </channel>
</rss>
