interval-algebra 2.0.1 → 2.0.2
raw patch · 4 files changed
+46/−3 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ IntervalAlgebra.IntervalUtilities: rangeInterval :: (Ord a, Foldable t) => t (Interval a) -> Maybe (Interval a)
Files
- ChangeLog.md +4/−0
- interval-algebra.cabal +1/−1
- src/IntervalAlgebra/IntervalUtilities.hs +21/−1
- test/IntervalAlgebra/IntervalUtilitiesSpec.hs +20/−1
ChangeLog.md view
@@ -1,5 +1,9 @@ # Changelog for interval-algebra +## 2.0.2++* Adds `rangeInterval`, which creates the smallest inverval containing all intervals in a `Foldable`.+ ## 2.0.1 * Relaxes cabal package bounds; notably:
interval-algebra.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: interval-algebra-version: 2.0.1+version: 2.0.2 synopsis: An implementation of Allen's interval algebra for temporal logic description: Please see the README on GitHub at <https://github.com/novisci/interval-algebra> category: Algebra,Time
src/IntervalAlgebra/IntervalUtilities.hs view
@@ -13,12 +13,14 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TupleSections #-} + module IntervalAlgebra.IntervalUtilities ( -- * Fold over sequential intervals combineIntervals , combineIntervalsL+ , rangeInterval , gaps , gapsL , gapsWithin@@ -74,8 +76,10 @@ , durations ) where -import safe Control.Applicative ( (<*>)+import safe Control.Applicative ( (<$>)+ , (<*>) , Applicative(pure)+ , liftA2 ) import qualified Control.Foldl as L import safe Control.Monad ( Functor(fmap) )@@ -486,6 +490,21 @@ combineIntervalsL l = unBox $ foldl' (<>) (Box []) (packIntervalBoxes l) {-# INLINABLE combineIntervalsL #-} +-- |+-- Maybe form an @Interval a@ from @Control.Foldl t => t (Interval a)@ spanning+-- the range of all intervals in the list, i.e. whose @begin@ is the minimum+-- of @begin@ across intervals in the list and whose @end@ is the maximum of+-- @end@. +--+-- >>> rangeInterval [beginerval 0 0, beginerval 0 (-1)]+-- Just (-1, 1)+-- >>> rangeInterval ([] :: [Interval Int])+-- Nothing+-- >>> rangeInterval (Just (beginerval 0 0))+-- Just (0, 1)+rangeInterval :: (Ord a, L.Foldable t) => t (Interval a) -> Maybe (Interval a)+rangeInterval = L.fold (liftA2 extenterval <$> L.minimum <*> L.maximum)+ -- Internal function for combining maybe intervals in the 'combineIntervalsL' -- function (<->) :: (IntervalCombinable i a) => Maybe (i a) -> Maybe (i a) -> [Interval a]@@ -770,3 +789,4 @@ hasEqData :: (Eq b) => [PairedInterval b a] -> Bool hasEqData x = or (L.fold (makeFolder (==)) (fmap getPairData x) :: [Bool])+
test/IntervalAlgebra/IntervalUtilitiesSpec.hs view
@@ -35,6 +35,7 @@ , intervalRelations , moment , predicate+ , rangeInterval , safeInterval , starts , strictWithinRelations@@ -620,7 +621,25 @@ it "intersection of (0, 3) (1, 2) should be Just (1, 2)" $ intersect (iv 3 0) (iv 1 1) `shouldBe` Just (iv 1 1)-+ describe "rangeInterval unit tests" $ do+ it "range of empty list returns Nothing"+ $ rangeInterval ([] :: [Interval Int])+ `shouldBe` Nothing+ it "rangeInterval returns the containing interval"+ $ rangeInterval [beginerval 0 (1 :: Int), beginerval 3 (-1)]+ `shouldBe` (Just $ beginerval 3 (-1))+ it "disjoint intervals"+ $ rangeInterval [beginerval 10 (1 :: Int), beginerval 1 (-1)]+ `shouldBe` (Just $ beginerval 12 (-1))+ it "order of list does not matter"+ $ rangeInterval [beginerval 10 (1 :: Int), beginerval 1 (-1)]+ `shouldBe` rangeInterval [beginerval 1 (-1), beginerval 10 (1 :: Int)]+ it "works on Right"+ $ rangeInterval (Right $ beginerval 10 (1 :: Int))+ `shouldBe` (Just $ beginerval 10 (1 :: Int))+ it "Left variant returns Nothing"+ $ rangeInterval (Left $ beginerval 10 (1 :: Int))+ `shouldBe` (Nothing :: Maybe (Interval Int)) describe "combineIntervals unit tests" $ do it "noncontainmentInt combined into containmentInt" $ combineIntervals [containmentInt, noncontainmentInt]