-
Notifications
You must be signed in to change notification settings - Fork 70
Add splitAt
#179
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
Add splitAt
#179
Conversation
1b9ca0a
to
e3268cf
Compare
assert $ A.splitAt 4 [1, 2, 3] == Tuple [1, 2, 3] [] | ||
assert $ A.splitAt 0 [1, 2, 3] == Tuple [] [1, 2, 3] | ||
assert $ A.splitAt (-1) [1, 2, 3] == Tuple [] [1, 2, 3] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a test here for when the array argument is empty. I assume splitAt n []
produces Tuple [] []
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 6f414d6:
assert $ A.splitAt 2 ([] :: Array Int) == { before: [], after: [] }
-- | | ||
-- | ```purescript | ||
-- | splitAt 3 [1, 2, 3, 4, 5] == Tuple [1, 2, 3] [4, 5] | ||
-- | ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs should specify what happens when an empty array is the argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 6f414d6:
-- | splitAt 2 ([] :: Array Int) == { before: [], after: [] }
I wonder if we should have this return a record |
I'd agree with that. It also removes the need to document which array is the before/after part (though that itself is likely obvious). |
Funnily enough, I had initially considered doing this, but was hung up on the It wasn't until just now where I realized that, conceptually, taking |
I see it more like this: let { before, after} = splitAt i array
in
(before == take i array) && (after == drop i array) |
This PR adds a
splitAt
function forArray
s andNonEmptyArray
s.Prior Art
Haskell:
splitAt
fp-ts:
splitAt