Skip to content

Conversation

@maxdeviant
Copy link
Contributor

This PR adds a splitAt function for Arrays and NonEmptyArrays.

Prior Art

Haskell: splitAt
fp-ts: splitAt

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]

Copy link
Contributor

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 [] [].

Copy link
Contributor Author

@maxdeviant maxdeviant Sep 27, 2020

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]
-- | ```
Copy link
Contributor

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

Copy link
Contributor Author

@maxdeviant maxdeviant Sep 27, 2020

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: [] }

@hdgarrood
Copy link
Contributor

I wonder if we should have this return a record { before :: Array a, after :: Array a } rather than a Tuple, since we're already doing something similar with partition, and the splitAt for strings already returns a record: https://pursuit.purescript.org/packages/purescript-strings/4.0.1/docs/Data.String.CodePoints#v:splitAt

@JordanMartinez
Copy link
Contributor

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).

@maxdeviant
Copy link
Contributor Author

splitAt now returns { before :: Array a, after :: Array a } instead of a Tuple.

Funnily enough, I had initially considered doing this, but was hung up on the before terminology since I was thinking of it in terms of take n elements, which is inclusive.

It wasn't until just now where I realized that, conceptually, taking n elements from an array (inclusive) is the same as taking elements up to that index (exclusive).

@JordanMartinez
Copy link
Contributor

It wasn't until just now where I realized that, conceptually, taking n elements from an array (inclusive) is the same as taking elements up to that index (exclusive).

I see it more like this:

let { before, after} = splitAt i array
in 
  (before == take i array) && (after == drop i array)

@JordanMartinez JordanMartinez merged commit 72e4b24 into purescript:master Oct 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants