module Music.Time.Past (
Past(..),
Future(..),
past,
future,
indexPast,
firstTrue,
pastSeg,
futureSeg,
) where
import Control.Applicative
import Control.Comonad
import Control.Lens
import Control.Monad
import Data.Functor.Couple
import Data.List (group, sort, sortBy, takeWhile)
import Data.List.Ordered
import Data.Maybe
import Data.Ord (comparing)
import Data.Semigroup
import Music.Time.Behavior
import Music.Time.Juxtapose
import Music.Time.Segment
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