Skip to content

Commit

Permalink
Prepare article for publication
Browse files Browse the repository at this point in the history
  • Loading branch information
ploeh committed Oct 28, 2024
1 parent 1bfed63 commit 6cbdaf9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ <h3 id="a90284d241c4463583401504395a4a8a">
<li><a href="/2022/07/18/natural-transformations">Natural transformations</a></li>
<li><a href="/2024/09/16/functor-products">Functor products</a></li>
<li><a href="/2024/10/14/functor-sums">Functor sums</a></li>
<li>Functor compositions</li>
<li><a href="/2024/10/28/functor-compositions">Functor compositions</a></li>
<li>Traversals</li>
</ul>
</li>
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-03-19-functors-applicatives-and-friends.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<li><a href="/2022/07/18/natural-transformations">Natural transformations</a></li>
<li><a href="/2024/09/16/functor-products">Functor products</a></li>
<li><a href="/2024/10/14/functor-sums">Functor sums</a></li>
<li>Functor compositions</li>
<li><a href="/2024/10/28/functor-compositions">Functor compositions</a></li>
<li>Traversals</li>
</ul>
</li>
Expand Down
2 changes: 1 addition & 1 deletion _posts/2022-07-11-functor-relationships.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h3 id="70383da35af346f6b2dc3095a1bf7273">
<li><a href="/2022/07/18/natural-transformations">Natural transformations</a></li>
<li><a href="/2024/09/16/functor-products">Functor products</a></li>
<li><a href="/2024/10/14/functor-sums">Functor sums</a></li>
<li>Functor compositions</li>
<li><a href="/2024/10/28/functor-compositions">Functor compositions</a></li>
<li>Traversals</li>
</ul>
<p>
Expand Down
4 changes: 2 additions & 2 deletions _posts/2024-10-14-functor-sums.html
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ <h3 id="02e7e55d7f6f4c0d94c50cf577238859">
By now the pattern should be familiar. Call <code><span style="font-weight:bold;color:#1f377f;">selector</span>(<span style="font-weight:bold;color:#1f377f;">v</span>)</code> directly on the 'naked' values, and pass <code>selector</code> to any other functors' <code>Select</code> method.
</p>
<p>
That's <em>almost</em> all the building blocks we have to declare <code><span style="color:#2b91af;">BinaryTreeZipper</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> a functor as well, but we need one last theorem before we can do that. We'll conclude this work in <a href="">the next article</a>.
That's <em>almost</em> all the building blocks we have to declare <code><span style="color:#2b91af;">BinaryTreeZipper</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> a functor as well, but we need one last theorem before we can do that. We'll conclude this work in <a href="/2024/10/28/functor-compositions">the next article</a>.
</p>
<h3 id="2b3a70f8791c41eb952ff160398fe441">
Higher arities <a href="#2b3a70f8791c41eb952ff160398fe441">#</a>
Expand Down Expand Up @@ -318,6 +318,6 @@ <h3 id="8545e09908fb4df4ace08e7b20ffc509">
There's one more rule like this one.
</p>
<p>
<strong>Next:</strong> Functor compositions.
<strong>Next:</strong> <a href="/2024/10/28/functor-compositions">Functor compositions</a>.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: post
title: "Functor compositions"
description: "A functor nested within another functor forms a functor. With examples in C# and another language."
date: 2024-08-20 10:58 UTC
date: 2024-10-28 6:58 UTC
tags: [Software Design, Functional Programming]
---
{% include JB/setup %}
Expand All @@ -21,10 +21,10 @@
Since <a href="/2018/03/22/functors">functors</a> tend to be quite common, and since they're useful enough that many programming languages have special support or syntax for them, the ability to recognize a potential functor can be useful. Given a type like <code>Foo&lt;T&gt;</code> (C# syntax) or <code>Bar&lt;T1, T2&gt;</code>, being able to recognize it as a functor can come in handy. One scenario is if you yourself have just defined this data type. Recognizing that it's a functor strongly suggests that you should give it a <code>Select</code> method in C#, a <code>map</code> function in <a href="https://fsharp.org/">F#</a>, and so on.
</p>
<p>
Not all generic types give rise to a (covariant) functor. Some are rather <a href="/2021/09/02/contravariant-functors">contravariant functors</a>, some are <a href="/2022/08/01/invariant-functors">invariant</a>, and some again are <a href="/2020/10/19/monomorphic-functors">monomorphic</a>.
Not all generic types give rise to a (covariant) functor. Some are rather <a href="/2021/09/02/contravariant-functors">contravariant functors</a>, and some are <a href="/2022/08/01/invariant-functors">invariant</a>.
</p>
<p>
If, on the other hand, you have a data type where one functor is nested within another functor <em>while sharing a type parameter</em>, then the data type itself gives rise to a functor. You'll see some examples in this article.
If, on the other hand, you have a data type where one functor is nested within another functor, then the data type itself gives rise to a functor. You'll see some examples in this article.
</p>
<h3 id="a97b2f6471b74db6a83362a552ee5b03">
Abstract shape <a href="#a97b2f6471b74db6a83362a552ee5b03">#</a>
Expand Down Expand Up @@ -139,16 +139,16 @@ <h3 id="32b4e828d4584c3d8cda81a9682aee34">
<span style="color:blue;">public</span>&nbsp;<span style="color:#2b91af;">T</span>&nbsp;Item&nbsp;{&nbsp;<span style="color:blue;">get</span>;&nbsp;}</pre>
</p>
<p>
Just like <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> there's a 'naked' <code>T</code> value and a collection. The main difference is that here, the collection is of the same type as the object itself: <code><span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>.
Just like <code><span style="color:#2b91af;">PriorityCollection</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> there's a collection, as well as a 'naked' <code>T</code> value. The main difference is that here, the collection is of the same type as the object itself: <code><span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code>.
</p>
<p>
You've seen a similar example in <a href="">the previous article</a>, which also had a recursive data structure. If you assume, however, that <code><span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> gives rise to a functor, then so does the nested composition of putting it in a collection. This means, from the 'theorem' put forth in this article, that <code><span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;</code> composes as a functor. Finally you have a product of a <code>T</code> (which is isomorphic to the <a href="/2018/09/03/the-identity-functor">Identity functor</a>) and that composed functor. From <a href="">Functor products</a> it follows that that's a functor too, which explains why <code><span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> forms a functor. <a href="/2018/08/06/a-tree-functor">The article</a> shows the <code>Select</code> implementation.
You've seen a similar example in <a href="/2024/10/14/functor-sums">the previous article</a>, which also had a recursive data structure. If you assume, however, that <code><span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> gives rise to a functor, then so does the nested composition of putting it in a collection. This means, from the 'theorem' put forth in this article, that <code><span style="color:#2b91af;">IReadOnlyCollection</span>&lt;<span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;&gt;</code> composes as a functor. Finally you have a product of a <code>T</code> (which is isomorphic to the <a href="/2018/09/03/the-identity-functor">Identity functor</a>) and that composed functor. From <a href="/2024/09/16/functor-products">Functor products</a> it follows that that's a functor too, which explains why <code><span style="color:#2b91af;">Tree</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> forms a functor. <a href="/2018/08/06/a-tree-functor">The article</a> shows the <code>Select</code> implementation.
</p>
<h3 id="17209725eab64da598ba924342dafbd0">
Binary tree Zipper <a href="#17209725eab64da598ba924342dafbd0">#</a>
</h3>
<p>
In both previous articles you've seen pieces of the puzzle explaining why the <a href="">binary tree Zipper</a> gives rise to functor. There's one missing piece, however, that we can now finally address.
In both previous articles you've seen pieces of the puzzle explaining why the <a href="/2024/09/09/a-binary-tree-zipper-in-c">binary tree Zipper</a> gives rise to functor. There's one missing piece, however, that we can now finally address.
</p>
<p>
Recall that <code><span style="color:#2b91af;">BinaryTreeZipper</span>&lt;<span style="color:#2b91af;">T</span>&gt;</code> composes these two objects:
Expand Down Expand Up @@ -196,7 +196,7 @@ <h3 id="800728c4c9c54aec815c62352843d52b">
On the other hand, <a href="/2016/06/28/roman-numerals-via-property-based-tdd">sometimes you need</a> to work with a collection of generators, such as <code>seq&lt;Gen&lt;'a&gt;&gt;</code>.
</p>
<p>
These are all examples of functors within functors. It's not a given that you <em>must</em> treat such combinations as a functor in its own right. To be honest, typically, you don't. On the other hand, if you find yourself writing <code>Select</code> within <code>Select</code>, or <code>map</code> within <code>map</code>, depending on your language, it might make your code more succinct and readable if you give that combination a specialized functor affordance.
These are all examples of functors within functors. It's not a given that you <em>must</em> treat such a combination as a functor in its own right. To be honest, typically, you don't. On the other hand, if you find yourself writing <code>Select</code> within <code>Select</code>, or <code>map</code> within <code>map</code>, depending on your language, it might make your code more succinct and readable if you give that combination a specialized functor affordance.
</p>
<h3 id="bffe8909eb904260be8aa4ab1a22efb2">
Higher arities <a href="#bffe8909eb904260be8aa4ab1a22efb2">#</a>
Expand Down Expand Up @@ -249,7 +249,7 @@ <h3 id="14f39729b7ab426e83a35a067cf8f3a1">
This is useful to know, particularly if you're working in a language with only partial support for functors. Mainstream languages aren't going to automatically turn such stacks into functors, in the way that Haskell's <code>Compose</code> container almost does. Thus, knowing when you can safely give your generic types a <code>Select</code> method or <code>map</code> function may come in handy.
</p>
<p>
To be honest, though, this result is hardly the most important 'theorem' concerning stacks of functors. In reality, you often run into situation where you <em>do</em> have a stack of functors, but they're in the wrong order. You may have a collection of asynchronous tasks, but you really need an asynchronous task that contains a collection of values. The next article addresses that problem.
To be honest, though, this result is hardly the most important 'theorem' concerning stacks of functors. In reality, you often run into situations where you <em>do</em> have a stack of functors, but they're in the wrong order. You may have a collection of asynchronous tasks, but you really need an asynchronous task that contains a collection of values. The next article addresses that problem.
</p>
<p>
<strong>Next:</strong> Traversals.
Expand Down

0 comments on commit 6cbdaf9

Please sign in to comment.