Skip to content

Add dropEnd and takeEnd #130

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 5 commits into from
Sep 9, 2017
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
16 changes: 16 additions & 0 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ module Data.List
, stripPrefix
, slice
, take
, takeEnd
, takeWhile
, drop
, dropEnd
, dropWhile
, span
, group
Expand Down Expand Up @@ -519,6 +521,13 @@ take = go Nil
go acc _ Nil = reverse acc
go acc n (x : xs) = go (x : acc) (n - 1) xs

-- | Take the specified number of elements from the end of a list.
-- |
-- | Running time: `O(2n - m)` where `n` is the number of elements in list
-- | and `m` is number of elements to take.
takeEnd :: forall a. Int -> List a -> List a
takeEnd n xs = drop (length xs - n) xs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This involves two traversals of the list, which we should try to avoid. Let's try to write these using foldr instead please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it even possible for this function to have better complexity?

in order to take n elements from the end we need to start iterating the list from the start till we reach length - n position and return the tail. So we require length (whole list iteration) and we need to determine the index we stop on (length - n elements iteration)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ignore me, this is fine as it is. I thought we could do it in one pass with a buffer of size n, but it's probably not worth it. We can revisit later if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative implementation will be to first reverse list and then take. it's performance will be n + m. it will be better then current version when m is smaller then n/2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first reverse list and then take and then reverse

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeh :D so that will be better when m is less then n/4. so never mind


-- | Take those elements from the front of a list which match a predicate.
-- |
-- | Running time (worst case): `O(n)`
Expand All @@ -536,6 +545,13 @@ drop 0 xs = xs
drop _ Nil = Nil
drop n (x : xs) = drop (n - 1) xs

-- | Drop the specified number of elements from the end of a list.
-- |
-- | Running time: `O(2n - m)` where `n` is the number of elements in list
-- | and `m` is number of elements to drop.
dropEnd :: forall a. Int -> List a -> List a
dropEnd n xs = take (length xs - n) xs

-- | Drop those elements from the front of a list which match a predicate.
-- |
-- | Running time (worst case): `O(n)`
Expand Down
14 changes: 12 additions & 2 deletions test/Test/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.List.NonEmpty as NEL
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Foldable (foldMap, foldl)
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, (:))
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, (:))
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.Monoid.Additive (Additive(..))
import Data.NonEmpty ((:|))
Expand Down Expand Up @@ -226,16 +226,26 @@ testList = do
assert $ (take 2 (l [1, 2, 3])) == l [1, 2]
assert $ (take 1 nil) == nil

log "takeEnd should keep the specified number of items from the end of an list, discarding the rest"
assert $ (takeEnd 1 (l [1, 2, 3])) == l [3]
assert $ (takeEnd 2 (l [1, 2, 3])) == l [2, 3]
assert $ (takeEnd 1 nil) == nil

log "takeWhile should keep all values that match a predicate from the front of an list"
assert $ (takeWhile (_ /= 2) (l [1, 2, 3])) == l [1]
assert $ (takeWhile (_ /= 3) (l [1, 2, 3])) == l [1, 2]
assert $ (takeWhile (_ /= 1) nil) == nil

log "drop should remove the specified number of items from the front of an list"
log "dropE should remove the specified number of items from the front of an list"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop not dropE :)

But this is fine.

assert $ (drop 1 (l [1, 2, 3])) == l [2, 3]
assert $ (drop 2 (l [1, 2, 3])) == l [3]
assert $ (drop 1 nil) == nil

log "dropEnd should remove the specified number of items from the end of an list"
assert $ (dropEnd 1 (l [1, 2, 3])) == l [1, 2]
assert $ (dropEnd 2 (l [1, 2, 3])) == l [1]
assert $ (dropEnd 1 nil) == nil

log "dropWhile should remove all values that match a predicate from the front of an list"
assert $ (dropWhile (_ /= 1) (l [1, 2, 3])) == l [1, 2, 3]
assert $ (dropWhile (_ /= 2) (l [1, 2, 3])) == l [2, 3]
Expand Down