List: add instance Splittable Seq

Add an instance of Splittable for Data.Sequence.Seq, and QuickCheck
properties for it.

Add lower bound containers >= 0.5.7, being the version at which the
Semigroup instance was introduced.
This commit is contained in:
Fraser Tweedale 2018-12-08 10:02:29 +10:00
parent 8db170a02b
commit f23b6b1dba
3 changed files with 18 additions and 1 deletions

View File

@ -94,7 +94,7 @@ library
directory >= 1.2.5.0,
dlist,
filepath,
containers,
containers >= 0.5.7,
microlens >= 0.3.0.0,
microlens-th,
microlens-mtl,

View File

@ -79,6 +79,7 @@ import Data.Foldable (find, toList)
import Data.List.NonEmpty (NonEmpty((:|)))
import Data.Maybe (fromMaybe)
import Data.Semigroup (Semigroup, (<>), sconcat)
import qualified Data.Sequence as Seq
import Graphics.Vty (Event(..), Key(..), Modifier(..))
import qualified Data.Vector as V
import GHC.Generics (Generic)
@ -140,6 +141,11 @@ class Splittable t where
-- | Slice the structure. Equivalent to @(take n . drop i) xs@,
-- therefore total.
--
-- The default implementation applies 'splitAt' two times: first
-- to drop elements leading up to the slice, and again to drop
-- elements after the slice.
--
slice :: Int {- ^ start index -} -> Int {- ^ length -} -> t a -> t a
slice i n = fst . splitAt n . snd . splitAt i
@ -147,6 +153,10 @@ class Splittable t where
instance Splittable V.Vector where
splitAt = V.splitAt
-- | /O(log(min(i,n-i)))/ 'splitAt'.
instance Splittable Seq.Seq where
splitAt = Seq.splitAt
handleListEvent
:: (Foldable t, Splittable t, Ord n)

View File

@ -14,6 +14,7 @@ import Data.Maybe (isNothing)
import Data.Monoid (Endo(..))
import Data.Semigroup (Semigroup((<>)))
import qualified Data.Sequence as Seq
import qualified Data.Vector as V
import Lens.Micro
import Test.QuickCheck
@ -272,6 +273,12 @@ prop_splitAtLength_Vector = splitAtLength . V.fromList
prop_splitAtAppend_Vector :: (Eq a) => [a] -> Int -> Bool
prop_splitAtAppend_Vector = splitAtAppend . V.fromList
prop_splitAtLength_Seq :: [a] -> Int -> Bool
prop_splitAtLength_Seq = splitAtLength . Seq.fromList
prop_splitAtAppend_Seq :: (Eq a) => [a] -> Int -> Bool
prop_splitAtAppend_Seq = splitAtAppend . Seq.fromList
return []