Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 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,16 @@ foreign import partition
-> Array a
-> { yes :: Array a, no :: Array a }

-- | Splits an array into two pieces, where the first array has `n` elements
-- | and the second array has the remaining elements.
-- |
-- | ```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: [] }

splitAt :: forall a. Int -> Array a -> Tuple (Array a) (Array a)
splitAt n xs | n <= 0 = Tuple [] xs
splitAt n xs = Tuple (slice 0 n xs) (slice n (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 -> Tuple (Array a) (Array a)
splitAt n xs = A.splitAt n $ toArray xs

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

Expand Down
8 changes: 8 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ 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 3 [1, 2, 3, 4, 5] == Tuple [1, 2, 3] [4, 5]
assert $ A.splitAt 1 [1, 2, 3] == Tuple [1] [2, 3]
assert $ A.splitAt 3 [1, 2, 3] == Tuple [1, 2, 3] []
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: [] }

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]) == Tuple [1, 2, 3] [4, 5]
assert $ NEA.splitAt 1 (fromArray [1, 2, 3]) == Tuple [1] [2, 3]
assert $ NEA.splitAt 3 (fromArray [1, 2, 3]) == Tuple [1, 2, 3] []
assert $ NEA.splitAt 4 (fromArray [1, 2, 3]) == Tuple [1, 2, 3] []
assert $ NEA.splitAt 0 (fromArray [1, 2, 3]) == Tuple [] [1, 2, 3]
assert $ NEA.splitAt (-1) (fromArray [1, 2, 3]) == Tuple [] [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