[Haskell-cafe] Foldable intercalate for Data.Text?

Viktor Dukhovni ietf-dane at dukhovni.org
Sun Jan 22 00:49:15 UTC 2023


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.


More information about the Haskell-Cafe mailing list