Source code for snippets ⋅ Twitter discussion ⋅ Reddit discussion ⋅ Linkedin discussion ⋅ Facebook discussion
This is an extension of Transformations, functors, categories article with more practical use cases.
We've seen this trivial component of natural transformation used as an example:
According to First behaviour we will get some value only if List is not empty:
If the same List would be non empty we could get a Front item at any time:
Despite the fact these are Arrow components between distinct functors they look alike:
We also have components with same labels but in another category called Scope:
Since Scope is a subtype of Arrow - components behaviour should be aligned with the latter accordingly:
But additional power that Scope brings to Arrow is to update values without affecting the rest of datastructures:
For both First and Front behaviour (they do point to a head item) we have their counterpart - Stock that points to tail items:
Challenge: why in both cases we point to a List? Why couldn't we point to a Nonempty List?
So far we have seen Arrow and Scope component, but we also have Event one:
Its purpose is to separate First item and Stock ones i.e. pop operation:
Okay, so it was an overview of some available components for List and its variation - Nonempty List.
As you may noticed already, we can co-represent both of them with Sum of Units infinitely:
There is another List variation called Scrolling List and we also can co-represent it:
Natural transformation associated with underlying functors could tell us a lot about purpose of such a datastructure:
Let's try out Event components - they let us Shift Front item Aback and Forth:
Many of you already recognised List Zipper here since we can bidirectionally traverse a List with O(n).
It's might be not convenient to look at the result as a whole datastructures, we can Scope within updated Front item instead:
But can we access adjacent items without modifying it's Front item? Scope components related to Scrolling List again:
So in co-representation of Scrolling List we put Front item in the middle of Shafted List so that we can visualise continuity:
... and Shafted List is essentially a Product of Lists, each of them we can access using according components:
Challenge: why in case of Aback the resulted List is reversed? It's related to ability to Shift for O(n).
... and, as if was stated in the beginning, List itself has a component that points to a First item. What we have:
Now look closely at type declarations above - having an itch for composing those? Just place these labels together:
What you just saw corresponds to vertical composition of natural transformations. See it in action: