chore(term): generalize termToRational by using Enum instances
This commit is contained in:
parent
bcbaad0da8
commit
94b4ff2e7e
@ -110,24 +110,16 @@ termFromText t
|
|||||||
(~=) = (==) `on` CI.mk
|
(~=) = (==) `on` CI.mk
|
||||||
|
|
||||||
termToRational :: TermIdentifier -> Rational
|
termToRational :: TermIdentifier -> Rational
|
||||||
termToRational TermIdentifier{..} = fromInteger year + seasonOffset
|
termToRational TermIdentifier{..} = toRational year + seasonOffset
|
||||||
where
|
where
|
||||||
seasonOffset
|
seasonOffset = fromIntegral (fromEnum season) % fromIntegral numSeasons
|
||||||
| Q1 <- season = 0
|
|
||||||
| Q2 <- season = 0.25
|
|
||||||
| Q3 <- season = 0.5
|
|
||||||
| Q4 <- season = 0.75
|
|
||||||
|
|
||||||
termFromRational :: Rational -> TermIdentifier
|
termFromRational :: Rational -> TermIdentifier
|
||||||
termFromRational n = TermIdentifier{..}
|
termFromRational n = TermIdentifier{..}
|
||||||
where
|
where
|
||||||
year = floor n
|
year = floor n
|
||||||
remainder = n - fromInteger (floor n)
|
remainder = n - fromInteger (floor n) -- properFraction problematic for negative year values
|
||||||
season
|
season = toEnum $ floor $ remainder * fromIntegral numSeasons
|
||||||
| remainder == 0 = Q1
|
|
||||||
| remainder == 0.25 = Q2
|
|
||||||
| remainder == 0.5 = Q3
|
|
||||||
| otherwise = Q4
|
|
||||||
|
|
||||||
instance PersistField TermIdentifier where
|
instance PersistField TermIdentifier where
|
||||||
toPersistValue = PersistRational . termToRational
|
toPersistValue = PersistRational . termToRational
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user