Commits

Aleksey Khudyakov committed 086948a

Add snoc

  • Participants
  • Parent commits 52df45a
  • Branches version-0.5

Comments (0)

Files changed (4)

Data/Vector/Fixed.hs

   , head
   , tail
   , cons
+  , snoc
   , reverse
   , (!)
     -- ** Comparison

Data/Vector/Fixed/Cont.hs

   , imapM
   , tail
   , cons
+  , snoc
   , reverse
   , changeMonad
     -- ** Zips
 cons a (ContVecT cont) = ContVecT $ \f -> cont $ apFun f a
 {-# INLINE cons #-}
 
+-- | /O(1)/ Append element to vector
+snoc :: Arity n => a -> ContVecT m n a -> ContVecT m (S n) a
+snoc a (ContVecT cont) = ContVecT $ \f -> cont $ apLast f a
+{-# INLINE snoc #-}
+
 -- | Reverse order of elements in the vector
 reverse :: Arity n => ContVecT m n a -> ContVecT m n a
 reverse (ContVecT cont) = ContVecT $ cont . reverseF

Data/Vector/Fixed/Internal.hs

 {-# INLINE cons #-}
 cons a = C.vector . C.cons a . C.cvec
 
+-- | Cons element to the vector
+snoc :: (Vector v a, Vector w a, S (Dim v) ~ Dim w)
+     => a -> v a -> w a
+{-# INLINE snoc #-}
+snoc a = C.vector . C.snoc a . C.cvec
+
 -- | Reverse order of elements in the vector
 reverse :: Vector v a => v a -> v a
 reverse = C.vector . C.reverse . C.cvec

Data/Vector/Fixed/Internal/Arity.hs

   , applyM
     -- ** Combinators
   , apFun
+  , apLast
   , constFun
   , stepFun
   , hideLast
 apFun (Fun f) x = Fun (f x)
 {-# INLINE apFun #-}
 
+-- | Apply last parameter to function
+apLast :: Arity n => Fun (S n) a b -> a -> Fun n a b
+apLast f x = fmap ($ x) $ hideLast f
+{-# INLINE apLast #-}
+
+
 -- | Add one parameter to function which is ignored.
 constFun :: Fun n a b -> Fun (S n) a b
 constFun (Fun f) = Fun $ \_ -> f