<p>Two common patterns that I seem to run all the time while working on my 4x space game are functor and applicative. This episode explains them briefly.</p>
<h2 id="functor">Functor</h2>
<p>Functor is a way to apply function over a structure we don’t want to alter. Type of the structure stays same, but values inside of it can change. One of the most common one is list, but there are many others.</p>
<p>Functor type class is defined below. There’s one function <code>fmap</code> that takes two parameters: a function from <code>a</code> to <code>b</code> and structure <code>f a</code>. Result will be structure <code>f b</code>.</p>
<pre><code>class Functor f where
    fmap :: (a -&gt; b) -&gt; f a -&gt; f b</code></pre>
<p>This is fairly abstract, so couple example might help. First we define a little helper function that raises it’s argument to 2nd power (in the episode I talk about doubling the value, my mistake there).</p>
<pre><code>-- | this really raises x to 2nd power and doesn&#39;t double it
double x = x * x</code></pre>
<p>Given a list of <code>Int</code> we can raise them to power of two by using <code>fmap</code>:</p>
<pre><code>&gt; fmap double [1, 2, 3, 4, 5]
[1, 4, 9, 16, 25]</code></pre>
<p>Since function being applied to structure is type of <code>(a -&gt; b)</code>, we can change type of the value inside of the structure. Below is example of turning list of <code>Int</code> to list of <code>Text</code>.</p>
<pre><code>&gt; fmap show [1, 2, 3, 4, 5]
[&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;]</code></pre>
<p>This pattern isn’t limited to list and there are many others. You can even define your own ones, if you’re so inclined. The pattern stays the same. One function, <code>fmap</code>, that takes function of type <code>(a -&gt; b)</code> and structure <code>f a</code> and turns it into structure of <code>f b</code>. Details how this is actually done depend on the specific functor.</p>
<p>Other common functor is <code>Maybe</code> that is often used in cases where data might or might not be present. <code>Maybe a</code> has two possible values <code>Just a</code> indicating that value <code>a</code> is present and <code>Nothing</code> indicating that there is no value present. When <code>fmap</code> is used in this context, <code>Just a</code> will turn to <code>Just b</code> and <code>Nothing</code> will stay as <code>Nothing</code>.</p>
<pre><code>&gt; fmap (x -&gt; x * x) $ Just 2
Just 4
&gt; fmap (x -&gt; x * x) Nothing
Nothing</code></pre>
<p><code>Either a b</code> is sometimes used for value that can be correct or an error. It has two value constructors <code>Right b</code> indicates that value is correct, <code>Left a</code> indicates an error case. <code>a</code> and <code>b</code> don’t have to be of same type (and usually aren’t). For example, if we have <code>Either Text Int</code>, then we have value where error case is <code>Text</code> and correct value is <code>Int</code>.</p>
<pre><code>&gt; fmap double $ Right 5
Right 25
&gt; fmap double $ Left &quot;distance calculation failed because of flux-capacitor malfunction&quot;
Left &quot;distance calculation failed because of flux-capacitor malfunction&quot;</code></pre>
<p>Functors can be placed inside of functors. The only difference is that you have to reach through multiple layers. Simplest way of doing that is to compose multiple <code>fmap</code> functions together like in the example below. Pay attention to in which order nested functors are defined as <code>Maybe [Int]</code> and <code>[Maybe Int]</code> are different things. Former is for case where list of <code>Int</code> might or might not be present. Latter is for case where there’s always list, but single element inside of the list might or might not be present.</p>
<pre><code>&gt; (fmap . fmap) double (Just [1, 2, 3, 4])
Just [1, 4, 9, 16]
&gt; (fmap . fmap) double Nothing :: Maybe Int
Nothing
&gt; (fmap . fmap) double [Just 1, Just 2, Nothing, Just 3]
[Just 1, Just 4, Nothing, Just 9]</code></pre>
<p>There’s also infix operator, that does exactly same thing as <code>fmap</code>, called <code>&lt;$&gt;</code>. The choice which one to use is often either personal or depends on the surrounding code (because Haskell doesn’t use parenthesis in function application, so sometimes it’s easier to use <code>fmap</code> and sometimes <code>&lt;$&gt;</code>).</p>
<pre><code>&gt; fmap show [1, 2, 3, 4, 5]
[&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;]

&gt; show &lt;$&gt; [1, 2, 3, 4, 5]
[&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;]</code></pre>
<p>There are many more functors, one place to check them is: <a href="https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Functor.html" class="uri">https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Functor.html</a></p>
<h2 id="applicative">Applicative</h2>
<p>While functor works fine when function applied has only one parameter, we need applicative in cases of multiparameter functions. Calling <code>fmap (+) [1, 2]</code> will produce list of functions waiting for second parameter. While it would be possible to handle these cases manually, we like to abstract it to more general solution.</p>
<pre><code>class Functor f =&gt; Applicative f where
    pure :: a -&gt; f a
    (&lt;*&gt;) :: f (a -&gt; b) -&gt; f a -&gt; f b</code></pre>
<p>Applicative is similar to functor. The big difference is that function being applied is now embedded inside of same type of structure. While functor has <code>(a -&gt; b)</code>, applicative has <code>f (a -&gt; b)</code>.</p>
<p>Below is an example of using list applicative to calculate all possible ways of summing two lists of <code>Int</code>.</p>
<pre><code>&gt; (+) &lt;$&gt; [1, 2, 3] &lt;*&gt; [4, 5, 6]
[5,6,7,6,7,8,7,8,9]</code></pre>
<p><code>Maybe Int</code> works with the same pattern. First we use <code>&lt;$&gt;</code> to get started, this results <code>Maybe</code> containing a function that is waiting for second parameter. Then we use <code>&lt;*&gt;</code> to apply the second parameter so we get the result.</p>
<pre><code>&gt; (+) &lt;$&gt; Just 2 &lt;*&gt; Just 5
Just 7
&gt; (+) &lt;$&gt; Just 2 &lt;*&gt; Nothing
Nothing</code></pre>
<p>As long as there’s only <code>Just a</code> in play, result is <code>Just</code>, but as soon as there’s even single <code>Nothing</code> the end result will be nothing.</p>
<p>If you have questions or comments, I would be delighted to hear about them. You can catch me on fediverse, where I’m <code>Tuula@mastodon.social</code>. Even better, you could record your own HPR episode.</p>
<p>Ad astra!</p>
