This is a strict, somewhat efficient implementation of something between Data.List.partition
and Beautiful Folding.
It allows a (pure or ST) stream of values to be processed into multiple fields, using a nice 'Applicative' interface.
Beautiful Folding allocates O(#fields * #inputs), whereas this allocates O(#fields + #inputs).
Time complexity may still be O(#fields * #inputs), because of the nested tuple reads, but presumably with a lower constant factor than Beautiful Folding. It would take a clever compiler to see that the m
type can be represented by a flat data structure instead of a heterogeneous linked list composed of 2-tuples.
Unlike partition
, it does not guarantee that a value only occurs in one field; it is a generalization after all. Partitioner
should probably be renamed because of this.
I wrote this module out of curiosity as a sort of feasibility study in case I had to optimize a pure implementation of this concept, which has the same interface but just looks a lot like a simpler Beautiful Folding on the inside.