module Music.Time.Past (
Past(..),
Future(..),
past,
future,
indexPast,
firstTrue,
pastSeg,
futureSeg,
) where
import Control.Lens
import Control.Applicative
import Control.Comonad
import Data.Functor.Couple
import Data.Ord (comparing)
import Data.List (takeWhile, sort, sortBy, group)
import Data.List.Ordered
import Data.Maybe
import Data.Semigroup
import Control.Monad
import Music.Time.Behavior
import Music.Time.Reverse
import Music.Time.Segment
import Music.Time.Split
newtype Past a = Past { getPast :: (Min (Maybe Time), a) }
deriving (Eq, Ord, Functor)
newtype Future a = Future { getFuture :: (Max (Maybe Time), a) }
deriving (Eq, Ord, Functor)
past :: Past a -> Time -> Maybe a
past (Past (extract -> t, x)) t'
| Just t' <= t = Just x
| otherwise = Nothing
future :: Future a -> Time -> Maybe a
future (Future (extract -> t, x)) t'
| Just t' >= t = Just x
| otherwise = Nothing
indexPast :: [Past a] -> Time -> Maybe a
indexPast ps t = firstTrue $ fmap (\p -> past p t) $ sortBy (comparing tv) ps
where
tv (Past (Min t, _)) = t
firstTrue :: [Maybe a] -> Maybe a
firstTrue = listToMaybe . join . fmap maybeToList
pastSeg :: Past (Segment a) -> Behavior (Maybe a)
pastSeg = undefined
futureSeg :: Future (Segment a) -> Behavior (Maybe a)
futureSeg = undefined