@@ -1,56 +1,33 @@
11{-# LANGUAGE BangPatterns #-}
2−import Debug.Trace
3−import Control.Monad
4−import Data.List
5−import qualified Data.Text.Read as T
2+import Control.Monad ()
3+import Data.List (foldl')
4+import System.Environment (getArgs)
5+import qualified Data.ByteString.Char8 as B
6+import qualified Data.ByteString.Lazy.Char8 as BL
67import qualified Data.Text as T
78import qualified Data.Text.Encoding as T
8−import qualified Data.ByteString.Char8 as B
9−import System.Environment
10−
11−dec = T.signed T.decimal :: T.Reader Int
12−
13−hex = T.signed T.hexadecimal :: T.Reader Int
14−
15−double = T.double :: T.Reader Double
16−
17−rational = T.rational :: T.Reader Double
9+import qualified Data.Text.Lazy as TL
10+import qualified Data.Text.Lazy.Encoding as TL
11+import qualified Data.Text.Lazy.Read as TL
12+import qualified Data.Text.Read as T
1813
19−read1 :: (Ord a, Num a) => T.Reader a -> T.Text -> a
20−read1 reader = foldl' go 1000000 . T.lines
14+readem :: (Ord a, Num a) =>
15+ IO [t] -> (t -> Either String (a,t)) -> IO ()
16+readem act reader = print . foldl' go 1000000 =<< act
2117 where go z t = case reader t of
2218 Left err -> error err
2319 Right (n,_) -> min n z
2420
25−--paranoid :: T.Text -> [(Double)]
26−paranoid = foldr go [] . T.lines
27− where go t xs = let a = case T.double t of
28− Left err -> error err
29− Right (n,_) -> n
30− b = case T.rational t of
31− Left err -> error err
32− Right (n,_) -> n
33− in if a /= b
34− then abs ((a-b)/a) : xs
35− else xs
36−
37−read2 :: Num a => T.Reader a -> T.Text -> a
38−read2 reader = go 0
39− where
40− go !i t
41− | T.null t = 0
42− | otherwise = case reader t of
43− Left err -> error err
44− Right (n,t') -> case T.uncons t' of
45− Nothing -> n
46− Just (_,t'') -> go (n+i) t''
47−
4821main = do
4922 args <- getArgs
50− t <- T.decodeUtf8 `fmap` B.getContents
23+ let strict = (T.lines . T.decodeUtf8) `fmap` B.getContents
24+ lazy = (TL.lines . TL.decodeUtf8) `fmap` BL.getContents
5125 case args of
52− ["dec"] -> print . read1 dec $ t
53− ["hex"] -> print . read1 hex $ t
54− ["double"] -> print . read1 double $ t
55− ["rational"] -> print . read1 rational $ t
56− --mapM_ print =<< paranoid `fmap` T.getContents
26+ ["dec"] -> readem strict (T.signed T.decimal :: T.Reader Int)
27+ ["hex"] -> readem strict (T.signed T.hexadecimal :: T.Reader Int)
28+ ["double"] -> readem strict (T.double :: T.Reader Double)
29+ ["rational"] -> readem strict (T.rational :: T.Reader Double)
30+ ["ldec"] -> readem lazy (TL.signed TL.decimal :: TL.Reader Int)
31+ ["lhex"] -> readem lazy (TL.signed TL.hexadecimal :: TL.Reader Int)
32+ ["ldouble"] -> readem lazy (TL.double :: TL.Reader Double)
33+ ["lrational"] -> readem lazy (TL.rational :: TL.Reader Double)