Skip to content

Commit

Permalink
Merge pull request #8 from AlistairB/cons-snoc-v
Browse files Browse the repository at this point in the history
Add consV, snocV for building NonEmptyVector
  • Loading branch information
emilypi authored Nov 18, 2020
2 parents ce2bced + 670a586 commit b323fb6
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Data/Vector/NonEmpty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module Data.Vector.NonEmpty
, enumFromTo, enumFromThenTo

-- ** Concatenation
, cons, snoc, (++), concat, concat1
, cons, consV, snoc, snocV, (++), concat, concat1

-- ** Restricting memory usage
, force
Expand Down Expand Up @@ -1049,18 +1049,36 @@ enumFromThenTo a0 a1 a2 = fromVector (V.enumFromThenTo a0 a1 a2)
-- [1,2,3]
--
cons :: a -> NonEmptyVector a -> NonEmptyVector a
cons a (NonEmptyVector as) = NonEmptyVector (V.cons a as)
cons a (NonEmptyVector as) = consV a as
{-# INLINE cons #-}

-- | /O(n)/ Prepend an element to a Vector
--
-- >>> consV 1 (V.fromList [2,3])
-- [1,2,3]
--
consV :: a -> Vector a -> NonEmptyVector a
consV a = NonEmptyVector . V.cons a
{-# INLINE consV #-}

-- | /O(n)/ Append an element
--
-- >>> snoc (unsafeFromList [1,2]) 3
-- [1,2,3]
--
snoc :: NonEmptyVector a -> a -> NonEmptyVector a
snoc (NonEmptyVector as) a = NonEmptyVector (V.snoc as a)
snoc (NonEmptyVector as) = snocV as
{-# INLINE snoc #-}

-- | /O(n)/ Append an element to a Vector
--
-- >>> snocV (V.fromList [1,2]) 3
-- [1,2,3]
--
snocV :: Vector a -> a -> NonEmptyVector a
snocV as = NonEmptyVector . V.snoc as
{-# INLINE snocV #-}

-- | /O(m+n)/ Concatenate two non-empty vectors
--
-- >>> (unsafeFromList [1..3]) ++ (unsafeFromList [4..6])
Expand Down

0 comments on commit b323fb6

Please sign in to comment.