Control.Foldl
packs a lot of interesting folds and folds transformers such as premap
and purely
but nowhere was I able to find a ‘folds transformer’, i.e. a function of type
transfold :: Fold a b -> Fold b c -> Fold a c
and that would allow me to extract and compose the step functions of a plurality of folds, making the following code type check:
fold (transfold increment1 double) [1,2,3] // purely hypothetically: expecting: [3,5,7] assuming a 'double' and an 'increment1' fold
.
Because there is no such utility one is stuck writing ‘parallel’ folds with applicatives as in
fold ( (/) <$> length <*> sum ) [1,2,3] // assuming a folder 'length'
where the folds don’t share results, or composing not folds per se but plain functions injected into a fold’s step function as in:
fold (premap (*2) increment1) [1,2,3] // [3,5,7] assuming an 'increment1' fold
which is fine but wastes the potential of preexisting folds which by the way could compose with each other.
In a nutshell, then, it seems that the whole library is premised on the idea that folds in the scope of a single call of a fold
function can never pipe their results. I am not sure if this premise is fine if you want to make the most of already usable folds.
Is there a way around this shortcoming?