module EventDataTheory.Utilities
( Predicate(..)
, containsTag
, findOccurrenceOfEvent
, firstOccurrenceOfTag
, lastOccurrenceOfTag
, splitByTags
, filterEvents
, tallyEvents
) where
import Data.Foldable (length, toList)
import Data.Functor.Contravariant (Predicate (..))
import Data.Semigroup
import EventDataTheory.Core
import IntervalAlgebra
import Safe (headMay, lastMay)
import qualified Witherable as W
containsTag :: (Ord t) => [t] -> Predicate (Event t m a)
containsTag :: forall t m a. Ord t => [t] -> Predicate (Event t m a)
containsTag [t]
tList = forall a. (a -> Bool) -> Predicate a
Predicate (forall a t. HasTag a t => a -> [t] -> Bool
`hasAnyTag` [t]
tList)
filterEvents
:: (W.Filterable f)
=> Predicate (Event t m a)
-> f (Event t m a)
-> f (Event t m a)
filterEvents :: forall (f :: * -> *) t m a.
Filterable f =>
Predicate (Event t m a) -> f (Event t m a) -> f (Event t m a)
filterEvents Predicate (Event t m a)
p = forall (f :: * -> *) a. Filterable f => (a -> Bool) -> f a -> f a
W.filter (forall a. Predicate a -> a -> Bool
getPredicate Predicate (Event t m a)
p)
findOccurrenceOfEvent
:: (W.Filterable f)
=> (f (Event t m a) -> Maybe (Event t m a))
-> Predicate (Event t m a)
-> f (Event t m a)
-> Maybe (Event t m a)
findOccurrenceOfEvent :: forall (f :: * -> *) t m a.
Filterable f =>
(f (Event t m a) -> Maybe (Event t m a))
-> Predicate (Event t m a)
-> f (Event t m a)
-> Maybe (Event t m a)
findOccurrenceOfEvent f (Event t m a) -> Maybe (Event t m a)
f Predicate (Event t m a)
p = f (Event t m a) -> Maybe (Event t m a)
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) t m a.
Filterable f =>
Predicate (Event t m a) -> f (Event t m a) -> f (Event t m a)
filterEvents Predicate (Event t m a)
p
firstOccurrenceOfTag
:: (W.Witherable f, Ord t) => [t] -> f (Event t m a) -> Maybe (Event t m a)
firstOccurrenceOfTag :: forall (f :: * -> *) t m a.
(Witherable f, Ord t) =>
[t] -> f (Event t m a) -> Maybe (Event t m a)
firstOccurrenceOfTag [t]
x =
forall (f :: * -> *) t m a.
Filterable f =>
(f (Event t m a) -> Maybe (Event t m a))
-> Predicate (Event t m a)
-> f (Event t m a)
-> Maybe (Event t m a)
findOccurrenceOfEvent (forall a. [a] -> Maybe a
headMay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList) (forall t m a. Ord t => [t] -> Predicate (Event t m a)
containsTag [t]
x)
lastOccurrenceOfTag
:: (W.Witherable f, Ord t) => [t] -> f (Event t m a) -> Maybe (Event t m a)
lastOccurrenceOfTag :: forall (f :: * -> *) t m a.
(Witherable f, Ord t) =>
[t] -> f (Event t m a) -> Maybe (Event t m a)
lastOccurrenceOfTag [t]
x =
forall (f :: * -> *) t m a.
Filterable f =>
(f (Event t m a) -> Maybe (Event t m a))
-> Predicate (Event t m a)
-> f (Event t m a)
-> Maybe (Event t m a)
findOccurrenceOfEvent (forall a. [a] -> Maybe a
lastMay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList) (forall t m a. Ord t => [t] -> Predicate (Event t m a)
containsTag [t]
x)
splitByTags
:: (W.Filterable f, Ord t)
=> [t]
-> [t]
-> f (Event t m a)
-> (f (Event t m a), f (Event t m a))
splitByTags :: forall (f :: * -> *) t m a.
(Filterable f, Ord t) =>
[t] -> [t] -> f (Event t m a) -> (f (Event t m a), f (Event t m a))
splitByTags [t]
t1 [t]
t2 f (Event t m a)
es =
(forall (f :: * -> *) t m a.
Filterable f =>
Predicate (Event t m a) -> f (Event t m a) -> f (Event t m a)
filterEvents (forall t m a. Ord t => [t] -> Predicate (Event t m a)
containsTag [t]
t1) f (Event t m a)
es, forall (f :: * -> *) t m a.
Filterable f =>
Predicate (Event t m a) -> f (Event t m a) -> f (Event t m a)
filterEvents (forall t m a. Ord t => [t] -> Predicate (Event t m a)
containsTag [t]
t2) f (Event t m a)
es)
tallyEvents
:: (W.Witherable f) => Predicate (Event t m a) -> f (Event t m a) -> Int
tallyEvents :: forall (f :: * -> *) t m a.
Witherable f =>
Predicate (Event t m a) -> f (Event t m a) -> Int
tallyEvents Predicate (Event t m a)
p = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) t m a.
Filterable f =>
Predicate (Event t m a) -> f (Event t m a) -> f (Event t m a)
filterEvents Predicate (Event t m a)
p