Skip to content

Commit 1b9ca0a

Browse files
committed
Add splitAt
1 parent e46daf7 commit 1b9ca0a

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/Data/Array.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module Data.Array
6969
, concatMap
7070
, filter
7171
, partition
72+
, splitAt
7273
, filterA
7374
, mapMaybe
7475
, catMaybes
@@ -600,6 +601,16 @@ foreign import partition
600601
-> Array a
601602
-> { yes :: Array a, no :: Array a }
602603

604+
-- | Splits an array into two pieces, where the first array has `n` elements
605+
-- | and the second array has the remaining elements.
606+
-- |
607+
-- | ```purescript
608+
-- | splitAt 3 [1, 2, 3, 4, 5] == Tuple [1, 2, 3] [4, 5]
609+
-- | ```
610+
splitAt :: forall a. Int -> Array a -> Tuple (Array a) (Array a)
611+
splitAt n xs | n < 0 = Tuple [] xs
612+
splitAt n xs = Tuple (slice 0 n xs) (slice n (length xs) xs)
613+
603614
-- | Filter where the predicate returns a `Boolean` in some `Applicative`.
604615
-- |
605616
-- | ```purescript

src/Data/Array/NonEmpty.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module Data.Array.NonEmpty
4848
, concat
4949
, concatMap
5050
, filter
51+
, splitAt
5152
, partition
5253
, filterA
5354
, mapMaybe
@@ -296,6 +297,9 @@ filterA
296297
-> f (Array a)
297298
filterA f = adaptAny $ A.filterA f
298299

300+
splitAt :: forall a. Int -> NonEmptyArray a -> Tuple (Array a) (Array a)
301+
splitAt n xs = A.splitAt n $ toArray xs
302+
299303
mapMaybe :: forall a b. (a -> Maybe b) -> NonEmptyArray a -> Array b
300304
mapMaybe f = adaptAny $ A.mapMaybe f
301305

test/Test/Data/Array.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ testArray = do
202202
log "filter should remove items that don't match a predicate"
203203
assert $ A.filter odd (A.range 0 10) == [1, 3, 5, 7, 9]
204204

205+
log "splitAt should split the array at the given number of elements"
206+
assert $ A.splitAt 3 [1, 2, 3, 4, 5] == Tuple [1, 2, 3] [4, 5]
207+
assert $ A.splitAt 1 [1, 2, 3] == Tuple [1] [2, 3]
208+
assert $ A.splitAt 3 [1, 2, 3] == Tuple [1, 2, 3] []
209+
assert $ A.splitAt 4 [1, 2, 3] == Tuple [1, 2, 3] []
210+
assert $ A.splitAt 0 [1, 2, 3] == Tuple [] [1, 2, 3]
211+
assert $ A.splitAt (-1) [1, 2, 3] == Tuple [] [1, 2, 3]
212+
205213
log "filterA should remove items that don't match a predicate while using an applicative behaviour"
206214
assert $ A.filterA (Just <<< odd) (A.range 0 10) == Just [1, 3, 5, 7, 9]
207215
assert $ A.filterA (const Nothing) (A.range 0 10) == Nothing

test/Test/Data/Array/NonEmpty.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ testNonEmptyArray = do
166166
log "filter should remove items that don't match a predicate"
167167
assert $ NEA.filter odd (NEA.range 0 10) == [1, 3, 5, 7, 9]
168168

169+
log "splitAt should split the array at the given number of elements"
170+
assert $ NEA.splitAt 3 (fromArray [1, 2, 3, 4, 5]) == Tuple [1, 2, 3] [4, 5]
171+
assert $ NEA.splitAt 1 (fromArray [1, 2, 3]) == Tuple [1] [2, 3]
172+
assert $ NEA.splitAt 3 (fromArray [1, 2, 3]) == Tuple [1, 2, 3] []
173+
assert $ NEA.splitAt 4 (fromArray [1, 2, 3]) == Tuple [1, 2, 3] []
174+
assert $ NEA.splitAt 0 (fromArray [1, 2, 3]) == Tuple [] [1, 2, 3]
175+
assert $ NEA.splitAt (-1) (fromArray [1, 2, 3]) == Tuple [] [1, 2, 3]
176+
169177
log "filterA should remove items that don't match a predicate while using an applicative behaviour"
170178
assert $ NEA.filterA (Just <<< odd) (NEA.range 0 10) == Just [1, 3, 5, 7, 9]
171179
assert $ NEA.filterA (const Nothing) (NEA.range 0 10) == Nothing

0 commit comments

Comments
 (0)