[Haskell-cafe] Polymorphic functions over string libraries

Konstantine Rybnikov k-bx at k-bx.com
Mon Oct 27 22:22:51 UTC 2014


I use small string-class library
http://hackage.haskell.org/package/string-class

You have type classes for generic things (I don't use them in practice
much, to be honest, so please check them out by yourself), and also you
have easy toText/toString/fromText/fromString etc. functions, which are
quite handy.

On Sun, Oct 26, 2014 at 3:36 AM, gonzaw <gonzaw308 at gmail.com> wrote:

> Hi.
>
> I was wondering what would be the best way to create a polymorphic function
> over any possible string library (Text, String, Bytestring, etc).
>
> For instance, imagine I have to read a file with text, transform this text
> in some way and output it to another file, or to the console.
> If I wanted to use String, I'd just do this:
> /
> transform :: String -> String
> main = readFile "input.txt" >>= writeFile "output.txt" . transform
> /
> But if I wanted to use Text instead, I'd have to use this:
> /
> import qualified Data.Text.IO as T
>
> transform :: Text -> Text
> main = T.readFile "input.txt" >>= T.writeFile "output.txt" . transform
> /
> Idem for ByteString.
>
> I was wondering if there was a way to create these computations in a
> generic
> way, for any kind of string library, something like this:
> /
> class StringLibrary s where:
> sReadFile :: FilePath -> IO s
> sWriteFile :: FilePath -> s -> IO ()
> ...
> /
> So then I'd just have this:
> /
> transform :: StringLibrary s => s -> s
> main = sReadFile "input.txt" >>= sWriteFile "output.txt" . transform
> /
> Now I can perform the computation I want without being tied down to a
> specific library. At times when I create some quick scripts, I find myself
> using one library (for example using String to get it finished more
> quickly,
> since I have less experience with the other ones), but find that it's too
> slow or has some problem that is solved by using one of the other
> libraries.
> Yet swapping from one to the other is more cumbersome than expected at
> times.
> In the example above, I could easily swap between them, just by forcing the
> compiler to typecheck to a specific one (for instance by changing the type
> of "transform"). Or if I wanted to, I could leave it as it is and export it
> as a library of my own.
>
> Is there a way to do something like this in Haskell, with existing
> libraries?
> In terms of using the string datatype as some sort of container of
> characters, I think there are libraries like Lens and mono-traversable that
> allow you to do stuff like this. But I'm not too familiar with them (at
> least using them in this way).
>
>
>
>
> --
> View this message in context:
> http://haskell.1045720.n5.nabble.com/Polymorphic-functions-over-string-libraries-tp5758630.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141028/1b8872c4/attachment.html>


More information about the Haskell-Cafe mailing list