[Haskell-beginners] Data.Text to Int?

Tony Morris tonymorris at gmail.com
Sat Dec 15 00:57:18 CET 2012


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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


-- 
Tony Morris
http://tmorris.net/




More information about the Beginners mailing list