Remove all name clashes in Time/Helpers.hs

Summary:
Solutions were:
 - use targeted and qualified imports to avoid pulling in the universe of Duckling.Types
 - use long-form descriptive names in a few places
 - shuffle a let clause to just define an output instead of a local func

Also got rid of another lint error suggesting to use a section instead of flip;
the module is now lint-warning free.

Reviewed By: chessai

Differential Revision: D28462775

fbshipit-source-id: 1e2855756b22cb62db0d94334a7e063aa728b7bf
This commit is contained in:
Steven Troxler 2021-05-18 11:48:02 -07:00 committed by Facebook GitHub Bot
parent dd1ae664cc
commit dbc5c91263

View File

@ -38,7 +38,7 @@ import Data.Maybe
import Data.Ord (comparing) import Data.Ord (comparing)
import Data.Text (Text) import Data.Text (Text)
import Data.Tuple.Extra (both) import Data.Tuple.Extra (both)
import Prelude import Prelude hiding (pred)
import qualified Data.Time as Time import qualified Data.Time as Time
import qualified Data.Time.LocalTime.TimeZone.Series as Series import qualified Data.Time.LocalTime.TimeZone.Series as Series
@ -64,6 +64,13 @@ import Duckling.Time.Types
, AMPM(..) , AMPM(..)
) )
import Duckling.Types import Duckling.Types
( Predicate
, Token(..)
, regex
, Rule
)
import qualified Duckling.Types as Types
import qualified Duckling.Duration.Types as TDuration import qualified Duckling.Duration.Types as TDuration
import qualified Duckling.Numeral.Types as TNumeral import qualified Duckling.Numeral.Types as TNumeral
import qualified Duckling.Ordinal.Types as TOrdinal import qualified Duckling.Ordinal.Types as TOrdinal
@ -91,7 +98,7 @@ timeComputed xs = mkSeriesPredicate series
where where
series t _ = (reverse start, end) series t _ = (reverse start, end)
where where
(start, end) = span (flip TTime.timeBefore t) xs (start, end) = span (`TTime.timeBefore` t) xs
timeCycle :: TG.Grain -> TTime.Predicate timeCycle :: TG.Grain -> TTime.Predicate
timeCycle grain = mkSeriesPredicate series timeCycle grain = mkSeriesPredicate series
@ -199,16 +206,16 @@ takeNthClosest :: Int -> TTime.Predicate -> TTime.Predicate -> TTime.Predicate
takeNthClosest n cyclicPred basePred = takeNthClosest n cyclicPred basePred =
mkSeriesPredicate $! TTime.timeSeqMap False f basePred mkSeriesPredicate $! TTime.timeSeqMap False f basePred
where where
f t ctx = nth (n `max` 0) past future Nothing f t ctx = mth (n `max` 0) past future Nothing
where where
(past, future) = runPredicate cyclicPred t ctx (past, future) = runPredicate cyclicPred t ctx
nth n pa fu res against = fmap (negate . TTime.diffStartTime t)
mth m pa fu res
| n < 0 = res | n < 0 = res
| otherwise = case comparing (against t) x y of | otherwise = case comparing against x y of
GT -> nth (n-1) (tailSafe pa) fu x GT -> mth (m-1) (tailSafe pa) fu x
_ -> nth (n-1) pa (tailSafe fu) y _ -> mth (m-1) pa (tailSafe fu) y
where (x,y) = both listToMaybe (pa,fu) where (x,y) = both listToMaybe (pa,fu)
against t = fmap (negate . TTime.diffStartTime t)
tailSafe (_:xs) = xs tailSafe (_:xs) = xs
tailSafe [] = [] tailSafe [] = []
@ -551,8 +558,8 @@ predEveryNDaysFrom period given = do
return $ predEveryFrom TG.Day period date return $ predEveryFrom TG.Day period date
toTimeObjectM :: (Integer, Int, Int) -> Maybe TTime.TimeObject toTimeObjectM :: (Integer, Int, Int) -> Maybe TTime.TimeObject
toTimeObjectM (year, month, day) = do toTimeObjectM (y, m, d) = do
day <- Time.fromGregorianValid year month day day <- Time.fromGregorianValid y m d
return TTime.TimeObject return TTime.TimeObject
{ TTime.start = Time.UTCTime day 0 { TTime.start = Time.UTCTime day 0
, TTime.grain = TG.Day , TTime.grain = TG.Day
@ -698,10 +705,10 @@ tt = Just . Token Time
-- | Rule constructors -- | Rule constructors
mkSingleRegexRule :: Text -> String -> Maybe Token -> Rule mkSingleRegexRule :: Text -> String -> Maybe Token -> Rule
mkSingleRegexRule name pattern token = Rule mkSingleRegexRule name pattern token = Types.Rule
{ name = name { Types.name = name
, pattern = [regex pattern] , Types.pattern = [regex pattern]
, prod = const token , Types.prod = const token
} }
mkRuleInstants :: [(Text, TG.Grain, Int, String)] -> [Rule] mkRuleInstants :: [(Text, TG.Grain, Int, String)] -> [Rule]
@ -741,6 +748,6 @@ mkRuleHolidays = map go
mkRuleHolidays' :: [(Text, String, Maybe TimeData)] -> [Rule] mkRuleHolidays' :: [(Text, String, Maybe TimeData)] -> [Rule]
mkRuleHolidays' = map go mkRuleHolidays' = map go
where where
go (name, ptn, td) = mkSingleRegexRule name ptn $ do go (name, ptn, maybeTimeData) = mkSingleRegexRule name ptn $ do
td <- td timeData <- maybeTimeData
tt $ withHoliday name $ mkOkForThisNext td tt $ withHoliday name $ mkOkForThisNext timeData