Skip to content

Commit e502257

Browse files
notgiorgipaf31
authored andcommitted
dropEnd and takeEnd functions (#114)
* add takeEnd and dropEnd functions with docs * add tests * change order of functions
1 parent b363605 commit e502257

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Data/Array.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ module Data.Array
7979
, sortWith
8080
, slice
8181
, take
82+
, takeEnd
8283
, takeWhile
8384
, drop
85+
, dropEnd
8486
, dropWhile
8587
, span
8688
, group
@@ -495,6 +497,11 @@ foreign import slice :: forall a. Int -> Int -> Array a -> Array a
495497
-- | array.
496498
foreign import take :: forall a. Int -> Array a -> Array a
497499

500+
-- | Keep only a number of elements from the end of an array, creating a new
501+
-- | array.
502+
takeEnd :: forall a. Int -> Array a -> Array a
503+
takeEnd n xs = drop (length xs - n) xs
504+
498505
-- | Calculate the longest initial subarray for which all element satisfy the
499506
-- | specified predicate, creating a new array.
500507
takeWhile :: forall a. (a -> Boolean) -> Array a -> Array a
@@ -503,6 +510,10 @@ takeWhile p xs = (span p xs).init
503510
-- | Drop a number of elements from the start of an array, creating a new array.
504511
foreign import drop :: forall a. Int -> Array a -> Array a
505512

513+
-- | Drop a number of elements from the start of an array, creating a new array.
514+
dropEnd :: forall a. Int -> Array a -> Array a
515+
dropEnd n xs = take (length xs - n) xs
516+
506517
-- | Remove the longest initial subarray for which all element satisfy the
507518
-- | specified predicate, creating a new array.
508519
dropWhile :: forall a. (a -> Boolean) -> Array a -> Array a

test/Test/Data/Array.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ testArray = do
251251
assert $ (A.takeWhile (_ /= 3) [1, 2, 3]) == [1, 2]
252252
assert $ (A.takeWhile (_ /= 1) nil) == nil
253253

254+
log "take should keep the specified number of items from the end of an array, discarding the rest"
255+
assert $ (A.takeEnd 1 [1, 2, 3]) == [3]
256+
assert $ (A.takeEnd 2 [1, 2, 3]) == [2, 3]
257+
assert $ (A.takeEnd 1 nil) == nil
258+
254259
log "drop should remove the specified number of items from the front of an array"
255260
assert $ (A.drop 1 [1, 2, 3]) == [2, 3]
256261
assert $ (A.drop 2 [1, 2, 3]) == [3]
@@ -261,6 +266,11 @@ testArray = do
261266
assert $ (A.dropWhile (_ /= 2) [1, 2, 3]) == [2, 3]
262267
assert $ (A.dropWhile (_ /= 1) nil) == nil
263268

269+
log "drop should remove the specified number of items from the end of an array"
270+
assert $ (A.dropEnd 1 [1, 2, 3]) == [1, 2]
271+
assert $ (A.dropEnd 2 [1, 2, 3]) == [1]
272+
assert $ (A.dropEnd 1 nil) == nil
273+
264274
log "take and drop should treat negative arguments as zero"
265275
assert $ (A.take (-2) [1, 2, 3]) == nil
266276
assert $ (A.drop (-2) [1, 2, 3]) == [1, 2, 3]

0 commit comments

Comments
 (0)