|
| 1 | +-- TODO commiting this temporarly as depending on my fork of datetime is |
| 2 | +-- not possibel as this module is not updated to [email protected] |
| 3 | +module Data.Interval |
| 4 | + ( Duration |
| 5 | + , Interval(..) |
| 6 | + , RecurringInterval(..) |
| 7 | + , year |
| 8 | + , month |
| 9 | + , week |
| 10 | + , day |
| 11 | + , hours |
| 12 | + , minutes |
| 13 | + , seconds |
| 14 | + , milliseconds |
| 15 | + ) where |
| 16 | + |
| 17 | +import Prelude |
| 18 | + |
| 19 | +import Data.Foldable (class Foldable, foldrDefault, foldMapDefaultL) |
| 20 | +import Data.Traversable (class Traversable, sequenceDefault) |
| 21 | +import Data.Monoid (class Monoid, mempty) |
| 22 | +import Control.Extend (class Extend) |
| 23 | + |
| 24 | +import Data.Maybe (Maybe) |
| 25 | +import Data.List (List(..), (:)) |
| 26 | +import Data.Tuple (Tuple(..)) |
| 27 | + |
| 28 | + |
| 29 | +data RecurringInterval a = RecurringInterval (Maybe Int) (Interval a) |
| 30 | + |
| 31 | +data Interval a |
| 32 | + = StartEnd a a |
| 33 | + | DurationEnd Duration a |
| 34 | + | StartDuration a Duration |
| 35 | + | JustDuration Duration |
| 36 | + |
| 37 | +instance showInterval ∷ (Show a) => Show (Interval a) where |
| 38 | + show (StartEnd x y) = "(StartEnd " <> show x <> " " <> show y <> ")" |
| 39 | + show (DurationEnd d x) = "(DurationEnd " <> show d <> " " <> show x <> ")" |
| 40 | + show (StartDuration x d) = "(StartDuration " <> show x <> " " <> show d <> ")" |
| 41 | + show (JustDuration d) = "(JustDuration " <> show d <> ")" |
| 42 | + |
| 43 | +instance functorInterval ∷ Functor Interval where |
| 44 | + map f (StartEnd x y) = StartEnd (f x) (f y ) |
| 45 | + map f (DurationEnd d x) = DurationEnd d (f x ) |
| 46 | + map f (StartDuration x d) = StartDuration (f x) d |
| 47 | + map _ (JustDuration d) = JustDuration d |
| 48 | + |
| 49 | +instance foldableInterval ∷ Foldable Interval where |
| 50 | + foldl f z (StartEnd x y) = (z `f` x) `f` y |
| 51 | + foldl f z (DurationEnd d x) = z `f` x |
| 52 | + foldl f z (StartDuration x d) = z `f` x |
| 53 | + foldl _ z _ = z |
| 54 | + foldr x = foldrDefault x |
| 55 | + foldMap = foldMapDefaultL |
| 56 | + |
| 57 | +instance traversableInterval ∷ Traversable Interval where |
| 58 | + traverse f (StartEnd x y) = StartEnd <$> f x <*> f y |
| 59 | + traverse f (DurationEnd d x) = f x <#> DurationEnd d |
| 60 | + traverse f (StartDuration x d) = f x <#> (_ `StartDuration` d) |
| 61 | + traverse _ (JustDuration d) = pure (JustDuration d) |
| 62 | + sequence = sequenceDefault |
| 63 | + |
| 64 | +instance extendInterval ∷ Extend Interval where |
| 65 | + extend f a@(StartEnd x y) = StartEnd (f a) (f a ) |
| 66 | + extend f a@(DurationEnd d x) = DurationEnd d (f a ) |
| 67 | + extend f a@(StartDuration x d) = StartDuration (f a) d |
| 68 | + extend f (JustDuration d) = JustDuration d |
| 69 | + |
| 70 | + |
| 71 | +data Duration = Duration DurationIn |
| 72 | +type DurationIn = List (Tuple DurationComponent Number) |
| 73 | + |
| 74 | +-- TODO `day 1 == hours 24` |
| 75 | +derive instance eqDuration ∷ Eq Duration |
| 76 | +instance showDuration ∷ Show Duration where |
| 77 | + show (Duration d)= "(Duration " <> show d <> ")" |
| 78 | + |
| 79 | +instance semigroupDuration ∷ Semigroup Duration where |
| 80 | + append (Duration a) (Duration b) = Duration (appendComponents a b) |
| 81 | + |
| 82 | +instance monoidDuration ∷ Monoid Duration where |
| 83 | + mempty = Duration mempty |
| 84 | + |
| 85 | +appendComponents ∷ DurationIn → DurationIn → DurationIn |
| 86 | +appendComponents Nil x = x |
| 87 | +appendComponents x Nil = x |
| 88 | +appendComponents ass@(a:as) bss@(b:bs) = case a, b of |
| 89 | + Tuple aC aV, Tuple bC bV |
| 90 | + | aC > bC → a : appendComponents as bss |
| 91 | + | aC < bC → b : appendComponents ass bs |
| 92 | + | otherwise → Tuple aC (aV + bV) : appendComponents as bs |
| 93 | + |
| 94 | +data DurationComponent = Seconds | Minutes | Hours | Day | Month | Year |
| 95 | + |
| 96 | +instance showDurationComponent ∷ Show DurationComponent where |
| 97 | + show Year = "Year" |
| 98 | + show Month = "Month" |
| 99 | + show Day = "Day" |
| 100 | + show Hours = "Hours" |
| 101 | + show Minutes = "Minutes" |
| 102 | + show Seconds = "Seconds" |
| 103 | + |
| 104 | +derive instance eqDurationComponent ∷ Eq DurationComponent |
| 105 | +derive instance ordDurationComponent ∷ Ord DurationComponent |
| 106 | + |
| 107 | + |
| 108 | +week ∷ Number → Duration |
| 109 | +week = durationFromComponent Day <<< (_ * 7.0) |
| 110 | + |
| 111 | +year ∷ Number → Duration |
| 112 | +year = durationFromComponent Year |
| 113 | + |
| 114 | +month ∷ Number → Duration |
| 115 | +month = durationFromComponent Month |
| 116 | + |
| 117 | +day ∷ Number → Duration |
| 118 | +day = durationFromComponent Day |
| 119 | + |
| 120 | +hours ∷ Number → Duration |
| 121 | +hours = durationFromComponent Hours |
| 122 | + |
| 123 | +minutes ∷ Number → Duration |
| 124 | +minutes = durationFromComponent Minutes |
| 125 | + |
| 126 | +seconds ∷ Number → Duration |
| 127 | +seconds = durationFromComponent Seconds |
| 128 | + |
| 129 | +milliseconds ∷ Number → Duration |
| 130 | +milliseconds = durationFromComponent Seconds <<< (_ / 1000.0) |
| 131 | + |
| 132 | +durationFromComponent ∷ DurationComponent → Number → Duration |
| 133 | +durationFromComponent c 0.0 = mempty |
| 134 | +durationFromComponent c n = Duration $ pure $ Tuple c n |
0 commit comments