r/functionalprogramming • u/josephjnk • Apr 26 '26
Question Idea: data structures which incrementally compute their own (right) folds?
I was implementing linked lists for the Nth time (as one does) and felt a certain temptation to add a size field to them, which would be computed incrementally as each cons cell was created. I had use for this, recomputing it is inefficient, and caching it is annoying. Thing is that for a reusable data structure, this feels arbitrary. Which got me to thinking: Why not accept an arbitrary folding function when instantiating the list, and applying it to every cons cell as it's created? This way the list could be customized for whatever purpose it would be put to-- you could compute things like counts (total, or of specific things), sums, or averages. I suspect more interesting things are possible. A list that works like a map/dictionary, maybe, and which has a lookup table for its elements as well as holding them in order? IDK if that specifically would be the best solution to any particular problem, but it feels fairly flexible.
Is this already a thing? I haven't seen or heard of it before, except that finger trees seem kind of related as an "extra-general data structure that can be customized with a function". They feel very different overall though. Also, if you were to make a library of data structures, what would you name these amortized folding variants?
10
u/aaaaargZombies Apr 26 '26
Sounds a bit like a scan but you are suggesting you'd have the operation baked into the list and it would work as you build it up? Kind of the opposite of laziness?
6
u/Bob_Dieter Apr 27 '26
https://www.cs.ox.ac.uk/ralf.hinze/publications/FingerTrees.pdf
These guys propose such a thing for finger trees (skip forward to section 4, annotated trees). They do it by parametrizing their tree with an extra type parameter v, which must implement monoid. Each node carries an extra value of type v, which is computed by joining the cached values of their respective children. Now remember that lists are essentially just maximally lobsided trees, and it starts to look similar to your idea.
Edit: sorry, I missed that you mentioned finger trees yourself.
3
u/Masynchin Apr 26 '26
Make it with Cofree and label it with whatever you want?
1
u/josephjnk 1d ago
Update nearly a year later, in case anyone stumbles into this question in the future: I was able to find a blog post where the author refers to these sorts of things as queues with “measures”: https://byorgey.github.io/blog/posts/2024/11/27/stacks-queues.html
4
u/joelangeway Apr 27 '26
I first saw this idea suggested for finger trees. You can do it with any immutable data structure and it makes the most sense for trees. A B-Tree for example always computes the min and or max of key values for each subtree. You can do that for any associative aggregation.
3
u/JealousPurchase7068 Apr 26 '26
Sounds like event sourcing
3
u/gnahraf Apr 26 '26
If you plan to record the size of each node, take a Elias Fano compression. No linked list necessary since you'll already have random access. However insertions will be expensive (unless the insert is really just an appending to the end).
3
u/Few-Big7409 Apr 27 '26
If you wanted a map/dict to hold order why not add the index as a part of the value? I am not deep into functional stuff so I would be happy for someone to explain why this doesn't work as a map that remembers order.
3
u/InfinitePoints Apr 27 '26
This feels somewhat equivalent to making the function we want to apply memorized.
0
u/[deleted] Apr 26 '26
[deleted] — view deleted comment