From 5297aea85d96d24acafe31da96cdbc6c9b022a05 Mon Sep 17 00:00:00 2001 From: flip111 Date: Sun, 12 Nov 2023 21:43:53 +0100 Subject: [PATCH] Add intersperse --- src/Data/Vector/NonEmpty.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Data/Vector/NonEmpty.hs b/src/Data/Vector/NonEmpty.hs index 2e24324..bda3a92 100644 --- a/src/Data/Vector/NonEmpty.hs +++ b/src/Data/Vector/NonEmpty.hs @@ -180,6 +180,9 @@ module Data.Vector.NonEmpty , scanl, scanl', scanl1, scanl1', iscanl, iscanl' , prescanr, prescanr', postscanr, postscanr' , scanr, scanr', scanr1, scanr1', iscanr, iscanr' + + -- * ?? +, intersperse ) where @@ -2646,3 +2649,17 @@ iscanr f b = NonEmptyVector . V.iscanr f b . _neVec iscanr' :: (Int -> a -> b -> b) -> b -> NonEmptyVector a -> NonEmptyVector b iscanr' f b = NonEmptyVector . V.iscanr' f b . _neVec {-# INLINE iscanr' #-} + +-- | \(\mathcal{O}(n)\). The 'intersperse' function takes an element and a NonEmptyVector +-- and \`intersperses\' that element between the elements of the NonEmptyVector. +-- +intersperse :: a -> NonEmptyVector a -> NonEmptyVector a +intersperse sep vne = + let (h, t) = uncons vne + in consV h (prependToAll sep t) +{-# INLINE intersperse #-} + +prependToAll :: a -> Vector a -> Vector a +prependToAll sep vec = case V.uncons vec of + Nothing -> V.empty + Just (h, t) -> V.cons sep (V.cons h (prependToAll sep t))