I have a bunch of type which holds time series data.
Like, a list of journal with dates ,a bank statement with list of transactions.
I want to make a class to annotate these type slice-able like in Python slice [:]
that would create a nightmare to implement TsList to all the types because:
there are lots of filter ... txns in common . Is there any way to factor out this common logic ?
data BoundDate = Include Date
| Exclude Date
class TsList a where
subByRange :: a -> Maybe BoundDate -> Maybe BoundDate -> a
--- with Statement type
instance TsList Statement where
subByRange s@(Statement txns) Nothing Nothing = s
subByRange s@(Statement txns) Nothing (Just ed) =
case ed of
Include d -> Statement $ filter ( x -> x <= d ) txns
Exclude d -> Statement $ filter ( x -> x < d ) txns
subByRange s@(Statement txns) (Just sd) Nothing =
case sd of
Include d -> Statement $ filter ( x -> x => d ) txns
Exclude d -> Statement $ filter ( x -> x > d ) txns
subByRange s@(Statement txns) (Just sd) (Just ed) =
Statement $ subByRange _s Nothing (Just ed)
where
_s = Statement $ subByRange s (Just sd) Nothing
--- now with Journal type
instance TsList Journal where
subByRange s@(Journal txns) Nothing Nothing = s
subByRange s@(Journal txns) Nothing (Just ed) =
case ed of
Include d -> Journal $ filter ( x -> x <= d ) txns
Exclude d -> Journal $ filter ( x -> x < d ) txns
subByRange s@(Statement txns) (Just sd) Nothing =
case sd of
Include d -> Journal $ filter ( x -> x => d ) txns
Exclude d -> Journal $ filter ( x -> x > d ) txns
subByRange s@(Statement txns) (Just sd) (Just ed) =
Journal $ subByRange _s Nothing (Just ed)
where
_s = Journal $ subByRange s (Just sd) Nothing