Repository : ssh://darcs.haskell.org//srv/darcs/packages/base

On branch  : master

http://hackage.haskell.org/trac/ghc/changeset/0e1a02b96cfd03b8488e3ff4ce232466d6d5ca77

>---------------------------------------------------------------

commit 0e1a02b96cfd03b8488e3ff4ce232466d6d5ca77
Author: Iavor S. Diatchki <[email protected]>
Date:   Thu Dec 22 15:43:31 2011 -0800

    Export "readEither" and add "readMaybe".
    
    This commit implements the change discussed in the following
    thread on the Haskell libraries list:
    
    http://www.haskell.org/pipermail/libraries/2011-December/thread.html#17290
    
    NOTE:  This only implements the change for GHC, but the change
    makes sense for Hugs too... Perhaps we should simply re-implement 
'readEither'
    in terms of 'reads'?

>---------------------------------------------------------------

 Text/Read.hs |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/Text/Read.hs b/Text/Read.hs
index cea334b..88784ac 100644
--- a/Text/Read.hs
+++ b/Text/Read.hs
@@ -42,6 +42,8 @@ module Text.Read (
 #ifdef __GLASGOW_HASKELL__
    readListDefault,     -- :: Read a => ReadS [a]
    readListPrecDefault, -- :: Read a => ReadPrec [a]
+   readEither,          -- :: Read a => String -> Either String a
+   readMaybe            -- :: Read a => String -> Maybe a
 #endif
 
  ) where
@@ -50,6 +52,7 @@ module Text.Read (
 import GHC.Base
 import GHC.Read
 import Data.Either
+import Data.Maybe
 import Text.ParserCombinators.ReadP as P
 #endif
 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
@@ -82,6 +85,9 @@ parens p = optional
 reads :: Read a => ReadS a
 reads = readsPrec minPrec
 
+-- | Parse a string using the 'Read' instance.
+-- Succeeds if there is exactly one valid result.
+-- A 'Left' value indicates a parse error.
 readEither :: Read a => String -> Either String a
 readEither s =
   case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
@@ -94,6 +100,13 @@ readEither s =
        lift P.skipSpaces
        return x
 
+-- | Parse a string using the 'Read' instance.
+-- Succeeds if there is exactly one valid result.
+readMaybe :: Read a => String -> Maybe a
+readMaybe s = case readEither s of
+                Left _  -> Nothing
+                Right a -> Just a
+
 -- | The 'read' function reads input from a string, which must be
 -- completely consumed by the input process.
 read :: Read a => String -> a



_______________________________________________
Cvs-libraries mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/cvs-libraries

Reply via email to