-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ((:|)) | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 reachlength - 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)There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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