diff options
author | Gregor Kleen <aethoago@141.li> | 2017-02-16 19:03:30 +0100 |
---|---|---|
committer | Gregor Kleen <aethoago@141.li> | 2017-02-16 19:03:30 +0100 |
commit | 4343e02ca8431e61e2dc1755d1288dd6c55c9a23 (patch) | |
tree | 2f1397eb4c8649dffb7ae2adaedfd101bdda4fab /lib/Postdelay/TimeSpec/Units.hs | |
parent | 333ea946916b005134e7ba249178acad4858a67d (diff) | |
download | postdelay-4343e02ca8431e61e2dc1755d1288dd6c55c9a23.tar postdelay-4343e02ca8431e61e2dc1755d1288dd6c55c9a23.tar.gz postdelay-4343e02ca8431e61e2dc1755d1288dd6c55c9a23.tar.bz2 postdelay-4343e02ca8431e61e2dc1755d1288dd6c55c9a23.tar.xz postdelay-4343e02ca8431e61e2dc1755d1288dd6c55c9a23.zip |
Bounded numeric parsers
Diffstat (limited to 'lib/Postdelay/TimeSpec/Units.hs')
-rw-r--r-- | lib/Postdelay/TimeSpec/Units.hs | 9 |
1 files changed, 1 insertions, 8 deletions
diff --git a/lib/Postdelay/TimeSpec/Units.hs b/lib/Postdelay/TimeSpec/Units.hs index a094ea3..4874ce9 100644 --- a/lib/Postdelay/TimeSpec/Units.hs +++ b/lib/Postdelay/TimeSpec/Units.hs | |||
@@ -56,14 +56,7 @@ instance HasResolution p => VectorSpace (Fixed p) where | |||
56 | 56 | ||
57 | 57 | ||
58 | timeLength :: StringParser s m => m Time | 58 | timeLength :: StringParser s m => m Time |
59 | timeLength = (*^) <$> lexeme rational <*> timeUnit <?> "Length of time" | 59 | timeLength = (*^) <$> lexeme (boundedRational False $ 0 <=..< PosInf) <*> timeUnit <?> "Length of time" |
60 | |||
61 | rational :: forall s m n. (StringParser s m, Fractional n, Num n) => m n | ||
62 | rational = combine <$> some digitChar <*> option [] (char '.' *> many digitChar) <?> "Decimal number" | ||
63 | where | ||
64 | combine :: [Char] -> [Char] -> n | ||
65 | combine (map fromDigit -> whole) (map fromDigit -> fractional) | ||
66 | = foldl' (\i l -> i * 10 + l) 0 whole + (foldr' (\h t -> h + t / 10) 0 fractional) / 10 | ||
67 | 60 | ||
68 | timeUnit :: StringParser s m => m Time | 61 | timeUnit :: StringParser s m => m Time |
69 | timeUnit = label "Unit of time" . choice $ | 62 | timeUnit = label "Unit of time" . choice $ |