Lazy comonadic array zipper
TL;DR: Stacking functions lazily can speed up your code.
Previously i showed the array zipper, which is really great. But: There is one big problem. And if you read the article you should have spotted it right away. I even added a dog-ear for myself in order to remind me that there is something left to be solved.
The dog-ear 🐶
Yeah. There is the original code. calling
head.lazy.map(f) which is kinda romantic.
There is nothing lazy in this code. It is all evaluated immediately.
So as soon as you create the
.zipper enum case … your beautiful lazy type gets converted into a stupid old array. Thats the way it is.
If you want to have full laziness in your type better prepare yourself.
Ah, that type of type ⛓
Whats the key? Having a lazy collection type as a starting point:
Based on this the whole thing has to keep track of the lazy type. like in
So here if we have a
ArrayZipper<LazyCollection<Int>>> we will receive a
Wanna really read that? Be Lazy! 👻
The hard part is really extract:
I found a great hint which made it clear to me. The lazy SubSequence stuff is kinda weird. We need to ensure it`s the same type.
So while calling extend you will se, that if you don`t want to evaluate, you have to provide the type information. Thats why at the end of the example we get a:
But - when you call
it gets crazy - you will find out when you check the
Let me see it 🙈
This is the current version. Maybe i am going to add further improvements. Still some TODOs left.
👻 Feel free to hit me on twitter. @elmkretzer