Foldable intercalate for Data.Text?

Hi, I was looking at intercalate in Data.Text, and I see that the signature is Text -> [Text] -> Text. I'm curious why this isn't (say) Foldable f => Text -> f Text -> Text Perhaps because this would be harder to optimize? -BenRI

On Sat, Jan 21, 2023 at 02:11:22PM -0500, Benjamin Redelings wrote:
I was looking at intercalate in Data.Text, and I see that the signature is
Text -> [Text] -> Text.
I'm curious why this isn't (say)
Foldable f => Text -> f Text -> Text
Perhaps because this would be harder to optimize?
I rather think it is simply a direct analogue of `intercalate` for Strings (i.e. Lists). The natural API for folding containers to Text is Text builders: import Data.Text.Lazy.Builder buildCalate :: Foldable f => (a -> Builder) -> Builder -> f a -> Builder buildCalate f sep1 = snd . foldr go (mempty, mempty) where go e (sep, r) = (sep1, f e <> sep <> r) -- Viktor.

On Sat, Jan 21, 2023 at 07:49:15PM -0500, Viktor Dukhovni wrote:
I rather think it is simply a direct analogue of `intercalate` for Strings (i.e. Lists). The natural API for folding containers to Text is Text builders:
import Data.Text.Lazy.Builder
buildCalate :: Foldable f => (a -> Builder) -> Builder -> f a -> Builder buildCalate f sep1 = snd . foldr go (mempty, mempty) where go e (sep, r) = (sep1, f e <> sep <> r)
But the above was hastily written, and came out too strict in the tail, the correct form is: buildCalate :: Foldable f => (a -> Builder) -> Builder -> f a -> Builder buildCalate f sep = snd . foldr go (mempty, mempty) where go x ~(s, r) = (sep, f x <> s <> r) we need to obtain the next separator without forcing the rest of the fold. -- Viktor.
participants (2)
-
Benjamin Redelings
-
Viktor Dukhovni