Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Data.Text to Int? (Emmanuel Touzery)
2. Re: Data.Text to Int? (Emmanuel Touzery)
3. Re: Data.Text to Int? (Brandon Allbery)
4. Re: Data.Text to Int? (Emmanuel Touzery)
5. Re: Data.Text to Int? (Daniel Fischer)
6. Re: Data.Text to Int? (Emmanuel Touzery)
----------------------------------------------------------------------
Message: 1
Date: Fri, 14 Dec 2012 15:57:21 +0100
From: Emmanuel Touzery <[email protected]>
Subject: [Haskell-beginners] Data.Text to Int?
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121214/b0902305/attachment-0001.htm>
------------------------------
Message: 2
Date: Fri, 14 Dec 2012 16:03:38 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] Data.Text to Int?
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
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:read
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121214/b91a7fce/attachment-0001.htm>
------------------------------
Message: 3
Date: Fri, 14 Dec 2012 10:05:43 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Data.Text to Int?
To: Emmanuel Touzery <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<CAKFCL4VkOJ19BbSevkLnb9LsVZpceQmVJqs7x=bh0edil76...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Fri, Dec 14, 2012 at 9:57 AM, Emmanuel Touzery <[email protected]>wrote:
> 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
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121214/101fff95/attachment-0001.htm>
------------------------------
Message: 4
Date: Fri, 14 Dec 2012 16:19:02 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] Data.Text to Int?
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
>
> 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
------------------------------
Message: 5
Date: Fri, 14 Dec 2012 18:25:23 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Data.Text to Int?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
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.
------------------------------
Message: 6
Date: Fri, 14 Dec 2012 19:42:37 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] Data.Text to Int?
To: "[email protected]" <[email protected]>
Message-ID:
<cac42rendeowwrjkm4x6beu6iiuqbg9hvsddwmha-pksqhgw...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 <
[email protected]> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121214/dfc5613e/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 54, Issue 20
*****************************************