foldl
foldl copied to clipboard
An idea concerning ST
I've had an interesting idea. There seems to be a way to specify all kinds of pure Fold as a case of FoldM spesialised to ST. This would solve the issue of the divergence of the API into pure and effectful. Also it would allow to use temporary mutable data-structures (those that have an ST API) in the accumulator.
type Fold =
forall s. FoldM (ST s)
It's hard to predict how that would affect GC and performance, but it surely seems interesting to explore.
I'm posting this primarily as a matter of a discussion.
If the goal is to eliminate the API divergence, you can also do:
type Fold = FoldM Identity
... and I know that @michaelt has done experiments showing that it gives identical performance
The main reason that I keep the separate APIs is just to preserve simpler inferred types and error messages when all you need is a Fold
In general I feel Haskell is often plagued with multiple APIs (fmap, map, ...). The excuse is often the same (better error messages and so on).
I just want to express the fact that multiple APIs are also very confusing for beginners (at least this is my experience coming from languages where subtype polymorphism is more natural) .
This is just a general comment and it might be not so relevant in this case. Sorry about that.
I like having both Fold and FoldM as separate things. The decision to go monadic is a big deal.
In this instance, I actually dont know what ST is (I used to know but forget), and since I do know what a Fold is, it seems like a confusing addition.
I'm +1 for merging Fold and FoldM, if that's up for discussion.