mono-traversable 0.4.0 → 0.4.0.1
raw patch · 3 files changed
+41/−7 lines, 3 filesdep +vector-algorithms
Dependencies added: vector-algorithms
Files
- mono-traversable.cabal +2/−1
- src/Data/Sequences.hs +19/−6
- test/Spec.hs +20/−0
mono-traversable.cabal view
@@ -1,5 +1,5 @@ name: mono-traversable-version: 0.4.0+version: 0.4.0.1 synopsis: Type classes for mapping, folding, and traversing monomorphic containers description: Monomorphic variants of the Functor, Foldable, and Traversable typeclasses. Contains even more experimental code for abstracting containers and sequences. homepage: https://github.com/snoyberg/mono-traversable@@ -32,6 +32,7 @@ , semigroupoids >=3.0 , comonad >=3.0.3 , vector-instances+ , vector-algorithms >= 0.6 hs-source-dirs: src default-language: Haskell2010
src/Data/Sequences.hs view
@@ -29,6 +29,8 @@ import qualified Data.ByteString.Unsafe as SU import Data.GrowingAppend import Data.Vector.Instances ()+import qualified Data.Vector.Generic as VG+import qualified Data.Vector.Algorithms.Merge as VAM -- | 'SemiSequence' was created to share code between 'IsSequence' and 'NonNull'. -- You should always use 'IsSequence' or 'NonNull' rather than using 'SemiSequence'@@ -199,6 +201,14 @@ defaultSortBy f = fromList . List.sortBy f . otoList {-# INLINE defaultSortBy #-} +vectorSortBy :: VG.Vector v e => (e -> e -> Ordering) -> v e -> v e+vectorSortBy f = VG.modify (VAM.sortBy f)+{-# INLINE vectorSortBy #-}++vectorSort :: (VG.Vector v e, Ord e) => v e -> v e+vectorSort = VG.modify VAM.sort+{-# INLINE vectorSort #-}+ defaultCons :: IsSequence seq => Element seq -> seq -> seq defaultCons e = fromList . (e:) . otoList {-# INLINE defaultCons #-}@@ -629,7 +639,7 @@ cons = V.cons snoc = V.snoc - sortBy = defaultSortBy+ sortBy = vectorSortBy intersperse = defaultIntersperse {-# INLINE intersperse #-} {-# INLINE reverse #-}@@ -700,7 +710,7 @@ find = U.find cons = U.cons snoc = U.snoc- sortBy = defaultSortBy+ sortBy = vectorSortBy {-# INLINE intersperse #-} {-# INLINE reverse #-} {-# INLINE find #-}@@ -770,7 +780,7 @@ snoc = VS.snoc intersperse = defaultIntersperse- sortBy = defaultSortBy+ sortBy = vectorSortBy {-# INLINE intersperse #-} {-# INLINE reverse #-} {-# INLINE find #-}@@ -994,13 +1004,16 @@ instance Ord a => OrdSequence (Seq.Seq a) instance Ord a => OrdSequence (V.Vector a) where- -- FIXME more efficient sort+ sort = vectorSort+ {-# INLINE sort #-} instance (Ord a, U.Unbox a) => OrdSequence (U.Vector a) where- -- FIXME more efficient sort+ sort = vectorSort+ {-# INLINE sort #-} instance (Ord a, VS.Storable a) => OrdSequence (VS.Vector a) where- -- FIXME more efficient sort+ sort = vectorSort+ {-# INLINE sort #-} class (IsSequence t, IsString t, Element t ~ Char) => Textual t where words :: t -> [t]
test/Spec.hs view
@@ -285,3 +285,23 @@ let x1 = Foldl.fold Foldl.length (xs :: [Int]) x2 = Foldl.purely ofoldlUnwrap Foldl.length xs x2 `shouldBe` x1++ describe "sorting" $ do+ let test typ dummy = describe typ $ do+ prop "sortBy" $ \input -> do+ let orig = fromList input `asTypeOf` dummy+ f x y = compare y x+ fromList (sortBy f input) `shouldBe` sortBy f orig+ fromList input `shouldBe` orig+ prop "sort" $ \input -> do+ let orig = fromList input `asTypeOf` dummy+ fromList (sort input) `shouldBe` sort orig+ fromList input `shouldBe` orig+ test "list" ([] :: [Int])+ test "vector" (V.empty :: V.Vector Int)+ test "storable vector" (VS.empty :: VS.Vector Int)+ test "unboxed vector" (U.empty :: U.Vector Int)+ test "strict ByteString" S.empty+ test "lazy ByteString" L.empty+ test "strict Text" T.empty+ test "lazy Text" TL.empty