Skip to content

Commit

Permalink
added better initTails implementation for index based structures
Browse files Browse the repository at this point in the history
  • Loading branch information
BebeSparkelSparkel committed Nov 12, 2023
1 parent eb7af70 commit cb40f59
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions mono-traversable/src/Data/Sequences.hs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,9 @@ instance SemiSequence S.ByteString where
{-# INLINE cons #-}
{-# INLINE snoc #-}

initTailsViaSplitAt :: IsSequence seq => seq -> [(seq, seq)]
initTailsViaSplitAt x = fmap (`splitAt` x) [0 .. lengthIndex x]

instance IsSequence S.ByteString where
fromList = S.pack
lengthIndex = S.length
Expand Down Expand Up @@ -750,6 +753,9 @@ instance IsSequence S.ByteString where
{-# INLINE indexEx #-}
{-# INLINE unsafeIndex #-}

initTails = initTailsViaSplitAt
{-# INLINE initTails #-}

instance SemiSequence T.Text where
type Index T.Text = Int
intersperse = T.intersperse
Expand Down Expand Up @@ -814,6 +820,9 @@ instance IsSequence T.Text where
{-# INLINE indexEx #-}
{-# INLINE unsafeIndex #-}

initTails = initTailsViaSplitAt
{-# INLINE initTails #-}

instance SemiSequence L.ByteString where
type Index L.ByteString = Int64
intersperse = L.intersperse
Expand Down Expand Up @@ -1072,6 +1081,9 @@ instance IsSequence (V.Vector a) where
{-# INLINE indexEx #-}
{-# INLINE unsafeIndex #-}

initTails = initTailsViaSplitAt
{-# INLINE initTails #-}

instance U.Unbox a => SemiSequence (U.Vector a) where
type Index (U.Vector a) = Int

Expand Down Expand Up @@ -1145,6 +1157,9 @@ instance U.Unbox a => IsSequence (U.Vector a) where
{-# INLINE indexEx #-}
{-# INLINE unsafeIndex #-}

initTails = initTailsViaSplitAt
{-# INLINE initTails #-}

instance VS.Storable a => SemiSequence (VS.Vector a) where
type Index (VS.Vector a) = Int
reverse = VS.reverse
Expand Down Expand Up @@ -1218,6 +1233,9 @@ instance VS.Storable a => IsSequence (VS.Vector a) where
{-# INLINE indexEx #-}
{-# INLINE unsafeIndex #-}

initTails = initTailsViaSplitAt
{-# INLINE initTails #-}

-- | @'splitElem'@ splits a sequence into components delimited by separator
-- element. It's equivalent to 'splitWhen' with equality predicate:
--
Expand Down

0 comments on commit cb40f59

Please sign in to comment.