fixed-vector 0.4.1.0 → 0.4.2.0
raw patch · 5 files changed
+137/−34 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Vector.Fixed: Only :: a -> Only a
+ Data.Vector.Fixed: fromFoldable :: (Vector v a, Foldable f) => f a -> Maybe (v a)
+ Data.Vector.Fixed: fromList' :: Vector v a => [a] -> v a
+ Data.Vector.Fixed: fromListM :: Vector v a => [a] -> Maybe (v a)
+ Data.Vector.Fixed: instance Data a => Data (Only a)
+ Data.Vector.Fixed: instance Eq a => Eq (Only a)
+ Data.Vector.Fixed: instance Foldable Only
+ Data.Vector.Fixed: instance Functor Only
+ Data.Vector.Fixed: instance Ord a => Ord (Only a)
+ Data.Vector.Fixed: instance Show a => Show (Only a)
+ Data.Vector.Fixed: instance Traversable Only
+ Data.Vector.Fixed: instance Typeable1 Only
+ Data.Vector.Fixed: instance Vector Only a
+ Data.Vector.Fixed: newtype Only a
+ Data.Vector.Fixed.Cont: fromList' :: Arity n => [a] -> ContVecT m n a
+ Data.Vector.Fixed.Cont: fromListM :: Arity n => [a] -> ContVecT Maybe n a
+ Data.Vector.Fixed.Internal.Arity: applyFun :: Arity n => (forall k. t (S k) -> (a, t k)) -> t n -> Fn n a b -> (b, t Z)
+ Data.Vector.Fixed.Internal.Arity: applyFunM :: (Arity n, Monad m) => (forall k. t (S k) -> m (a, t k)) -> t n -> Fn n a (m b) -> m (b, t Z)
Files
- Data/Vector/Fixed.hs +27/−2
- Data/Vector/Fixed/Cont.hs +28/−0
- Data/Vector/Fixed/Internal.hs +19/−0
- Data/Vector/Fixed/Internal/Arity.hs +53/−30
- fixed-vector.cabal +10/−2
Data/Vector/Fixed.hs view
@@ -95,12 +95,16 @@ , convert , toList , fromList+ , fromList'+ , fromListM+ , fromFoldable -- * Data types , VecList(..)+ , Only(..) ) where -import Control.Applicative (Applicative(..))-import Data.Typeable (Typeable)+import Control.Applicative (Applicative(..),(<$>))+import Data.Data (Typeable,Data) import qualified Data.Foldable as F import qualified Data.Traversable as T @@ -203,3 +207,24 @@ instance Arity n => T.Traversable (VecList n) where sequenceA = sequenceA traverse = traverse+++-- | Single-element tuple.+newtype Only a = Only a+ deriving (Show,Eq,Ord,Typeable,Data)++instance Functor Only where+ fmap f (Only a) = Only (f a)+instance F.Foldable Only where+ foldr = foldr+instance T.Traversable Only where+ sequenceA (Only f) = Only <$> f+ traverse f (Only a) = Only <$> f a++type instance Dim Only = S Z++instance Vector Only a where+ construct = Fun Only+ inspect (Only a) (Fun f) = f a+ {-# INLINE construct #-}+ {-# INLINE inspect #-}
Data/Vector/Fixed/Cont.hs view
@@ -26,6 +26,8 @@ , cvec , empty , fromList+ , fromList'+ , fromListM , replicate , replicateM , generate@@ -192,7 +194,33 @@ step (T_flist [] ) = error "Data.Vector.Fixed.Cont.fromList: too few elements" step (T_flist (a:as)) = (a, T_flist as) +-- | Same as 'fromList' bu throws error is list doesn't have same+-- length as vector.+fromList' :: forall m n a. Arity n => [a] -> ContVecT m n a+{-# INLINE fromList' #-}+fromList' xs = ContVecT $ \(Fun fun) ->+ let (r,rest) = applyFun step (T_flist xs :: T_flist a n) fun+ step (T_flist [] ) = error "Data.Vector.Fixed.Cont.fromList': too few elements"+ step (T_flist (a:as)) = (a, T_flist as)+ in case rest of+ T_flist [] -> r+ _ -> error "Data.Vector.Fixed.Cont.fromList': too many elements"++-- | Convert list to continuation-based vector. Will fail with+-- 'Nothing' if list doesn't have right length.+fromListM :: forall n a. Arity n => [a] -> ContVecT Maybe n a+fromListM xs = ContVecT $ \(Fun fun) -> do+ (r,rest) <- applyFunM step (T_flist xs :: T_flist a n) fun+ case rest of+ T_flist [] -> return r+ _ -> Nothing+ where+ step (T_flist [] ) = Nothing+ step (T_flist (a:as)) = return (a, T_flist as)++ data T_flist a n = T_flist [a]+ -- | Execute monadic action for every element of vector. Synonym for 'pure'.
Data/Vector/Fixed/Internal.hs view
@@ -6,6 +6,7 @@ module Data.Vector.Fixed.Internal where import Control.Applicative (Applicative)+import qualified Data.Foldable as T import qualified Data.Traversable as T import Data.Vector.Fixed.Internal.Arity@@ -465,3 +466,21 @@ fromList :: (Vector v a) => [a] -> v a {-# INLINE fromList #-} fromList = C.vector . C.fromList++-- | Create vector form list. Will throw error if list has different+-- length from resulting vector.+fromList' :: (Vector v a) => [a] -> v a+{-# INLINE fromList' #-}+fromList' = C.vector . C.fromList'++-- | Create vector form list. Will return @Nothing@ if list has different+-- length from resulting vector.+fromListM :: (Vector v a) => [a] -> Maybe (v a)+{-# INLINE fromListM #-}+fromListM = C.vectorM . C.fromListM++-- | Create vector from 'Foldable' data type. Will return @Nothing@ if+-- data type different number of elements that resulting vector.+fromFoldable :: (Vector v a, T.Foldable f) => f a -> Maybe (v a)+{-# INLINE fromFoldable #-}+fromFoldable = fromListM . T.toList
Data/Vector/Fixed/Internal/Arity.hs view
@@ -19,6 +19,8 @@ , Fn , Fun(..) , Arity(..)+ , apply+ , applyM ) where ----------------------------------------------------------------@@ -78,42 +80,63 @@ -> Fn n a (m b) -- ^ Reduction function -- | Apply all parameters to the function.- apply :: (forall k. t (S k) -> (a, t k)) -- ^ Get value to apply to function- -> t n -- ^ Initial value- -> Fn n a b -- ^ N-ary function- -> b+ applyFun :: (forall k. t (S k) -> (a, t k)) -- ^ Get value to apply to function+ -> t n -- ^ Initial value+ -> Fn n a b -- ^ N-ary function+ -> (b, t Z) -- | Monadic apply- applyM :: Monad m- => (forall k. t (S k) -> m (a, t k)) -- ^ Get value to apply to function- -> t n -- ^ Initial value- -> Fn n a (m b) -- ^ N-ary function- -> m b+ applyFunM :: Monad m+ => (forall k. t (S k) -> m (a, t k)) -- ^ Get value to apply to function+ -> t n -- ^ Initial value+ -> Fn n a (m b) -- ^ N-ary function+ -> m (b, t Z) -- | Arity of function. arity :: n -> Int +-- | Apply all parameters to the function.+apply :: Arity n+ => (forall k. t (S k) -> (a, t k)) -- ^ Get value to apply to function+ -> t n -- ^ Initial value+ -> Fn n a b -- ^ N-ary function+ -> b+{-# INLINE apply #-}+apply step z f = fst $ applyFun step z f++-- | Apply all parameters to the function.+applyM :: (Arity n, Monad m)+ => (forall k. t (S k) -> m (a, t k)) -- ^ Get value to apply to function+ -> t n -- ^ Initial value+ -> Fn n a (m b) -- ^ N-ary function+ -> m b+{-# INLINE applyM #-}+applyM step z f = do+ (r,_) <- applyFunM step z f+ return r+ instance Arity Z where- accum _ g t = g t- accumM _ g t = g =<< t- apply _ _ h = h- applyM _ _ h = h+ accum _ g t = g t+ accumM _ g t = g =<< t+ applyFun _ t h = (h,t)+ applyFunM _ t h = do r <- h+ return (r,t) arity _ = 0- {-# INLINE accum #-}- {-# INLINE accumM #-}- {-# INLINE apply #-}- {-# INLINE arity #-}+ {-# INLINE accum #-}+ {-# INLINE accumM #-}+ {-# INLINE applyFun #-}+ {-# INLINE applyFunM #-}+ {-# INLINE arity #-} + instance Arity n => Arity (S n) where- accum f g t = \a -> accum f g (f t a)- accumM f g t = \a -> accumM f g $ flip f a =<< t- apply f t h = case f t of (a,u) -> apply f u (h a)- applyM f t h = do (a,u) <- f t- applyM f u (h a)- arity n = 1 + arity (prevN n)- where- prevN :: S n -> n- prevN _ = undefined- {-# INLINE accum #-}- {-# INLINE accumM #-}- {-# INLINE apply #-}- {-# INLINE arity #-}+ accum f g t = \a -> accum f g (f t a)+ accumM f g t = \a -> accumM f g $ flip f a =<< t+ applyFun f t h = case f t of (a,u) -> applyFun f u (h a)+ applyFunM f t h = do (a,u) <- f t+ applyFunM f u (h a)+ arity _ = 1 + arity (undefined :: n)+ {-# INLINE accum #-}+ {-# INLINE accumM #-}+ {-# INLINE applyFun #-}+ {-# INLINE applyFunM #-}+ {-# INLINE arity #-}
fixed-vector.cabal view
@@ -1,5 +1,5 @@ Name: fixed-vector-Version: 0.4.1.0+Version: 0.4.2.0 Synopsis: Generic vectors with statically known size. Description: Generic library for vectors with statically known@@ -45,7 +45,15 @@ [@Data.Vector.Fixed.Monomorphic@] Wrappers for monomorphic vectors .- Changes in 0.4.0.0+ Changes in 0.4.2.0+ .+ * 1-tuple @Only@ added.+ .+ * @fromList'@ and @fromListM@ added.+ .+ * @apply@ functions from @Arity@ type class generalized.+ .+ Changes in 0.4.1.0 . * @cons@ function added. .