Source code for snippets ⋅ Twitter discussion ⋅ Linkedin discussion ⋅ Facebook discussion
Let's say we have two covariant functors from Arrow into Arrow:
There are basically two ways to compose them - nesting each other i.e. one functor becomes a parameter of another one:
But why do we call this nesting exactly a functor composition? Let's take a look at their subtypes:
So that we can wrap our nestings into corresponding wrappers and get new functors:
I don't want to mention category of functors since we don't use these notions anywhere else but what we need to know is that this functor composition should be associative for sure...
... and identity. There should be a functor that doesn't change any other functors - it's called Alone:
Coming back to our example - which of these functor compositions is more suitable for these two?
What if I tell you that you don't need to think about it since for some functors there is a reserved place?
How does it work? Why do we get the same result when these functors are placed on opposite sides?
These jointed functors define a preferred type of functor composition per each functor so that you don't get into details of how they are actually built - some joints could be quite complicated:
The main benefit of these preordered functor compositions is that they are bundled with a set of natural transformations:
Let's see how it works on a primive example - Given some Boolean value, halt if it's False:
This example is to simple, let's alternate it a bit - let it be ASCII instead of Boolean and we stop if it's not a printable character:
This snippet is not that practical, but there are some things worth to be noted: having this jointed functor composition we can define individual natural transformations that relate functors inside functor composition!
Let's choose another pair of functors - World and State, we also have dedicated natural transformations for them:
Let's start with a simple example and gradually add more features - we can statefully update ASCII from console input:
Now we want to keep many ASCII characters instead of possible one so that each new character from console input is pushed onto the top of List:
These are basic examples of jointed functor compositions, in the next chapter we will use more complex ones with different behaviour labels.