|
| 1 | +module Data.Interval |
| 2 | + ( Interval(..) |
| 3 | + , RecurringInterval(..) |
| 4 | + , module DurationExports |
| 5 | + ) where |
| 6 | + |
| 7 | +import Prelude |
| 8 | + |
| 9 | +import Control.Extend (class Extend, extend) |
| 10 | +import Data.Bifoldable (class Bifoldable, bifoldl, bifoldr, bifoldrDefault, bifoldMapDefaultL) |
| 11 | +import Data.Bifunctor (class Bifunctor, bimap) |
| 12 | +import Data.Bitraversable (class Bitraversable, bitraverse, bisequenceDefault) |
| 13 | +import Data.Foldable (class Foldable, foldl, foldr, foldrDefault, foldMapDefaultL) |
| 14 | +import Data.Interval.Duration as DurationExports |
| 15 | +import Data.Maybe (Maybe) |
| 16 | +import Data.Traversable (class Traversable, traverse, sequenceDefault) |
| 17 | + |
| 18 | +data RecurringInterval d a = RecurringInterval (Maybe Int) (Interval d a) |
| 19 | + |
| 20 | +derive instance eqRecurringInterval :: (Eq d, Eq a) => Eq (RecurringInterval d a) |
| 21 | +derive instance ordRecurringInterval :: (Ord d, Ord a) => Ord (RecurringInterval d a) |
| 22 | +instance showRecurringInterval :: (Show d, Show a) => Show (RecurringInterval d a) where |
| 23 | + show (RecurringInterval x y) = "(RecurringInterval " <> show x <> " " <> show y <> ")" |
| 24 | + |
| 25 | +interval :: ∀ d a. RecurringInterval d a -> Interval d a |
| 26 | +interval (RecurringInterval _ i) = i |
| 27 | + |
| 28 | +over :: ∀ f d a d' a'. Functor f => (Interval d a -> f (Interval d' a')) -> RecurringInterval d a -> f (RecurringInterval d' a') |
| 29 | +over f (RecurringInterval n i) = map (RecurringInterval n) (f i) |
| 30 | + |
| 31 | +instance functorRecurringInterval :: Functor (RecurringInterval d) where |
| 32 | + map f (RecurringInterval n i) = RecurringInterval n (map f i) |
| 33 | + |
| 34 | +instance bifunctorRecurringInterval :: Bifunctor RecurringInterval where |
| 35 | + bimap f g (RecurringInterval n i) = RecurringInterval n (bimap f g i) |
| 36 | + |
| 37 | +instance foldableRecurringInterval :: Foldable (RecurringInterval d) where |
| 38 | + foldl f i = foldl f i <<< interval |
| 39 | + foldr f i = foldr f i <<< interval |
| 40 | + foldMap = foldMapDefaultL |
| 41 | + |
| 42 | +instance bifoldableRecurringInterval :: Bifoldable RecurringInterval where |
| 43 | + bifoldl f g i = bifoldl f g i <<< interval |
| 44 | + bifoldr f g i = bifoldr f g i <<< interval |
| 45 | + bifoldMap = bifoldMapDefaultL |
| 46 | + |
| 47 | +instance traversableRecurringInterval :: Traversable (RecurringInterval d) where |
| 48 | + traverse f i = traverse f `over` i |
| 49 | + sequence = sequenceDefault |
| 50 | + |
| 51 | +instance bitraversableRecurringInterval :: Bitraversable RecurringInterval where |
| 52 | + bitraverse l r i = bitraverse l r `over` i |
| 53 | + bisequence = bisequenceDefault |
| 54 | + |
| 55 | +instance extendRecurringInterval :: Extend (RecurringInterval d) where |
| 56 | + extend f a@(RecurringInterval n i) = RecurringInterval n (extend (const (f a)) i) |
| 57 | + |
| 58 | +data Interval d a |
| 59 | + = StartEnd a a |
| 60 | + | DurationEnd d a |
| 61 | + | StartDuration a d |
| 62 | + | DurationOnly d |
| 63 | + |
| 64 | +derive instance eqInterval :: (Eq d, Eq a) => Eq (Interval d a) |
| 65 | +derive instance ordInterval :: (Ord d, Ord a) => Ord (Interval d a) |
| 66 | +instance showInterval :: (Show d, Show a) => Show (Interval d a) where |
| 67 | + show (StartEnd x y) = "(StartEnd " <> show x <> " " <> show y <> ")" |
| 68 | + show (DurationEnd d x) = "(DurationEnd " <> show d <> " " <> show x <> ")" |
| 69 | + show (StartDuration x d) = "(StartDuration " <> show x <> " " <> show d <> ")" |
| 70 | + show (DurationOnly d) = "(DurationOnly " <> show d <> ")" |
| 71 | + |
| 72 | +instance functorInterval :: Functor (Interval d) where |
| 73 | + map = bimap id |
| 74 | + |
| 75 | +instance bifunctorInterval :: Bifunctor Interval where |
| 76 | + bimap _ f (StartEnd x y) = StartEnd (f x) (f y) |
| 77 | + bimap g f (DurationEnd d x) = DurationEnd (g d) (f x) |
| 78 | + bimap g f (StartDuration x d) = StartDuration (f x) (g d) |
| 79 | + bimap g _ (DurationOnly d) = DurationOnly (g d) |
| 80 | + |
| 81 | +instance foldableInterval :: Foldable (Interval d) where |
| 82 | + foldl f z (StartEnd x y) = (z `f` x) `f` y |
| 83 | + foldl f z (DurationEnd d x) = z `f` x |
| 84 | + foldl f z (StartDuration x d) = z `f` x |
| 85 | + foldl _ z _ = z |
| 86 | + foldr x = foldrDefault x |
| 87 | + foldMap = foldMapDefaultL |
| 88 | + |
| 89 | +instance bifoldableInterval :: Bifoldable Interval where |
| 90 | + bifoldl _ f z (StartEnd x y) = (z `f` x) `f` y |
| 91 | + bifoldl g f z (DurationEnd d x) = (z `g` d) `f` x |
| 92 | + bifoldl g f z (StartDuration x d) = (z `g` d) `f` x |
| 93 | + bifoldl g _ z (DurationOnly d) = z `g` d |
| 94 | + bifoldr x = bifoldrDefault x |
| 95 | + bifoldMap = bifoldMapDefaultL |
| 96 | + |
| 97 | +instance traversableInterval :: Traversable (Interval d) where |
| 98 | + traverse f (StartEnd x y) = StartEnd <$> f x <*> f y |
| 99 | + traverse f (DurationEnd d x) = f x <#> DurationEnd d |
| 100 | + traverse f (StartDuration x d) = f x <#> (_ `StartDuration` d) |
| 101 | + traverse _ (DurationOnly d) = pure (DurationOnly d) |
| 102 | + sequence = sequenceDefault |
| 103 | + |
| 104 | +instance bitraversableInterval :: Bitraversable Interval where |
| 105 | + bitraverse _ r (StartEnd x y) = StartEnd <$> r x <*> r y |
| 106 | + bitraverse l r (DurationEnd d x) = DurationEnd <$> l d <*> r x |
| 107 | + bitraverse l r (StartDuration x d) = StartDuration <$> r x <*> l d |
| 108 | + bitraverse l _ (DurationOnly d) = DurationOnly <$> l d |
| 109 | + bisequence = bisequenceDefault |
| 110 | + |
| 111 | +instance extendInterval :: Extend (Interval d) where |
| 112 | + extend f a@(StartEnd x y) = StartEnd (f a) (f a) |
| 113 | + extend f a@(DurationEnd d x) = DurationEnd d (f a) |
| 114 | + extend f a@(StartDuration x d) = StartDuration (f a) d |
| 115 | + extend f (DurationOnly d) = DurationOnly d |
0 commit comments