Skip to content

Commit

Permalink
Merge pull request #481 from IntersectMBO/coot/seq-takeWhile
Browse files Browse the repository at this point in the history
Added takeWhileR, takeWhileL to Data.Sequence.Strict
  • Loading branch information
coot authored Jun 19, 2024
2 parents 849b461 + a72a543 commit caf8a35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cardano-strict-containers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for `cardano-strict-containers`

# next version

* Added `takeWhileR` and `takeWhileL` to `Data.Sequence.Strict`

# 0.1.3.1

*
Expand Down
16 changes: 16 additions & 0 deletions cardano-strict-containers/src/Data/Sequence/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module Data.Sequence.Strict
-- * Sublists

-- ** Sequential searches
takeWhileL,
takeWhileR,
dropWhileL,
dropWhileR,
spanl,
Expand Down Expand Up @@ -241,6 +243,20 @@ scanl f !z0 (StrictSeq xs) = StrictSeq $ forceElemsToWHNF (Seq.scanl f z0 xs)
Sublists
-------------------------------------------------------------------------------}

-- | \( O(i) \) where \( i \) is the prefix length. 'takeWhileL', applied
-- to a predicate @p@ and a sequence @xs@, returns the longest prefix
-- (possibly empty) of @xs@ of elements that satisfy @p@.
takeWhileL :: (a -> Bool) -> StrictSeq a -> StrictSeq a
takeWhileL p (StrictSeq xs) = StrictSeq (Seq.takeWhileL p xs)

-- | \( O(i) \) where \( i \) is the suffix length. 'takeWhileR', applied
-- to a predicate @p@ and a sequence @xs@, returns the longest suffix
-- (possibly empty) of @xs@ of elements that satisfy @p@.
--
-- @'takeWhileR' p xs@ is equivalent to @'reverse' ('takeWhileL' p ('reverse' xs))@.
takeWhileR :: (a -> Bool) -> StrictSeq a -> StrictSeq a
takeWhileR p (StrictSeq xs) = StrictSeq (Seq.takeWhileR p xs)

-- | \( O(i) \) where \( i \) is the prefix length. @'dropWhileL' p xs@ returns
-- the suffix remaining after @'takeWhileL' p xs@.
dropWhileL :: (a -> Bool) -> StrictSeq a -> StrictSeq a
Expand Down

0 comments on commit caf8a35

Please sign in to comment.