Skip to content

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

Merged
merged 3 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module Data.Array
, concatMap
, filter
, partition
, splitAt
, filterA
, mapMaybe
, catMaybes
Expand Down Expand Up @@ -600,6 +601,27 @@ foreign import partition
-> Array a
-> { yes :: Array a, no :: Array a }

-- | Splits an array into two subarrays, where `before` contains the elements
-- | up to (but not including) the given index, and `after` contains the rest
-- | of the elements, from that index on.
-- |
-- | ```purescript
-- | >>> splitAt 3 [1, 2, 3, 4, 5]
-- | { before: [1, 2, 3], after: [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: [] }

-- |
-- | Thus, the length of `(splitAt i arr).before` will equal either `i` or
-- | `length arr`, if that is shorter. (Or if `i` is negative the length will
-- | be 0.)
-- |
-- | ```purescript
-- | splitAt 2 ([] :: Array Int) == { before: [], after: [] }
-- | splitAt 3 [1, 2, 3, 4, 5] == { before: [1, 2, 3], after: [4, 5] }
-- | ```
splitAt :: forall a. Int -> Array a -> { before :: Array a, after :: Array a }
splitAt i xs | i <= 0 = { before: [], after: xs }
splitAt i xs = { before: slice 0 i xs, after: slice i (length xs) xs }

-- | Filter where the predicate returns a `Boolean` in some `Applicative`.
-- |
-- | ```purescript
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Data.Array.NonEmpty
, concat
, concatMap
, filter
, splitAt
, partition
, filterA
, mapMaybe
Expand Down Expand Up @@ -296,6 +297,9 @@ filterA
-> f (Array a)
filterA f = adaptAny $ A.filterA f

splitAt :: forall a. Int -> NonEmptyArray a -> { before :: Array a, after :: Array a }
splitAt i xs = A.splitAt i $ toArray xs

mapMaybe :: forall a b. (a -> Maybe b) -> NonEmptyArray a -> Array b
mapMaybe f = adaptAny $ A.mapMaybe f

Expand Down
9 changes: 9 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ testArray = do
log "filter should remove items that don't match a predicate"
assert $ A.filter odd (A.range 0 10) == [1, 3, 5, 7, 9]

log "splitAt should split the array at the given number of elements"
assert $ A.splitAt 2 ([] :: Array Int) == { before: [], after: [] }
assert $ A.splitAt 3 [1, 2, 3, 4, 5] == { before: [1, 2, 3], after: [4, 5] }
assert $ A.splitAt 1 [1, 2, 3] == { before: [1], after: [2, 3] }
assert $ A.splitAt 3 [1, 2, 3] == { before: [1, 2, 3], after: [] }
assert $ A.splitAt 4 [1, 2, 3] == { before: [1, 2, 3], after: [] }
assert $ A.splitAt 0 [1, 2, 3] == { before: [], after: [1, 2, 3] }
assert $ A.splitAt (-1) [1, 2, 3] == { before: [], after: [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: [] }

log "filterA should remove items that don't match a predicate while using an applicative behaviour"
assert $ A.filterA (Just <<< odd) (A.range 0 10) == Just [1, 3, 5, 7, 9]
assert $ A.filterA (const Nothing) (A.range 0 10) == Nothing
Expand Down
8 changes: 8 additions & 0 deletions test/Test/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ testNonEmptyArray = do
log "filter should remove items that don't match a predicate"
assert $ NEA.filter odd (NEA.range 0 10) == [1, 3, 5, 7, 9]

log "splitAt should split the array at the given number of elements"
assert $ NEA.splitAt 3 (fromArray [1, 2, 3, 4, 5]) == { before: [1, 2, 3], after: [4, 5] }
assert $ NEA.splitAt 1 (fromArray [1, 2, 3]) == { before: [1], after: [2, 3] }
assert $ NEA.splitAt 3 (fromArray [1, 2, 3]) == { before: [1, 2, 3], after: [] }
assert $ NEA.splitAt 4 (fromArray [1, 2, 3]) == { before: [1, 2, 3], after: [] }
assert $ NEA.splitAt 0 (fromArray [1, 2, 3]) == { before: [], after: [1, 2, 3] }
assert $ NEA.splitAt (-1) (fromArray [1, 2, 3]) == { before: [], after: [1, 2, 3] }

log "filterA should remove items that don't match a predicate while using an applicative behaviour"
assert $ NEA.filterA (Just <<< odd) (NEA.range 0 10) == Just [1, 3, 5, 7, 9]
assert $ NEA.filterA (const Nothing) (NEA.range 0 10) == Nothing
Expand Down