Skip to content

Don't force more than necessary in Data.List.Lazy take #139

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 6 commits into from
May 22, 2018
Merged

Don't force more than necessary in Data.List.Lazy take #139

merged 6 commits into from
May 22, 2018

Conversation

ibara1454
Copy link
Contributor

#138

Made a fix of take.

fix_take

@@ -509,10 +509,10 @@ slice start end xs = take (end - start) (drop start xs)
-- |
-- | Running time: `O(n)` where `n` is the number of elements to take.
take :: forall a. Int -> List a -> List a
take n = List <<< map (go n) <<< unwrap
take n _ | n <= 0 = nil
take n xs = List <<< map (go n) <<< unwrap $ xs
Copy link
Contributor

Choose a reason for hiding this comment

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

Tiny nitpick: You could probably just do $ unwrap xs, no?

Copy link
Contributor

Choose a reason for hiding this comment

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

(Or maintain the point-free form used previously.)

@matthewleon
Copy link
Contributor

matthewleon commented Jan 19, 2018

This looks good to me. I added a tiny comment about style.

@hdgarrood
Copy link
Contributor

👍 for keeping the previous pointfree form. Also would you mind adding a test? Perhaps something like this?

> oops x = oops x
> xs = defer \_ -> 1 : defer oops
> take 1 xs == fromFoldable [1]

The idea being this will test that we don't evaluate any more of the list than needed, since if we tried to, it would crash with a stack overflow.

@ibara1454
Copy link
Contributor Author

Thanks for the replys.:smiley:
About the style, I have tried the pointfree form.
Like this

take :: forall a. Int -> List a -> List a
take n | n <= 0 = const nil
take n = List <<< map (go n) <<< unwrap

But I think it is a little difficult to understand, since it used an additional const.
Is it better than the current one?

@hdgarrood
Copy link
Contributor

Yes, although come to think of it I think using if n <= 0 then const nil else ... would be better style here since we’re matching on the arguments in the same way in both alternatives currently.

@ibara1454
Copy link
Contributor Author

Done.:smiley:

  • Rewrite in pointfree style
  • Add test case

(I use oops x = 0 : (oops x) instead, since oops x = oops x is tail recursive)

@@ -270,6 +270,11 @@ testListLazy = do
assert $ (take 2 (l [1, 2, 3])) == l [1, 2]
assert $ (take 1 nil') == nil'

log "take should evaluate exactly n items which we needed"
assert let oops x = 0 : (oops x)
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry to nitpick here, but the parens around oops x aren't necessary and I think it would be better style to remove them, and also I think it would be a little nicer (and more in line with the style used elsewhere in this file) to use parens instead of $ here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I see! parens are not unnecessary here. Thanks for your correction.

* Remove parens
* Remove `($)` by let xs = 1 : defer oops
@ibara1454
Copy link
Contributor Author

Fixed.

  • Remove unnecessary parens
  • Remove ($)

Copy link
Contributor

@hdgarrood hdgarrood left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@hdgarrood hdgarrood changed the title Fix Data.List.Lazy take Don't force more than necessary in Data.List.Lazy take Jan 20, 2018
@garyb garyb changed the base branch from master to compiler/0.12 May 22, 2018 19:56
@garyb garyb merged commit a49621c into purescript:compiler/0.12 May 22, 2018
@ibara1454 ibara1454 deleted the issue138 branch August 13, 2018 10:10
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.

4 participants