Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

splitAtExactMay interface #240

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading