Monoid mconcat
jamesdbrock opened this issue · comments
https://hackage.haskell.org/package/base/docs/Data-Monoid.html#v:mconcat
The whole point of mconcat
in a Monoid
typeclass is so that that instances can specialize mconcat
as a performance optimization, especially stringy instances like String
, Array
, or ArrayBuffer
.
For it to be possible to write the instances to make that optimization work, though, mconcat
must be in the Monoid
typeclass, not in the Foldable
typeclass.
Does Purescript have something like that? And if not, why not?
purescript/purescript-foldable-traversable#41
Maybe there's a good reason for not having mconcat
which I haven't thought of, and if so, I'd like to document the reason in this issue.
Maybe a new typeclass would be called for:
class Monoid m <= Mconcat m where
mconcat :: forall t m. Foldable t => t m -> m
Ah but Foldable
is not in prelude. So this would be in foldable-traversable.
Oh, maybe MonadWriter can be used as an mconcat
?
I think the reason is basically just that we don't have default method implementations, and including any function you might want to optimize in the class definition is awkward, especially if the class is already in use downstream (because adding new members to an existing class is a breaking change). I think I would prefer to just provide specialised optimised versions of fold
as regular functions rather than adding a new type class, though. Do you have any specific examples of optimised versions of fold
you'd like to use in PureScript?
I think I would prefer to just provide specialised optimised versions of fold as regular functions rather than adding a new type class
That is a totally reasonable attitude, I think.
Do you have any specific examples of optimised versions of fold you'd like to use in PureScript?
ArrayBuffer
? There doesn't seem to exist a builder for ArrayBuffer
. But ArrayBuffer
isn't even a Monoid
, so mconcat
wouldn't help.
And come to think of it, I don't think the Monoid
constraint adds anything to the Mconcat
typeclass which I wrote above.
Perhaps "builder", or "special optimized version of fold", is a better answer than "mconcat" for this kind of activity.