Skip to content

Conversation

@notgiorgi
Copy link
Contributor

Copy link
Contributor

@safareli safareli left a comment

Choose a reason for hiding this comment

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

👍 just a small issue in test comments

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

log "take should keep the specified number of items from the end of an list, discarding the rest"
Copy link
Contributor

Choose a reason for hiding this comment

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

takeEnd should keep the specified number of items from the end of an list, discarding the rest

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

log "drop 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.

dropEnd should remove the specified number of items from the end of an 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

assert $ (takeWhile (_ /= 1) nil) == nil

log "drop should remove the specified number of items from the front of an list"
log "dropEnd should remove the specified number of items from the end 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.

You made this change in for wrong string :p

@notgiorgi
Copy link
Contributor Author

Looks good now?

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.

@paf31 paf31 merged commit 55b1848 into purescript:master Sep 9, 2017
@paf31
Copy link
Contributor

paf31 commented Sep 9, 2017

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants