Merge pull request #120 from martijnbastiaan/readMaybeEither

Banish `String` on `readMaybe` and `readEither`
This commit is contained in:
Stephen Diehl
2021-01-04 18:30:54 +00:00
committed by GitHub
10 changed files with 47 additions and 18 deletions
+5
View File
@@ -1,3 +1,8 @@
Unreleased
=====
* Banish `String` on `readMaybe` and `readEither`.
0.3.0
=====
+2 -2
View File
@@ -750,10 +750,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+2 -2
View File
@@ -750,10 +750,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+2 -2
View File
@@ -749,10 +749,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+2 -2
View File
@@ -749,10 +749,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+2 -2
View File
@@ -749,10 +749,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+2 -2
View File
@@ -746,10 +746,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+2 -2
View File
@@ -746,10 +746,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+2 -2
View File
@@ -746,10 +746,10 @@ ratioPrec from GHC.Real
ratioPrec1 from GHC.Real
ratioZeroDenominatorError from GHC.Real
readChan from Control.Concurrent.Chan
readEither from Text.Read
readEither from Protolude
readFile from Data.Text.IO
readMVar from GHC.MVar
readMaybe from Text.Read
readMaybe from Protolude
reader from Control.Monad.Reader.Class
reads from Text.Read
realPart from Data.Complex
+26 -2
View File
@@ -128,6 +128,8 @@ module Protolude (
-- * Read functions
module Read,
readMaybe,
readEither,
-- * System functions
module System,
@@ -909,11 +911,10 @@ import Foreign.Storable as Foreign (Storable)
import Foreign.StablePtr as Foreign (StablePtr)
-- Read instances hiding unsafe builtins (read)
import qualified Text.Read as Read
import Text.Read as Read (
Read
, reads
, readMaybe
, readEither
)
-- Type synonymss for lazy texts
@@ -950,6 +951,29 @@ unsnoc = Foldable.foldr go Nothing
applyN :: Int -> (a -> a) -> a -> a
applyN n f = Foldable.foldr (.) identity (List.replicate n f)
-- | Parse a string using the 'Read' instance.
-- Succeeds if there is exactly one valid result.
--
-- >>> readMaybe ("123" :: Text) :: Maybe Int
-- Just 123
--
-- >>> readMaybe ("hello" :: Text) :: Maybe Int
-- Nothing
readMaybe :: (Read b, Conv.StringConv a String) => a -> Maybe b
readMaybe = Read.readMaybe . Conv.toS
-- | Parse a string using the 'Read' instance.
-- Succeeds if there is exactly one valid result.
-- A 'Left' value indicates a parse error.
--
-- >>> readEither "123" :: Either Text Int
-- Right 123
--
-- >>> readEither "hello" :: Either Text Int
-- Left "Prelude.read: no parse"
readEither :: (Read a, Conv.StringConv String e, Conv.StringConv e String) => e -> Either e a
readEither = first Conv.toS . Read.readEither . Conv.toS
-- | The print function outputs a value of any printable type to the standard
-- output device. Printable types are those that are instances of class Show;
-- print converts values to strings for output using the show operation and adds