classy-prelude 0.5.7 → 0.5.8
raw patch · 4 files changed
+205/−1 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ ClassyPrelude: asUVector :: UVector a -> UVector a
Files
- ClassyPrelude.hs +5/−0
- ClassyPrelude/UVector.hs +185/−0
- classy-prelude.cabal +2/−1
- test/main.hs +13/−0
ClassyPrelude.hs view
@@ -134,6 +134,7 @@ , asMaybe , asSet , asVector+ , asUVector , asIOException , asSomeException ) where@@ -165,6 +166,7 @@ import ClassyPrelude.Set () import ClassyPrelude.Text () import ClassyPrelude.Vector ()+import ClassyPrelude.UVector () import ClassyPrelude.Sequence (Seq) @@ -247,6 +249,9 @@ asVector :: Vector a -> Vector a asVector = id++asUVector :: UVector a -> UVector a+asUVector = id forM :: CanMapM ci mco m i o => ci -> (i -> m o) -> mco forM = flip mapM
+ ClassyPrelude/UVector.hs view
@@ -0,0 +1,185 @@+{-# LANGUAGE NoImplicitPrelude #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE TypeFamilies #-}+module ClassyPrelude.UVector+ ( UVector+ ) where++import CorePrelude+import ClassyPrelude.Classes+import qualified Data.Vector.Unboxed as UVector++instance (Unbox a, Unbox b) => CanMap (UVector a) (UVector b) a b where+ map = UVector.map++instance (Unbox a, Unbox b) => CanConcatMap (UVector a) (UVector b) a (UVector b) where+ concatMap = UVector.concatMap++instance (Unbox a) => CanFilter (UVector a) a where+ filter = UVector.filter++instance (Unbox a) => CanFilterM (UVector a) a where+ filterM = UVector.filterM++instance (Unbox a) => CanLength (UVector a) Int where+ length = UVector.length++instance (Unbox a) => CanSingleton (UVector a) a where+ singleton = UVector.singleton++instance (Unbox a) => CanNull (UVector a) where+ null = UVector.null++instance (Unbox a) => CanPack (UVector a) a where+ pack = UVector.fromList+ unpack = UVector.toList++instance (Unbox a) => CanIntersperse (UVector a) a where+ -- | Implementation is a rip off from <http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-List.html#intersperse>.+ intersperse _ xs | null xs = UVector.empty+ intersperse sep xs = UVector.cons (UVector.head xs) $ prependToAll sep $ UVector.unsafeTail xs+ where+ prependToAll _ xs | null xs = UVector.empty+ prependToAll sep xs = UVector.cons sep $ UVector.cons (UVector.head xs) $ prependToAll sep $ UVector.unsafeTail xs++instance (Unbox a, Eq a) => CanStripPrefix (UVector a) where+ stripPrefix x y+ | x == y1 = Just y2+ | otherwise = Nothing+ where+ (y1, y2) = UVector.splitAt (UVector.length x) y+ isPrefixOf x y = UVector.take (UVector.length x) y == x++instance (Unbox i, Unbox o, Monad m) => CanMapM (UVector i) (m (UVector o)) m i o where+ mapM = UVector.mapM++instance (Unbox a) => CanMapM_ (UVector a) a where+ mapM_ = UVector.mapM_++instance (Unbox x, Eq x) => CanMember (UVector x) x where+ member x = UVector.any (== x)++instance (Unbox a) => CanBreak (UVector a) a where+ break = UVector.break+ span = UVector.span+ dropWhile = UVector.dropWhile+ takeWhile = UVector.takeWhile++instance (Unbox a) => CanAny (UVector a) a where+ any = UVector.any+ all = UVector.all++instance (Unbox a) => CanSplitAt (UVector a) Int where+ splitAt = UVector.splitAt++instance (Unbox a) => CanFold (UVector a) a accum where+ fold = UVector.foldl'++instance (Unbox a) => CanReverse (UVector a) where+ reverse = UVector.reverse++instance (Unbox a) => CanReplicate (UVector a) a Int where+ replicate = UVector.replicate++instance (Unbox a) => CanReplicateM (UVector a) a Int where+ replicateM = UVector.replicateM++instance (Unbox a) => CanFind (UVector a) a where+ find = UVector.find+ +instance (Unbox m, Monoid m) => CanConcat (UVector m) m where+ concat = UVector.foldr mappend mempty++instance (Unbox a) => CanPartition (UVector a) a where+ partition = UVector.partition++instance (Unbox a) => CanCons (UVector a) a where+ cons = UVector.cons++instance (Unbox a) => CanUncons (UVector a) a where+ uncons v = if null v then Nothing else Just (UVector.unsafeHead v, UVector.unsafeTail v)++instance (Unbox a) => CanGroupBy (UVector a) a where+ -- | Implementation is stolen from <http://hackage.haskell.org/packages/archive/storablevector/latest/doc/html/src/Data-StorableVector.html#groupBy>+ groupBy k xs =+ switchL []+ (\h t ->+ let n = 1 + findIndexOrEnd (not . k h) t+ in UVector.unsafeTake n xs : groupBy k (UVector.unsafeDrop n xs))+ xs++instance (Unbox a, Eq a) => CanGroup (UVector a) a where+ -- | A special case of 'groupBy', which is about 40% faster than + -- /groupBy (==)/.+ -- + -- Implementation is stolen from <http://hackage.haskell.org/packages/archive/storablevector/latest/doc/html/src/Data-StorableVector.html#groupBy>+ group xs =+ switchL []+ (\h _ ->+ let (ys, zs) = span (== h) xs+ in ys : group zs)+ xs++-- Helper functions stolen from <http://hackage.haskell.org/packages/archive/storablevector/latest/doc/html/src/Data-StorableVector.html#groupBy>++-- | 'findIndexOrEnd' is a variant of findIndex, that returns the length+-- of the string if no element is found, rather than Nothing.+findIndexOrEnd p xs =+ UVector.foldr+ (\x k n ->+ if p x then n else k (succ n))+ id xs 0+switchL n j x =+ if null x+ then n+ else j (UVector.unsafeHead x) (UVector.unsafeTail x)+++instance (Unbox a, Unbox b, Unbox c) => CanZipWith (UVector a) a (UVector b) b (UVector c) c where+ zipWith = UVector.zipWith++instance (Unbox a, Unbox b, Unbox c, Unbox d) => CanZipWith3 (UVector a) a (UVector b) b (UVector c) c (UVector d) d where+ zipWith3 = UVector.zipWith3++instance (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => CanZipWith4 (UVector a) a (UVector b) b (UVector c) c (UVector d) d (UVector e) e where+ zipWith4 = UVector.zipWith4++instance (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => CanZipWith5 (UVector a) a (UVector b) b (UVector c) c (UVector d) d (UVector e) e (UVector f) f where+ zipWith5 = UVector.zipWith5++instance (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f, Unbox g) => CanZipWith6 (UVector a) a (UVector b) b (UVector c) c (UVector d) d (UVector e) e (UVector f) f (UVector g) g where+ zipWith6 = UVector.zipWith6++instance (Unbox a, Unbox b) => CanZip (UVector a) a (UVector b) b UVector where+ zip = UVector.zip++instance (Unbox a, Unbox b, Unbox c) => CanZip3 (UVector a) a (UVector b) b (UVector c) c UVector where+ zip3 = UVector.zip3++instance (Unbox a, Unbox b, Unbox c, Unbox d) => CanZip4 (UVector a) a (UVector b) b (UVector c) c (UVector d) d UVector where+ zip4 = UVector.zip4++instance (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => CanZip5 (UVector a) a (UVector b) b (UVector c) c (UVector d) d (UVector e) e UVector where+ zip5 = UVector.zip5++instance (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => CanZip6 (UVector a) a (UVector b) b (UVector c) c (UVector d) d (UVector e) e (UVector f) f UVector where+ zip6 = UVector.zip6++instance (Unbox a, Unbox b) => CanUnzip (UVector a) a (UVector b) b UVector where+ unzip = UVector.unzip++instance (Unbox a, Unbox b, Unbox c) => CanUnzip3 (UVector a) a (UVector b) b (UVector c) c UVector where+ unzip3 = UVector.unzip3++instance (Unbox a, Unbox b, Unbox c, Unbox d) => CanUnzip4 (UVector a) a (UVector b) b (UVector c) c (UVector d) d UVector where+ unzip4 = UVector.unzip4++instance (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => CanUnzip5 (UVector a) a (UVector b) b (UVector c) c (UVector d) d (UVector e) e UVector where+ unzip5 = UVector.unzip5++instance (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => CanUnzip6 (UVector a) a (UVector b) b (UVector c) c (UVector d) d (UVector e) e (UVector f) f UVector where+ unzip6 = UVector.unzip6++instance (Unbox a) => CanEmpty (UVector a) where+ empty = UVector.empty
classy-prelude.cabal view
@@ -1,5 +1,5 @@ name: classy-prelude-version: 0.5.7+version: 0.5.8 synopsis: A typeclass-based Prelude. description: Focuses on using common typeclasses when possible, and creating new ones to avoid name clashing. Exposes many recommended datastructures (Map, ByteString, etc) directly without requiring long import lists and qualified modules. homepage: https://github.com/snoyberg/classy-prelude@@ -24,6 +24,7 @@ ClassyPrelude.Set ClassyPrelude.FilePath ClassyPrelude.Vector+ ClassyPrelude.UVector ClassyPrelude.HashMap ClassyPrelude.HashSet ClassyPrelude.Char
test/main.hs view
@@ -269,6 +269,7 @@ describe "map" $ do describe "list" $ mapProps (undefined :: [Int]) (+ 1) (+ 2) describe "Data.Vector" $ mapProps (undefined :: Vector Int) (+ 1) (+ 2)+ describe "Data.Vector.Unboxed" $ mapProps (undefined :: UVector Int) (+ 1) (+ 2) describe "Data.Set" $ mapProps (undefined :: Set Int) (+ 1) (+ 2) describe "Data.HashSet" $ mapProps (undefined :: HashSet Int) (+ 1) (+ 2) describe "Data.ByteString" $ mapProps (undefined :: ByteString) (+ 1) (+ 2)@@ -279,6 +280,7 @@ describe "concatMap" $ do describe "list" $ concatMapProps (undefined :: [Int]) (\i -> [i + 1, i + 2]) describe "Data.Vector" $ concatMapProps (undefined :: Vector Int) (\i -> fromList [i + 1, i + 2])+ describe "Data.Vector.Unboxed" $ concatMapProps (undefined :: UVector Int) (\i -> fromList [i + 1, i + 2]) describe "Data.ByteString" $ concatMapProps (undefined :: ByteString) (\i -> fromList [i + 1, i + 2]) describe "Data.ByteString.Lazy" $ concatMapProps (undefined :: LByteString) (\i -> fromList [i + 1, i + 2]) describe "Data.Text" $ concatMapProps (undefined :: Text) (\c -> pack [succ c, succ $ succ c])@@ -287,6 +289,7 @@ describe "filter" $ do describe "list" $ filterProps (undefined :: [Int]) (< 20) describe "Data.Vector" $ filterProps (undefined :: Vector Int) (< 20)+ describe "Data.Vector.Unboxed" $ filterProps (undefined :: UVector Int) (< 20) describe "Data.ByteString" $ filterProps (undefined :: ByteString) (< 20) describe "Data.ByteString.Lazy" $ filterProps (undefined :: LByteString) (< 20) describe "Data.Text" $ filterProps (undefined :: Text) (< 'A')@@ -298,10 +301,12 @@ describe "filterM" $ do describe "list" $ filterMProps (undefined :: [Int]) (< 20) describe "Data.Vector" $ filterMProps (undefined :: Vector Int) (< 20)+ describe "Data.Vector.Unboxed" $ filterMProps (undefined :: Vector Int) (< 20) describe "Data.Sequence" $ filterMProps (undefined :: Seq Int) (< 20) describe "length" $ do describe "list" $ lengthProps (undefined :: [Int]) describe "Data.Vector" $ lengthProps (undefined :: Vector Int)+ describe "Data.Vector.Unboxed" $ lengthProps (undefined :: UVector Int) describe "Data.ByteString" $ lengthProps (undefined :: ByteString) describe "Data.ByteString.Lazy" $ lengthProps (undefined :: LByteString) describe "Data.Text" $ lengthProps (undefined :: Text)@@ -314,10 +319,12 @@ describe "mapM" $ do describe "list" $ mapMProps (undefined :: [Int]) describe "Data.Vector" $ mapMProps (undefined :: Vector Int)+ describe "Data.Vector.Unboxed" $ mapMProps (undefined :: UVector Int) describe "Seq" $ mapMProps (undefined :: Seq Int) describe "mapM_" $ do describe "list" $ mapM_Props (undefined :: [Int]) describe "Data.Vector" $ mapM_Props (undefined :: Vector Int)+ describe "Data.Vector.Unboxed" $ mapM_Props (undefined :: UVector Int) describe "Set" $ mapM_Props (undefined :: Set Int) describe "HashSet" $ mapM_Props (undefined :: HashSet Int) describe "Seq" $ mapM_Props (undefined :: Seq Int)@@ -325,6 +332,7 @@ let f = flip (:) describe "list" $ foldProps (undefined :: [Int]) f [] describe "Data.Vector" $ foldProps (undefined :: Vector Int) f []+ describe "Data.Vector.Unboxed" $ foldProps (undefined :: UVector Int) f [] describe "Data.ByteString" $ foldProps (undefined :: ByteString) f [] describe "Data.ByteString.Lazy" $ foldProps (undefined :: LByteString) f [] describe "Data.Text" $ foldProps (undefined :: Text) f []@@ -335,6 +343,7 @@ describe "replicate" $ do describe "list" $ replicateProps (undefined :: [Int]) pack describe "Data.Vector" $ replicateProps (undefined :: Vector Int) pack+ describe "Data.Vector.Unboxed" $ replicateProps (undefined :: UVector Int) pack describe "Data.ByteString" $ replicateProps (undefined :: ByteString) pack describe "Data.ByteString.Lazy" $ replicateProps (undefined :: LByteString) pack describe "Data.Text" $ replicateProps (undefined :: Text) concat@@ -352,6 +361,7 @@ describe "replicateM" $ do describe "list" $ replicateMProps (undefined :: [Int]) describe "Vector" $ replicateMProps (undefined :: Vector Int)+ describe "UVector" $ replicateMProps (undefined :: UVector Int) describe "Seq" $ replicateMProps (undefined :: Seq Int) describe "encode/decode UTF8" $ do describe "Text" $ utf8Props (undefined :: Text)@@ -367,6 +377,7 @@ describe "ByteString" $ prefixProps (undefined :: ByteString) describe "LByteString" $ prefixProps (undefined :: LByteString) describe "Vector" $ prefixProps (undefined :: Vector Int)+ describe "UVector" $ prefixProps (undefined :: UVector Int) describe "Seq" $ prefixProps (undefined :: Seq Int) instance Arbitrary (Map Int Char) where@@ -374,6 +385,8 @@ instance Arbitrary (HashMap Int Char) where arbitrary = fromList <$> arbitrary instance Arbitrary (Vector Int) where+ arbitrary = fromList <$> arbitrary+instance Arbitrary (UVector Int) where arbitrary = fromList <$> arbitrary instance Arbitrary (Set Int) where arbitrary = fromList <$> arbitrary