Skip to content

Commit

Permalink
Merge branch 'split-exact'
Browse files Browse the repository at this point in the history
  • Loading branch information
BebeSparkelSparkel committed Aug 27, 2024
2 parents 7f865a1 + 19a11c8 commit 41101ae
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mono-traversable/src/Data/Sequences.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ class (Monoid seq, MonoTraversable seq, SemiSequence seq, MonoPointed seq) => Is
unsafeSplitAt :: Index seq -> seq -> (seq, seq)
unsafeSplitAt i seq = (unsafeTake i seq, unsafeDrop i seq)

splitAtExactMay :: Index seq -> seq -> Maybe (seq, seq)
splitAtExactMay i seq = let x = splitAt i seq in if lengthIndex (fst x) == i then Just x else Nothing

-- | @'take' n@ returns the prefix of a sequence of length @n@, or the
-- sequence itself if @n > 'olength' seq@.
--
Expand All @@ -254,6 +257,9 @@ class (Monoid seq, MonoTraversable seq, SemiSequence seq, MonoPointed seq) => Is
unsafeTake :: Index seq -> seq -> seq
unsafeTake = take

takeExactMay :: Index seq -> seq -> Maybe seq
takeExactMay i = fmap fst . splitAtExactMay i

-- | @'drop' n@ returns the suffix of a sequence after the first @n@
-- elements, or an empty sequence if @n > 'olength' seq@.
--
Expand All @@ -270,6 +276,9 @@ class (Monoid seq, MonoTraversable seq, SemiSequence seq, MonoPointed seq) => Is
unsafeDrop :: Index seq -> seq -> seq
unsafeDrop = drop

dropExactMay :: Index seq -> seq -> Maybe seq
dropExactMay i = fmap snd . splitAtExactMay i

-- | Same as 'drop' but drops from the end of the sequence instead.
--
-- @
Expand Down

0 comments on commit 41101ae

Please sign in to comment.