Skip to content

Commit d05340e

Browse files
authored
add MonadError tryRead (#42)
1 parent 4388c62 commit d05340e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE FlexibleContexts #-}
2+
module MonadReadError where
3+
4+
import Control.Monad.Except
5+
6+
7+
data ReadError = EmptyInput | NoParse String deriving Show
8+
9+
tryRead :: (Read a, MonadError ReadError m) => String -> m a
10+
tryRead [] = throwError EmptyInput
11+
tryRead s = f $ reads s where
12+
f [(n, "")] = return n
13+
f [(n, xs)] = throwError $ NoParse s
14+
f [] = throwError $ NoParse s

0 commit comments

Comments
 (0)