
Hello, I expected "read" to work directly on a Data.Text because Data.Text has an instance of the Read class, but it seems to fail: --------- import Data.Text main = do let a = pack "1" let b = read a :: Int putStrLn "parsed OK" --------- test.hs:5:22: Couldn't match expected type `String' with actual type `Text' In the first argument of `read', namely `a' In the expression: read a :: Int In an equation for `b': b = read a :: Int Sure I can do"read (unpack a) :: Int" but there must be a way to do it directly? Emmanuel

I see I got it wrong... It's the other way around in read x :: Int, it's Int which has an instance of Read... http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:re... So if Data.Text has an instance of Read I can convert a String to Data.Text using Read... Not what I wanted. Must I really do Data.Text->String->Int or is there a direct way? emmanuel On 14.12.2012 15:57, Emmanuel Touzery wrote:
Hello,
I expected "read" to work directly on a Data.Text because Data.Text has an instance of the Read class, but it seems to fail:
--------- import Data.Text
main = do let a = pack "1" let b = read a :: Int putStrLn "parsed OK" ---------
test.hs:5:22: Couldn't match expected type `String' with actual type `Text' In the first argument of `read', namely `a' In the expression: read a :: Int In an equation for `b': b = read a :: Int
Sure I can do"read (unpack a) :: Int" but there must be a way to do it directly?
Emmanuel

On Fri, Dec 14, 2012 at 9:57 AM, Emmanuel Touzery
I expected "read" to work directly on a Data.Text because Data.Text has an instance of the Read class, but it seems to fail:
An instance of Read means that read can *produce* a Text, not that it can consume one. read always reads from a String, as you can see from its type: Prelude Data.Text> :t read read :: Read a => String -> a (Note that it is the result type a in the context for Read.) See the Data.Text.Read module (part of the text package you already have installed) for how to do similar things with a Text as a source. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

An instance of Read means that read can *produce* a Text, not that it can consume one. read always reads from a String, as you can see from its type:
Prelude Data.Text> :t read read :: Read a => String -> a
(Note that it is the result type a in the context for Read.)
yes, seeing the type signature I understood it. I was googling it without much luck, I didn't think of using ghci. I'll use that next time.
See the Data.Text.Read module (part of the text package you already have installed) for how to do similar things with a Text as a source.
I see... However it uses Either and returns a pair, unlike "read". It's a plus for reliability but an annoyance in my case. In my case I know positively it's a number. In this case I did a filter isDigit, but this will happen also if I match using a regular expression and [0-9] or \d. In the end the most terse way to code it is to go through unpack then it seems. Using Data.Text.Read all I see is: fst $ right $ decimal t where right (Right a) = a so I'll probably do: read $ unpack t and be done with it... Thank you! emmanuel

On Freitag, 14. Dezember 2012, 16:19:02, Emmanuel Touzery wrote:
I see... However it uses Either and returns a pair, unlike "read". It's a plus for reliability but an annoyance in my case. In my case I know positively it's a number. In this case I did a filter isDigit, but this will happen also if I match using a regular expression and [0-9] or \d.
In the end the most terse way to code it is to go through unpack then it seems. Using Data.Text.Read all I see is:
fst $ right $ decimal t where right (Right a) = a
so I'll probably do:
read $ unpack t
and be done with it...
Note that unpacking to String and then reading from the String is not the most efficient way. For the cases you are sure to have a valid input Text and no leftovers (you are interested in), you can define it'sSafeIPromise :: Reader a -> Text -> a it'sSafeIPromise = (value .) where value (Right (v,_)) = v and use readInt :: Text -> Int readInt = it'sSafeIPromise decimal If you use it a lot, it's worth the bit of additional typing.

good pattern matching, combining both the Right and the pair matching.. I didn't realize I can do both at once :-) I'll use this, thank you! Emmanuel On Fri, Dec 14, 2012 at 6:25 PM, Daniel Fischer < daniel.is.fischer@googlemail.com> wrote:
On Freitag, 14. Dezember 2012, 16:19:02, Emmanuel Touzery wrote:
I see... However it uses Either and returns a pair, unlike "read". It's a plus for reliability but an annoyance in my case. In my case I know positively it's a number. In this case I did a filter isDigit, but this will happen also if I match using a regular expression and [0-9] or \d.
In the end the most terse way to code it is to go through unpack then it seems. Using Data.Text.Read all I see is:
fst $ right $ decimal t where right (Right a) = a
so I'll probably do:
read $ unpack t
and be done with it...
Note that unpacking to String and then reading from the String is not the most efficient way.
For the cases you are sure to have a valid input Text and no leftovers (you are interested in), you can define
it'sSafeIPromise :: Reader a -> Text -> a it'sSafeIPromise = (value .) where value (Right (v,_)) = v
and use
readInt :: Text -> Int readInt = it'sSafeIPromise decimal
If you use it a lot, it's worth the bit of additional typing.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Rather than filter isDigit then read, perhaps you might prefer mapMaybe. On 15/12/12 01:19, Emmanuel Touzery wrote:
An instance of Read means that read can *produce* a Text, not that it can consume one. read always reads from a String, as you can see from its type:
Prelude Data.Text> :t read read :: Read a => String -> a
(Note that it is the result type a in the context for Read.)
yes, seeing the type signature I understood it. I was googling it without much luck, I didn't think of using ghci. I'll use that next time.
See the Data.Text.Read module (part of the text package you already have installed) for how to do similar things with a Text as a source.
I see... However it uses Either and returns a pair, unlike "read". It's a plus for reliability but an annoyance in my case. In my case I know positively it's a number. In this case I did a filter isDigit, but this will happen also if I match using a regular expression and [0-9] or \d.
In the end the most terse way to code it is to go through unpack then it seems. Using Data.Text.Read all I see is:
fst $ right $ decimal t where right (Right a) = a
so I'll probably do:
read $ unpack t
and be done with it...
Thank you!
emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Tony Morris http://tmorris.net/
participants (4)
-
Brandon Allbery
-
Daniel Fischer
-
Emmanuel Touzery
-
Tony Morris