Hello i was wondering if given an Algebraic Data Type
how can i resolve the following problem :
u::Text->String
u =Data.Text.unpack
data Numeric=I Int | D Double
readNumeric::Text->Either String Numeric
readNumeric text=let str=u text in
if '.' `elem` str then
D (readEither str::Either String Double)
else
I (readEither str::Either String Int)
How can i cover both sides
of Either
given an ADT
? Practically 2*2
cases , uniformly?
I was considering using fromRight
on each branch ( e.g fromRight (D 0)
) of the pattern-matching
(in our case if since there are only 2 cases) but i do not know if it is the
best approach .
But fromRight
returns the inner type..i want to preserve the Either
Any ideas?
Isn't this is what you need?
readNumeric::Text -> Either String Numeric
readNumeric text=let str = u text in
if '.' `elem` str then
fmap D (readEither str::Either String Double)
else
fmap I (readEither str::Either String Int)