Skip to content

Commit 55b1848

Browse files
notgiorgipaf31
authored andcommitted
Add dropEnd and takeEnd (#130)
* added takeEnd and dropEnd * added tests * fix the docs * fix test labels * fix test message
1 parent b1b118b commit 55b1848

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Data/List.purs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ module Data.List
6060
, stripPrefix
6161
, slice
6262
, take
63+
, takeEnd
6364
, takeWhile
6465
, drop
66+
, dropEnd
6567
, dropWhile
6668
, span
6769
, group
@@ -519,6 +521,13 @@ take = go Nil
519521
go acc _ Nil = reverse acc
520522
go acc n (x : xs) = go (x : acc) (n - 1) xs
521523

524+
-- | Take the specified number of elements from the end of a list.
525+
-- |
526+
-- | Running time: `O(2n - m)` where `n` is the number of elements in list
527+
-- | and `m` is number of elements to take.
528+
takeEnd :: forall a. Int -> List a -> List a
529+
takeEnd n xs = drop (length xs - n) xs
530+
522531
-- | Take those elements from the front of a list which match a predicate.
523532
-- |
524533
-- | Running time (worst case): `O(n)`
@@ -536,6 +545,13 @@ drop 0 xs = xs
536545
drop _ Nil = Nil
537546
drop n (x : xs) = drop (n - 1) xs
538547

548+
-- | Drop the specified number of elements from the end of a list.
549+
-- |
550+
-- | Running time: `O(2n - m)` where `n` is the number of elements in list
551+
-- | and `m` is number of elements to drop.
552+
dropEnd :: forall a. Int -> List a -> List a
553+
dropEnd n xs = take (length xs - n) xs
554+
539555
-- | Drop those elements from the front of a list which match a predicate.
540556
-- |
541557
-- | Running time (worst case): `O(n)`

test/Test/Data/List.purs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Data.List.NonEmpty as NEL
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77
import Data.Foldable (foldMap, foldl)
8-
import Data.List (List(..), (..), stripPrefix, Pattern(..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, partition, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
8+
import Data.List (List(..), (..), stripPrefix, Pattern(..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, partition, span, dropWhile, drop, dropEnd, takeWhile, take, takeEnd, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
99
import Data.Maybe (Maybe(..), isNothing, fromJust)
1010
import Data.Monoid.Additive (Additive(..))
1111
import Data.NonEmpty ((:|))
@@ -226,16 +226,26 @@ testList = do
226226
assert $ (take 2 (l [1, 2, 3])) == l [1, 2]
227227
assert $ (take 1 nil) == nil
228228

229+
log "takeEnd should keep the specified number of items from the end of an list, discarding the rest"
230+
assert $ (takeEnd 1 (l [1, 2, 3])) == l [3]
231+
assert $ (takeEnd 2 (l [1, 2, 3])) == l [2, 3]
232+
assert $ (takeEnd 1 nil) == nil
233+
229234
log "takeWhile should keep all values that match a predicate from the front of an list"
230235
assert $ (takeWhile (_ /= 2) (l [1, 2, 3])) == l [1]
231236
assert $ (takeWhile (_ /= 3) (l [1, 2, 3])) == l [1, 2]
232237
assert $ (takeWhile (_ /= 1) nil) == nil
233238

234-
log "drop should remove the specified number of items from the front of an list"
239+
log "dropE should remove the specified number of items from the front of an list"
235240
assert $ (drop 1 (l [1, 2, 3])) == l [2, 3]
236241
assert $ (drop 2 (l [1, 2, 3])) == l [3]
237242
assert $ (drop 1 nil) == nil
238243

244+
log "dropEnd should remove the specified number of items from the end of an list"
245+
assert $ (dropEnd 1 (l [1, 2, 3])) == l [1, 2]
246+
assert $ (dropEnd 2 (l [1, 2, 3])) == l [1]
247+
assert $ (dropEnd 1 nil) == nil
248+
239249
log "dropWhile should remove all values that match a predicate from the front of an list"
240250
assert $ (dropWhile (_ /= 1) (l [1, 2, 3])) == l [1, 2, 3]
241251
assert $ (dropWhile (_ /= 2) (l [1, 2, 3])) == l [2, 3]

0 commit comments

Comments
 (0)