sevo
sevo

Reputation: 4609

Haskell implicit String polymorphism

How can I reduce effort in writing scripts that need to convert between different string representations?

For example, how to create a polymorphic setting where getContents and putStrLn do not need additional "glue code" to use functions like one below (derived from Network.HTTP.Types)?

pathElementsFromUri :: B.ByteString -> [T.Text]

How can I leverage locale to completely avoid I/O conversions?

Complete example where correct namespaces are needed to do simple things. Persons new to Haskell don't get things like this right at first try.

import Network.HTTP.Types
import Data.Foldable
import qualified Data.ByteString as B
import qualified Data.Text.IO as T

main = B.getContents >>= printout

printout = traverse_ T.putStrLn . pathElements
pathElements = fst . decodePath . extractPath

Upvotes: 3

Views: 136

Answers (1)

ErikR
ErikR

Reputation: 52039

I would check out the string-conversions package.

There is a completely polymorphic convertString function to convert to and from any of the major string types. Also, there are less polymorphic to... and from... functions.

E.g.

bs :: ByteString
bs = ...

txt :: Text
txt = ...

putStrLn $ convertString bs <> convertString txt

Upvotes: 5

Related Questions